12#include <nlohmann/json.hpp>
37 using json = nlohmann::json;
103 std::vector<std::string>
const& group_names);
152 template<
typename V> std::ostream&
operator<<(std::ostream& ostream, std::vector<V>
const& vec) {
153 json json_vector(vec);
154 return ostream << json_vector.dump();
157 template<
typename V,
typename C1,
typename C2>
158 std::ostream&
operator<<(std::ostream& ostream, std::set<V, C1, C2>
const& set) {
160 return ostream << json_set.dump();
163 template<
typename K,
typename V> std::ostream&
operator<<(std::ostream& ostream, std::map<K, V>
const& map) {
165 return ostream << json_map.dump();
168 template<
typename V> std::ostream&
operator<<(std::ostream& ostream, std::map<int, V>
const& map) {
169 std::map<std::string, V> string_map;
171 for (
auto const& [key, val] : map) {
172 string_map[std::to_string(key)] = val;
175 json json_map(string_map);
176 return ostream << json_map.dump();
194 template<>
struct adl_serializer<
ppforest2::stats::ConfusionMatrix> {
200 template<>
struct adl_serializer<
ppforest2::VariableImportance> {
206 template<>
struct adl_serializer<
ppforest2::serialization::Export<ppforest2::Tree>> {
210 template<>
struct adl_serializer<
ppforest2::serialization::Export<ppforest2::Forest>> {
214 template<>
struct adl_serializer<
ppforest2::serialization::Export<ppforest2::Model::Ptr>> {
nlohmann::json json
Definition CLI.integration.hpp:24
Confusion matrix for classification model evaluation.
JSON serialization and deserialization for ppforest2 models.
Definition Json.hpp:36
T from_json(json const &j)
Deserialize from a model block (integer labels only).
Forest from_json< Forest >(json const &j)
std::ostream & operator<<(std::ostream &os, TreeNode const &node)
VariableImportance from_json< VariableImportance >(json const &j)
std::vector< std::string > GroupNames
Group name vector for labeled serialization.
Definition Json.hpp:40
Tree from_json< Tree >(json const &j)
std::vector< std::string > to_labels(types::ResponseVector const &predictions, std::vector< std::string > const &group_names)
Map integer response codes to group name strings.
stats::ConfusionMatrix from_json< stats::ConfusionMatrix >(json const &j)
json to_json(Model const &model)
nlohmann::json json
Definition Json.hpp:37
Eigen::Matrix< Feature, Eigen::Dynamic, Eigen::Dynamic > FeatureMatrix
Dynamic-size matrix of feature values.
Definition Types.hpp:24
Eigen::Matrix< Response, Eigen::Dynamic, 1 > ResponseVector
Dynamic-size column vector of group labels.
Definition Types.hpp:29
Definition Benchmark.hpp:22
static ppforest2::Forest from_json(json const &j)
Definition Json.hpp:189
static ppforest2::Tree from_json(json const &j)
Definition Json.hpp:185
static ppforest2::VariableImportance from_json(json const &j)
Definition Json.hpp:201
static ppforest2::serialization::Export< ppforest2::Forest > from_json(json const &j)
static ppforest2::serialization::Export< ppforest2::Model::Ptr > from_json(json const &j)
static ppforest2::serialization::Export< ppforest2::Tree > from_json(json const &j)
static ppforest2::stats::ConfusionMatrix from_json(json const &j)
Definition Json.hpp:195
Definition BootstrapTree.hpp:9
A projection pursuit random forest.
Definition Forest.hpp:32
Visitor interface for model dispatch.
Definition Model.hpp:34
Abstract base class for predictive models (trees and forests).
Definition Model.hpp:25
std::shared_ptr< TrainingSpec > Ptr
Definition TrainingSpec.hpp:41
Internal split node in a projection pursuit tree.
Definition TreeCondition.hpp:19
Visitor interface for tree node dispatch.
Definition TreeNode.hpp:28
Abstract base class for nodes in a projection pursuit tree.
Definition TreeNode.hpp:19
Leaf node in a projection pursuit tree.
Definition TreeResponse.hpp:12
A single projection pursuit decision tree.
Definition Tree.hpp:26
Grouped result of the three variable importance measures.
Definition VariableImportance.hpp:27
A model bundled with its export metadata and optional metrics.
Definition Json.hpp:54
std::vector< std::string > feature_names
Definition Json.hpp:60
GroupNames groups
Definition Json.hpp:56
TrainingSpec::Ptr spec
Definition Json.hpp:57
int n_features
Definition Json.hpp:59
std::optional< double > oob_error
Definition Json.hpp:67
std::optional< stats::ConfusionMatrix > training_confusion_matrix
Definition Json.hpp:65
json to_json() const
Serialize to JSON. Only defined for Export<Model::Ptr>.
std::optional< VariableImportance > variable_importance
Definition Json.hpp:64
std::optional< stats::ConfusionMatrix > oob_confusion_matrix
Definition Json.hpp:66
T model
Definition Json.hpp:55
void compute_metrics(types::FeatureMatrix const &x, types::ResponseVector const &y)
Compute and store metrics on this export.
int n_observations
Definition Json.hpp:58
Visitor that serializes a model (Tree or Forest) to JSON.
Definition Json.hpp:94
void visit(Tree const &tree) override
json result
Definition Json.hpp:95
void visit(Forest const &forest) override
GroupNames const * group_names
Definition Json.hpp:96
Visitor that serializes a tree node to JSON.
Definition Json.hpp:86
json result
Definition Json.hpp:87
void visit(TreeResponse const &node) override
void visit(TreeCondition const &node) override
GroupNames const * group_names
Definition Json.hpp:88
A confusion matrix comparing predicted vs actual group labels.
Definition ConfusionMatrix.hpp:34