Medial Code Documentation
Loading...
Searching...
No Matches
MedLM.h
1#pragma once
3
4#define NITER 200
5#define EITER 0.00001
6#define RFACTOR 0.99
7
8#define XIDX(i,j,ncol) ((i)*(ncol) + (j))
9
10//======================================================================================
11// Linear Model: Linear Regression (+ Ridge)
12//======================================================================================
13
14#define LM_NITER 200
15#define LM_EITER 0.00001
16
18
19 // Required params
20 float eiter;
21 int niter;
22
23
25 int get_col = -1;
26
27 // Optional params
28 float rfactor;
29 float *rfactors;
30 float *corrs;
31 float *sumxx;
32
33 MedLMParams() { eiter = (float)EITER; niter = NITER; rfactor = 1.0; rfactors = NULL; corrs = NULL; sumxx = NULL; get_col = -1; }
34
35};
36
37class MedLM : public MedPredictor {
38public:
39 // Model
40 int n_ftrs;
41 vector<float> b;
42 float b0;
43 float err;
44
47
48 // Function
49 MedLM();
50 ~MedLM() {};
51 MedLM(void *params);
53 int init(void *params);
56 virtual int set_params(map<string, string>& mapper);
57 void init_defaults();
58
59 //int learn(MedMat<float> &x, MedMat<float> &y) {return (MedPredictor::learn(x,y));}; // Special case - un-normalized Y
61
62 int Learn(float *x, float *y, int nsamples, int nftrs);
63 int Learn(float *x, float *y, const float *w, int nsamples, int nftrs);
64
65 int Predict(float *x, float *&preds, int nsamples, int nftrs) const;
66 int Predict(float *x, float *&preds, int nsamples, int nftrs, int transposed_flag) const;
67
68 void normalize_x_and_y(float *x, float *y, const float *w, int nsamples, int nftrs, vector<float>& x_avg, vector<float>& x_std, float& y_avg, float& y_std);
69 int denormalize_model(float *f_avg, float *f_std, float lavel_avg, float label_std);
70 void print(FILE *fp, const string& prefix, int level = 0) const;
71
72 bool predict_single_not_implemented() { return true; }
73
74 ADD_CLASS_NAME(MedLM)
75 ADD_SERIALIZATION_FUNCS(classifier_type, n_ftrs, b0, b, err)
76};
77
78// Ancillary function for string analysis
79int init_farray(string& in, float **out);
80int init_darray(string& in, int **out);
81
82//======================================================================================
83// Linear Model: Linear Regression (+ Lasso)
84//======================================================================================
85
86#define LASSO_LAMBDA 0
87#define LASSO_NITER 1000
88
90
91 // Required params
92 double lambda;
93 int num_iterations;
94 MedLassoParams() { lambda = LASSO_LAMBDA; num_iterations = LASSO_NITER; }
95
96};
97
98class MedLasso : public MedPredictor {
99
100public:
101 // Model
102 int n_ftrs;
103 vector<float> b;
104 float b0;
105
108
109 // Work variables
110 double **trainx;
111 double *y1;
112
113 // Function
114 void Initb();
115 MedLasso();
116 ~MedLasso() {};
117 MedLasso(void *params);
119 int init(void *params);
122 int set_params(map<string, string>& mapper);
123 void init_defaults();
124
125 //int learn(MedMat<float> &x, MedMat<float> &y) {return (MedPredictor::learn(x,y));}; // Special case - un-normalized Y
126
127 int Learn(float *x, float *y, int nsamples, int nftrs);
128 int Learn(float *x, float *y, const float *w, int nsamples, int nftrs);
129
130 int Predict(float *x, float *&preds, int nsamples, int nftrs) const;
131 int Predict(float *x, float *&preds, int nsamples, int nftrs, int transposed_flag) const;
132
133 void normalize_x_and_y(float *x, float *y, const float *w, int nsamples, int nftrs, vector<float>& x_avg, vector<float>& x_std, float& y_avg, float& y_std);
134 int denormalize_model(float *f_avg, float *f_std, float lavel_avg, float label_std);
135
136 void initialize_vars(float *x_in, float *y_in, const float *w, vector<float>& b, int nrow_train, int n_ftrs);
137 void lasso_regression(vector<float>& b, int nrow_train, int n_ftrs, double lambda, int num_iterations);
138 void print(FILE *fp, const string& prefix, int level = 0) const;
139
140 ADD_CLASS_NAME(MedLasso)
142};
144int learn_lm(float *x, float *_y, const float *w, int nsamples, int nftrs, int niter, float eiter, float *rfactors, float *b, float *err, float *corrs);
146int learn_lm(float *x, float *_y, const float *w, int nsamples, int nftrs, int niter, float eiter, float *rfactors, float *b, float *err, float *corrs, float *sumxx);
147
148void init_default_lm_params(MedLMParams& _parmas);
149
MedAlgo - APIs to different algorithms: Linear Models, RF, GBM, KNN, and more.
#define ADD_SERIALIZATION_FUNCS(...)
Definition SerializableObject.h:122
#define MEDSERIALIZE_SUPPORT(Type)
Definition SerializableObject.h:108
Definition MedLM.h:37
int Predict(float *x, float *&preds, int nsamples, int nftrs) const
Predict should be implemented for each model.
Definition MedLM.cpp:225
MedLMParams params
Parameters.
Definition MedLM.h:46
bool predict_single_not_implemented()
Prepartion function for fast prediction on single item each time.
Definition MedLM.h:72
void calc_feature_contribs(MedMat< float > &x, MedMat< float > &contribs)
Feature contributions explains the prediction on each sample (aka BUT_WHY)
Definition MedLM.cpp:149
virtual int set_params(map< string, string > &mapper)
The parsed fields from init command.
Definition MedLM.cpp:64
Definition MedLM.h:98
int set_params(map< string, string > &mapper)
The parsed fields from init command.
Definition MedLasso.cpp:20
int Predict(float *x, float *&preds, int nsamples, int nftrs) const
Predict should be implemented for each model.
Definition MedLasso.cpp:170
MedLassoParams params
Parameters.
Definition MedLM.h:107
Definition MedMat.h:63
Base Interface for predictor.
Definition MedAlgo.h:78
MedPredictorTypes classifier_type
The Predicotr enum type.
Definition MedAlgo.h:80
Definition MedLM.h:17
int get_col
A simple way to check a single column , default is -1, but if >=0 the algorithm will simply return th...
Definition MedLM.h:25
Definition MedLM.h:89