Medial Code Documentation
Loading...
Searching...
No Matches
Public Member Functions | Data Fields
MedLinearModel Class Reference

Linear Model with customizable SGD support. More...

#include <MedLinearModel.h>

Inheritance diagram for MedLinearModel:
MedPredictor PredictiveModel SerializableObject

Public Member Functions

int init (map< string, string > &mapper)
 The parsed fields from init command.
 
int set_params (map< string, string > &mapper)
 
subGradientFunction getSubGradients ()
 Subgradient of RMSE loss function.
 
subGradientFunction getSubGradientsAUC ()
 Subgradient of smooth auc loss function.
 
subGradientFunction getSubGradientsSvm ()
 Subgradient of svm loss function.
 
double predict (const vector< float > &input) const
 
void predict (const vector< vector< float > > &inputs, vector< double > &preds) const
 virtual to allow more efficeint implemention
 
PredictiveModelclone () const
 copy model
 
void print (const vector< string > &signalNames) const
 print model to stdout
 
void set_normalization (const vector< float > &meanShift, const vector< float > &factors)
 Normalization.
 
void apply_normalization (vector< vector< float > > &input) const
 apply Normalization
 
void get_normalization (vector< float > &meanShift, vector< float > &factors) const
 
int Learn (float *x, float *y, const float *w, int nsamples, int nftrs)
 Learn should be implemented for each model.
 
int Predict (float *x, float *&preds, int nsamples, int nftrs) const
 Predict should be implemented for each model.
 
void calc_feature_importance (vector< float > &features_importance_scores, const string &general_params, const MedFeatures *features)
 
- Public Member Functions inherited from MedPredictor
virtual int init (void *classifier_params)
 
int init_from_string (string initialization_text)
 
virtual void init_defaults ()
 
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_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
 
- Public Member Functions inherited from PredictiveModel
 PredictiveModel (string name)
 The name of the model.
 

Data Fields

double(* loss_function )(const vector< double > &got, const vector< float > &y, const vector< float > *weights)
 The custom loss_function.
 
double(* loss_function_step )(const vector< double > &, const vector< float > &, const vector< double > &, const vector< float > *)
 The custom loss_function step for sgd.
 
int sample_count
 The sample count of sgd.
 
int tot_steps
 The total iteration count of sgd.
 
double learning_rate
 The learning rate of sgd.
 
float block_num
 The blocking norm for parameter search in sgd.
 
bool norm_l1
 The blocking norm should be n1 or n2?
 
int print_steps
 how many prints for learn to print
 
bool print_model
 If true will print model coeff in the end.
 
int poly_degree
 add polynom degree
 
int min_cat
 control minimal samples per categ in categories
 
bool print_auc = false
 internal var if o print also auc metric
 
bool normalize = true
 iof true will do internal method for normalizing
 
- 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.
 
- Data Fields inherited from PredictiveModel
vector< double > model_params
 model parameters
 
string model_name
 model name
 

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)
 
- 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

Linear Model with customizable SGD support.

Member Function Documentation

◆ calc_feature_importance()

void MedLinearModel::calc_feature_importance ( vector< float > &  features_importance_scores,
const string &  general_params,
const MedFeatures features 
)
virtual

Reimplemented from MedPredictor.

◆ clone()

PredictiveModel * MedLinearModel::clone ( ) const
virtual

copy model

Implements PredictiveModel.

◆ getSubGradients()

subGradientFunction MedLinearModel::getSubGradients ( )
virtual

Subgradient of RMSE loss function.

Reimplemented from PredictiveModel.

◆ init()

int MedLinearModel::init ( map< string, string > &  mapper)
inlinevirtual

The parsed fields from init command.

if (it->first == "sample_count")
sample_count = stoi(it->second);
else if (it->first == "tot_steps")
tot_steps = stoi(it->second);
else if (it->first == "learning_rate")
learning_rate = stod(it->second);
else if (it->first == "block_num")
block_num = stof(it->second);
else if (it->first == "norm_l1")
norm_l1 = stoi(it->second) > 0;
else if (it->first == "normalize")
normalize = stoi(it->second) > 0;
else if (it->first == "print_steps")
print_steps = stoi(it->second);
else if (it->first == "poly_degree")
poly_degree = stoi(it->second);
else if (it->first == "print_model")
print_model = stoi(it->second) > 0;
else if (it->first == "min_cat")
min_cat = stoi(it->second);
else if (it->first == "loss_function") {
if (it->second == "rmse") {
loss_function = _linear_loss_target_rmse;
loss_function_step = _linear_loss_step_rmse;
min_cat = 0; //regression
}
else if (it->second == "auc") {
loss_function = _linear_loss_target_auc;
loss_function_step = _linear_loss_step_auc;
}
else if (it->second == "svm") {
loss_function = _linear_loss_target_svm;
loss_function_step = _linear_loss_step_svm;
print_auc = true;
}
else if (it->second == "work_point") {
loss_function = _linear_loss_target_work_point;
loss_function_step = _linear_loss_step_work_point;
print_auc = true;
}
else
invalid_argument("MedLinearModel::set_params - Unknown loss_function \"" + it->second + "\"");
}
else
throw invalid_argument("MedLinearModel::set_params - Unknown parameter \"" + it->first + "\"");
int poly_degree
add polynom degree
Definition MedLinearModel.h:44
double(* loss_function)(const vector< double > &got, const vector< float > &y, const vector< float > *weights)
The custom loss_function.
Definition MedLinearModel.h:34
double(* loss_function_step)(const vector< double > &, const vector< float > &, const vector< double > &, const vector< float > *)
The custom loss_function step for sgd.
Definition MedLinearModel.h:36
int print_steps
how many prints for learn to print
Definition MedLinearModel.h:42
bool norm_l1
The blocking norm should be n1 or n2?
Definition MedLinearModel.h:41
int min_cat
control minimal samples per categ in categories
Definition MedLinearModel.h:45
float block_num
The blocking norm for parameter search in sgd.
Definition MedLinearModel.h:40
double learning_rate
The learning rate of sgd.
Definition MedLinearModel.h:39
bool print_auc
internal var if o print also auc metric
Definition MedLinearModel.h:46
int tot_steps
The total iteration count of sgd.
Definition MedLinearModel.h:38
bool print_model
If true will print model coeff in the end.
Definition MedLinearModel.h:43
bool normalize
iof true will do internal method for normalizing
Definition MedLinearModel.h:47
int sample_count
The sample count of sgd.
Definition MedLinearModel.h:37

Reimplemented from MedPredictor.

◆ Learn()

int MedLinearModel::Learn ( float *  x,
float *  y,
const float *  w,
int  n_samples,
int  n_ftrs 
)
virtual

Learn should be implemented for each model.

This API always assumes the data is already normalized/transposed as needed, and never changes data in x,y,w. method should support calling with w=NULL.

Reimplemented from MedPredictor.

◆ predict() [1/2]

double MedLinearModel::predict ( const vector< float > &  input) const
virtual

Implements PredictiveModel.

◆ predict() [2/2]

void MedLinearModel::predict ( const vector< vector< float > > &  inputs,
vector< double > &  preds 
) const
virtual

virtual to allow more efficeint implemention

Reimplemented from PredictiveModel.

◆ Predict()

int MedLinearModel::Predict ( float *  x,
float *&  preds,
int  n_samples,
int  n_ftrs 
) const
virtual

Predict should be implemented for each model.

This API assumes x is normalized/transposed if needed. preds should either be pre-allocated or NULL - in which case the predictor should allocate it to the right size.

Reimplemented from MedPredictor.

◆ print()

void MedLinearModel::print ( const vector< string > &  signalNames) const
virtual

print model to stdout

Implements PredictiveModel.

◆ set_params()

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

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