Medial Code Documentation
Loading...
Searching...
No Matches
Public Member Functions
MedBART Class Reference

a wrapper for BART class model. More...

#include <MedBART.h>

Inheritance diagram for MedBART:
MedPredictor SerializableObject

Public Member Functions

void init_defaults ()
 an initialization for model
 
int set_params (map< string, string > &mapper)
 
int Learn (float *x, float *y, const float *w, int nsamples, int nftrs)
 learning on x vector which represents matrix.
 
int Predict (float *x, float *&preds, int nsamples, int nftrs) const
 prediction on x vector which represents matrix
 
 MedBART ()
 a simple default ctor
 
- Public Member Functions inherited from MedPredictor
virtual int init (void *classifier_params)
 
int init_from_string (string initialization_text)
 
virtual int init (map< string, string > &mapper)
 Virtual to init object from parsed fields.
 
virtual void print (FILE *fp, const string &prefix, int level=0) const
 
virtual int n_preds_per_sample () const
 Number of predictions per sample. typically 1 - but some models return several per sample (for example a probability vector)
 
virtual int denormalize_model (float *f_avg, float *f_std, float label_avg, float label_std)
 
int learn (float *x, float *y, int nsamples, int nftrs)
 simple no weights call
 
virtual int learn (MedMat< float > &x, MedMat< float > &y, const vector< float > &wgts)
 MedMat x,y : will transpose/normalize x,y if needed by algorithm The convention is that untransposed mats are always samples x features, and transposed are features x samples.
 
int learn (MedMat< float > &x, MedMat< float > &y)
 MedMat x,y : will transpose/normalize x,y if needed by algorithm The convention is that untransposed mats are always samples x features, and transposed are features x samples.
 
int learn (MedMat< float > &x, vector< float > &y, const vector< float > &wgts)
 MedMat x, vector y: will transpose normalize x if needed (y assumed to be normalized)
 
int learn (MedMat< float > &x, vector< float > &y)
 MedMat x, vector y: will transpose normalize x if needed (y assumed to be normalized)
 
int learn (vector< float > &x, vector< float > &y, const vector< float > &wgts, int n_samples, int n_ftrs)
 vector x,y: transpose/normalizations not done.
 
int learn (vector< float > &x, vector< float > &y, int n_samples, int n_ftrs)
 vector x,y: transpose/normalizations not done.
 
virtual int predict (MedMat< float > &x, vector< float > &preds) const
 
int predict (vector< float > &x, vector< float > &preds, int n_samples, int n_ftrs) const
 
int threaded_predict (MedMat< float > &x, vector< float > &preds, int nthreads) const
 
int learn (const MedFeatures &features)
 
int learn (const MedFeatures &features, vector< string > &names)
 
virtual int predict (MedFeatures &features) const
 
virtual void calc_feature_importance (vector< float > &features_importance_scores, const string &general_params)
 Feature Importance - assume called after learn.
 
virtual void calc_feature_importance (vector< float > &features_importance_scores, const string &general_params, const MedFeatures *features)
 
virtual void calc_feature_contribs (MedMat< float > &x, MedMat< float > &contribs)
 Feature contributions explains the prediction on each sample (aka BUT_WHY)
 
virtual void calc_feature_contribs_conditional (MedMat< float > &mat_x_in, unordered_map< string, float > &contiditional_variables, MedMat< float > &mat_x_out, MedMat< float > &mat_contribs)
 
virtual void export_predictor (const string &output_fname)
 
int learn_prob_calibration (MedMat< float > &x, vector< float > &y, vector< float > &min_range, vector< float > &max_range, vector< float > &map_prob, int min_bucket_size=10000, float min_score_jump=0.001, float min_prob_jump=0.005, bool fix_prob_order=false)
 calibration for probability using training data
 
int convert_scores_to_prob (const vector< float > &preds, const vector< float > &min_range, const vector< float > &max_range, const vector< float > &map_prob, vector< float > &probs) const
 If you have ran learn_prob_calibration before, you have min_range,max_range,map_prob from This function - that is used to convert preds to probs.
 
int learn_prob_calibration (MedMat< float > &x, vector< float > &y, int poly_rank, vector< double > &params, int min_bucket_size=10000, float min_score_jump=0.001)
 Will create probability bins using Platt scale method.
 
template<class T , class L >
int convert_scores_to_prob (const vector< T > &preds, const vector< double > &params, vector< L > &converted) const
 Converts probability from Platt scale model.
 
virtual bool predict_single_not_implemented ()
 Prepartion function for fast prediction on single item each time.
 
virtual void prepare_predict_single ()
 
virtual void predict_single (const vector< float > &x, vector< float > &preds) const
 
virtual void predict_single (const vector< double > &x, vector< double > &preds) const
 
virtual void calc_feature_importance_shap (vector< float > &features_importance_scores, string &importance_type, const MedFeatures *features)
 
void * new_polymorphic (string derived_class_name)
 for polymorphic classes that want to be able to serialize/deserialize a pointer * to the derived class given its type one needs to implement this function to return a new to the derived class given its type (as in my_type)
 
size_t get_predictor_size ()
 
size_t predictor_serialize (unsigned char *blob)
 
- Public Member Functions inherited from SerializableObject
virtual int version () const
 Relevant for serializations.
 
virtual string my_class_name () const
 For better handling of serializations it is highly recommended that each SerializableObject inheriting class will implement the next method.
 
virtual void serialized_fields_name (vector< string > &field_names) const
 The names of the serialized fields.
 
virtual void pre_serialization ()
 
virtual void post_deserialization ()
 
virtual size_t get_size ()
 Gets bytes sizes for serializations.
 
virtual size_t serialize (unsigned char *blob)
 Serialiazing object to blob memory. return number ob bytes wrote to memory.
 
virtual size_t deserialize (unsigned char *blob)
 Deserialiazing blob to object. returns number of bytes read.
 
size_t serialize_vec (vector< unsigned char > &blob)
 
size_t deserialize_vec (vector< unsigned char > &blob)
 
virtual size_t serialize (vector< unsigned char > &blob)
 
virtual size_t deserialize (vector< unsigned char > &blob)
 
virtual int read_from_file (const string &fname)
 read and deserialize model
 
virtual int write_to_file (const string &fname)
 serialize model and write to file
 
virtual int read_from_file_unsafe (const string &fname)
 read and deserialize model without checking version number - unsafe read
 
int init_from_string (string init_string)
 Init from string.
 
int init_params_from_file (string init_file)
 
int init_param_from_file (string file_str, string &param)
 
int update_from_string (const string &init_string)
 
virtual int update (map< string, string > &map)
 Virtual to update object from parsed fields.
 
virtual string object_json () const
 

Additional Inherited Members

- Static Public Member Functions inherited from MedPredictor
static MedPredictormake_predictor (string model_type)
 
static MedPredictormake_predictor (MedPredictorTypes model_type)
 
static MedPredictormake_predictor (string model_type, string params)
 
static MedPredictormake_predictor (MedPredictorTypes model_type, string params)
 
- Data Fields inherited from MedPredictor
MedPredictorTypes classifier_type
 The Predicotr enum type.
 
bool transpose_for_learn
 True if need to transpose before learn.
 
bool normalize_for_learn
 True if need to normalize before learn.
 
bool normalize_y_for_learn
 True if need to normalize labels before learn.
 
bool transpose_for_predict
 True if need to transpose before predict.
 
bool normalize_for_predict
 True if need to normalize before predict.
 
vector< string > model_features
 The model features used in Learn, to validate when caling predict.
 
int features_count = 0
 The model features count used in Learn, to validate when caling predict.
 
- Protected Member Functions inherited from MedPredictor
void prepare_x_mat (MedMat< float > &x, const vector< float > &wgts, int &nsamples, int &nftrs, bool transpose_needed) const
 
void predict_thread (void *p) const
 

Detailed Description

a wrapper for BART class model.

BART is forest of binary trees - sum of trees. the model created MCMC and tries to maximize likelihood using stochstic process

Member Function Documentation

◆ init_defaults()

void MedBART::init_defaults ( )
inlinevirtual

an initialization for model

unordered_map<string, int> map_types = {
{ "classification", bart_data_prior_type::classification },
{ "regression_mean_shift", bart_data_prior_type::regression_mean_shift }
};
string all_options = medial::io::get_list(map_types);
for (auto it = mapper.begin(); it != mapper.end(); ++it)
{
if (it->first == "ntrees")
ntrees = stoi(it->second);
else if (it->first == "iter_count")
iter_count = stoi(it->second);
else if (it->first == "burn_count")
burn_count = stoi(it->second);
else if (it->first == "restart_count")
restart_count = stoi(it->second);
else if (it->first == "tree_params") {
map<string, string> tree_args;
MedSerialize::initialization_text_to_map(it->second, tree_args);
for (auto ti = tree_args.begin(); ti != tree_args.end(); ++ti)
{
if (ti->first == "k")
tree_params.k = stof(ti->second);
else if (ti->first == "alpha")
tree_params.alpha = stof(ti->second);
else if (ti->first == "beta")
tree_params.beta = stof(ti->second);
else if (ti->first == "lambda")
tree_params.lambda = stof(ti->second);
else if (ti->first == "nu")
tree_params.nu = stof(ti->second);
else if (ti->first == "min_obs_in_node")
tree_params.min_obs_in_node = stoi(ti->second);
else if (ti->first == "data_prior_type") {
if (map_types.find(ti->second) == map_types.end()) {
MTHROW_AND_ERR("unsupported data_prior_type \"%s\". options are: %s\n",
ti->second.c_str(), all_options.c_str());
}
else
tree_params.data_prior_type = bart_data_prior_type(map_types.at(ti->second));
}
else
MTHROW_AND_ERR("Unsupported argument \"%s\" for tree_params\n", ti->first.c_str());
}
}
else
MTHROW_AND_ERR("Unsupported argument \"%s\" for MedBart\n", it->first.c_str());
if (tree_params.data_prior_type == classification)
tree_params.set_classification(ntrees);
//else if (tree_params.data_prior_type == regression_mean_shift)
// tree_params.set_regression(ntrees, );
}
float nu
the node-data dict params for sigma_i: sigma_i ~ IG(nu, mean_sigma*lambda/2)
Definition BART.h:123
float lambda
the node-data dict params for sigma_i: sigma_i ~ IG(mean_sigma/2, mean_sigma*lambda/2)
Definition BART.h:124
float k
the range for bandwidth interval
Definition BART.h:122
float beta
prior for tree structure: alpha * (1 + depth(node)) ^ -beta
Definition BART.h:116
int min_obs_in_node
minimal allowed observations in node
Definition BART.h:113
void set_classification(int num_trees)
an initializer for classification problems
Definition BART.h:144
float alpha
prior for tree structure: alpha * (1 + depth(node)) ^ -beta
Definition BART.h:115
float stof(const std::string &value, size_t *pos=nullptr)
A faster implementation of stof(). See documentation of std::stof() for more information....
Definition strtonum.h:467

Reimplemented from MedPredictor.

◆ Learn()

int MedBART::Learn ( float *  x,
float *  y,
const float *  w,
int  nsamples,
int  nftrs 
)
virtual

learning on x vector which represents matrix.

y is the labels

Parameters
xa vector which represnts matrix. the data is ordered by observations. we first see first observation all features and than second obseravtion all features...
ylabels vector for each observation in x

Reimplemented from MedPredictor.

◆ Predict()

int MedBART::Predict ( float *  x,
float *&  preds,
int  nsamples,
int  nftrs 
) const
virtual

prediction on x vector which represents matrix

Parameters
xa vector which represnts matrix. the data is ordered by observations. we first see first observation all features and than second obseravtion all features...
nsamplesthe number of samples in x
nftrsthe number of features in x
Returns

preds the result scores for each observation

Reimplemented from MedPredictor.

◆ set_params()

int MedBART::set_params ( map< string, string > &  mapper)
virtual

[MedBART::init]

[MedBART::init]

Reimplemented from MedPredictor.


The documentation for this class was generated from the following files: