learningmachine
Top-level package for learningmachine.
class
Base(sklearn.base.BaseEstimator):
23class Base(BaseEstimator): 24 """ 25 Base class. 26 """ 27 28 def __init__( 29 self, 30 type=None, 31 name="Base", 32 method="ranger", 33 pi_method="kdesplitconformal", 34 level=95, 35 B=100, 36 nb_hidden=0, 37 nodes_sim="sobol", 38 activ="relu", 39 params=None, 40 seed=123, 41 ): 42 """ 43 Initialize the model. 44 """ 45 super().__init__() 46 self.name = name 47 self.type = type 48 self.method = method 49 self.pi_method = pi_method 50 self.level = level 51 self.B = B 52 self.nb_hidden = nb_hidden 53 assert nodes_sim in ( 54 "sobol", 55 "halton", 56 "unif", 57 ), "must have nodes_sim in ('sobol', 'halton', 'unif')" 58 self.nodes_sim = "sobol" 59 assert activ in ( 60 "relu", 61 "sigmoid", 62 "tanh", 63 "leakyrelu", 64 "elu", 65 "linear", 66 ), "must have activ in ('relu', 'sigmoid', 'tanh', 'leakyrelu', 'elu', 'linear')" 67 self.activ = activ 68 self.params = params 69 self.seed = seed 70 self.obj = None 71 self.column_names = None 72 73 def load_learningmachine(self): 74 # Install R packages 75 # check "learningmachine" is installed 76 commands1_lm = 'base::system.file(package = "learningmachine")' 77 # check "learningmachine" is installed locally 78 commands2_lm = 'base::system.file("learningmachine_r", package = "learningmachine")' 79 exec_commands1_lm = subprocess.run( 80 ["Rscript", "-e", commands1_lm], capture_output=True, text=True 81 ) 82 exec_commands2_lm = subprocess.run( 83 ["Rscript", "-e", commands2_lm], capture_output=True, text=True 84 ) 85 if ( 86 len(exec_commands1_lm.stdout) == 7 87 and len(exec_commands2_lm.stdout) == 7 88 ): # kind of convoluted, but works 89 print("Installing R packages along with 'learningmachine'...") 90 commands1 = [ 91 'try(utils::install.packages(c("R6", "Rcpp", "skimr"), repos="https://cloud.r-project.org", dependencies = TRUE), silent=FALSE)', 92 'try(utils::install.packages("learningmachine", repos="https://techtonique.r-universe.dev", dependencies = TRUE), silent=FALSE)', 93 ] 94 commands2 = [ 95 'try(utils::install.packages(c("R6", "Rcpp", "skimr"), lib="./learningmachine_r", repos="https://cloud.r-project.org", dependencies = TRUE), silent=FALSE)', 96 'try(utils::install.packages("learningmachine", lib="./learningmachine_r", repos="https://techtonique.r-universe.dev", dependencies = TRUE), silent=FALSE)', 97 ] 98 try: 99 for cmd in commands1: 100 subprocess.run(["Rscript", "-e", cmd]) 101 except NotImplementedError as e: # can't install packages globally 102 subprocess.run(["mkdir", "learningmachine_r"]) 103 for cmd in commands2: 104 subprocess.run(["Rscript", "-e", cmd]) 105 106 try: 107 base.library(StrVector(["learningmachine"])) 108 except: # can't load the package from the global environment 109 try: 110 base.library( 111 StrVector(["learningmachine"]), 112 lib_loc="learningmachine_r", 113 ) 114 except: # well, we tried 115 try: 116 r( 117 "try(suppressWarnings(suppressMessages(library('learningmachine'))), silence=TRUE)" 118 ) 119 except: # well, we tried everything at this point 120 r( 121 "try(suppressWarnings(suppressMessages(library('learningmachine', lib.loc='learningmachine_r'))), silence=TRUE)" 122 ) 123 124 def score(self, X, y, scoring=None, **kwargs): 125 """Score the model on test set features X and response y. 126 127 Parameters: 128 129 X: {array-like}, shape = [n_samples, n_features] 130 Training vectors, where n_samples is the number 131 of samples and n_features is the number of features 132 133 y: array-like, shape = [n_samples] 134 Target values 135 136 scoring: str 137 must be in ('explained_variance', 'neg_mean_absolute_error', 138 'neg_mean_squared_error', 'neg_mean_squared_log_error', 139 'neg_median_absolute_error', 'r2') 140 141 **kwargs: additional parameters to be passed to scoring functions 142 143 Returns: 144 145 model scores: {array-like} 146 147 """ 148 149 preds = self.predict(X) 150 151 if self.type == "classification": 152 153 if scoring is None: 154 scoring = "accuracy" 155 156 # check inputs 157 assert scoring in ( 158 "accuracy", 159 "average_precision", 160 "brier_score_loss", 161 "f1", 162 "f1_micro", 163 "f1_macro", 164 "f1_weighted", 165 "f1_samples", 166 "neg_log_loss", 167 "precision", 168 "recall", 169 "roc_auc", 170 ), "'scoring' should be in ('accuracy', 'average_precision', \ 171 'brier_score_loss', 'f1', 'f1_micro', \ 172 'f1_macro', 'f1_weighted', 'f1_samples', \ 173 'neg_log_loss', 'precision', 'recall', \ 174 'roc_auc')" 175 176 scoring_options = { 177 "accuracy": skm.accuracy_score, 178 "average_precision": skm.average_precision_score, 179 "brier_score_loss": skm.brier_score_loss, 180 "f1": skm.f1_score, 181 "f1_micro": skm.f1_score, 182 "f1_macro": skm.f1_score, 183 "f1_weighted": skm.f1_score, 184 "f1_samples": skm.f1_score, 185 "neg_log_loss": skm.log_loss, 186 "precision": skm.precision_score, 187 "recall": skm.recall_score, 188 "roc_auc": skm.roc_auc_score, 189 } 190 191 try: 192 preds = preds.ravel().astype(int) 193 return scoring_options[scoring](y, preds, **kwargs) 194 except TypeError: 195 return scoring_options[scoring](y, preds, **kwargs) 196 197 if self.type == "regression": 198 199 if ( 200 type(preds) == tuple 201 ): # if there are std. devs in the predictions 202 preds = preds[0] 203 204 if scoring is None: 205 scoring = "neg_mean_squared_error" 206 207 # check inputs 208 assert scoring in ( 209 "explained_variance", 210 "neg_mean_absolute_error", 211 "neg_mean_squared_error", 212 "neg_mean_squared_log_error", 213 "neg_median_absolute_error", 214 "r2", 215 ), "'scoring' should be in ('explained_variance', 'neg_mean_absolute_error', \ 216 'neg_mean_squared_error', 'neg_mean_squared_log_error', \ 217 'neg_median_absolute_error', 'r2')" 218 219 scoring_options = { 220 "explained_variance": skm.explained_variance_score, 221 "neg_mean_absolute_error": skm.median_absolute_error, 222 "neg_mean_squared_error": skm.mean_squared_error, 223 "neg_mean_squared_log_error": skm.mean_squared_log_error, 224 "neg_median_absolute_error": skm.median_absolute_error, 225 "r2": skm.r2_score, 226 } 227 228 return scoring_options[scoring](y, preds, **kwargs) 229 230 def summary( 231 self, 232 X, 233 y, 234 class_index=None, 235 cl=None, 236 type_ci="student", 237 show_progress=True, 238 ): 239 240 if isinstance(X, pd.DataFrame): 241 X_r = r.matrix( 242 FloatVector(X.values.ravel()), 243 byrow=True, 244 ncol=X.shape[1], 245 nrow=X.shape[0], 246 ) 247 X_r.colnames = StrVector(self.column_names) 248 else: 249 X_r = r.matrix( 250 FloatVector(X.ravel()), 251 byrow=True, 252 ncol=X.shape[1], 253 nrow=X.shape[0], 254 ) 255 256 if cl is None: 257 258 if self.type == "classification": 259 260 assert ( 261 class_index is not None 262 ), "For classifiers, 'class_index' must be provided" 263 264 return self.obj["summary"]( 265 X=X_r, 266 y=FactorVector(IntVector(y)), 267 class_index=int(class_index) + 1, 268 type_ci=StrVector([type_ci]), 269 show_progress=show_progress, 270 ) 271 272 elif self.type == "regression": 273 274 return self.obj["summary"]( 275 X=X_r, 276 y=FloatVector(y), 277 type_ci=StrVector([type_ci]), 278 show_progress=show_progress, 279 ) 280 281 else: # cl is not None, parallel computing 282 283 pass
Base class.
def
score(self, X, y, scoring=None, **kwargs):
124 def score(self, X, y, scoring=None, **kwargs): 125 """Score the model on test set features X and response y. 126 127 Parameters: 128 129 X: {array-like}, shape = [n_samples, n_features] 130 Training vectors, where n_samples is the number 131 of samples and n_features is the number of features 132 133 y: array-like, shape = [n_samples] 134 Target values 135 136 scoring: str 137 must be in ('explained_variance', 'neg_mean_absolute_error', 138 'neg_mean_squared_error', 'neg_mean_squared_log_error', 139 'neg_median_absolute_error', 'r2') 140 141 **kwargs: additional parameters to be passed to scoring functions 142 143 Returns: 144 145 model scores: {array-like} 146 147 """ 148 149 preds = self.predict(X) 150 151 if self.type == "classification": 152 153 if scoring is None: 154 scoring = "accuracy" 155 156 # check inputs 157 assert scoring in ( 158 "accuracy", 159 "average_precision", 160 "brier_score_loss", 161 "f1", 162 "f1_micro", 163 "f1_macro", 164 "f1_weighted", 165 "f1_samples", 166 "neg_log_loss", 167 "precision", 168 "recall", 169 "roc_auc", 170 ), "'scoring' should be in ('accuracy', 'average_precision', \ 171 'brier_score_loss', 'f1', 'f1_micro', \ 172 'f1_macro', 'f1_weighted', 'f1_samples', \ 173 'neg_log_loss', 'precision', 'recall', \ 174 'roc_auc')" 175 176 scoring_options = { 177 "accuracy": skm.accuracy_score, 178 "average_precision": skm.average_precision_score, 179 "brier_score_loss": skm.brier_score_loss, 180 "f1": skm.f1_score, 181 "f1_micro": skm.f1_score, 182 "f1_macro": skm.f1_score, 183 "f1_weighted": skm.f1_score, 184 "f1_samples": skm.f1_score, 185 "neg_log_loss": skm.log_loss, 186 "precision": skm.precision_score, 187 "recall": skm.recall_score, 188 "roc_auc": skm.roc_auc_score, 189 } 190 191 try: 192 preds = preds.ravel().astype(int) 193 return scoring_options[scoring](y, preds, **kwargs) 194 except TypeError: 195 return scoring_options[scoring](y, preds, **kwargs) 196 197 if self.type == "regression": 198 199 if ( 200 type(preds) == tuple 201 ): # if there are std. devs in the predictions 202 preds = preds[0] 203 204 if scoring is None: 205 scoring = "neg_mean_squared_error" 206 207 # check inputs 208 assert scoring in ( 209 "explained_variance", 210 "neg_mean_absolute_error", 211 "neg_mean_squared_error", 212 "neg_mean_squared_log_error", 213 "neg_median_absolute_error", 214 "r2", 215 ), "'scoring' should be in ('explained_variance', 'neg_mean_absolute_error', \ 216 'neg_mean_squared_error', 'neg_mean_squared_log_error', \ 217 'neg_median_absolute_error', 'r2')" 218 219 scoring_options = { 220 "explained_variance": skm.explained_variance_score, 221 "neg_mean_absolute_error": skm.median_absolute_error, 222 "neg_mean_squared_error": skm.mean_squared_error, 223 "neg_mean_squared_log_error": skm.mean_squared_log_error, 224 "neg_median_absolute_error": skm.median_absolute_error, 225 "r2": skm.r2_score, 226 } 227 228 return scoring_options[scoring](y, preds, **kwargs)
Score the model on test set features X and response y.
Parameters:
X: {array-like}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number
of samples and n_features is the number of features
y: array-like, shape = [n_samples]
Target values
scoring: str
must be in ('explained_variance', 'neg_mean_absolute_error',
'neg_mean_squared_error', 'neg_mean_squared_log_error',
'neg_median_absolute_error', 'r2')
**kwargs: additional parameters to be passed to scoring functions
Returns:
model scores: {array-like}
22class Classifier(Base, ClassifierMixin): 23 """ 24 Classifier. 25 """ 26 27 def __init__( 28 self, 29 method="ranger", 30 pi_method="none", 31 level=95, 32 type_prediction_set="score", 33 B=100, 34 nb_hidden=0, 35 nodes_sim="sobol", 36 activ="relu", 37 seed=123, 38 ): 39 """ 40 Initialize the model. 41 """ 42 super().__init__( 43 name="Classifier", 44 type="classification", 45 method=method, 46 pi_method=pi_method, 47 level=level, 48 B=B, 49 nb_hidden=nb_hidden, 50 nodes_sim=nodes_sim, 51 activ=activ, 52 seed=seed, 53 ) 54 55 self.type_prediction_set = type_prediction_set 56 57 try: 58 r_obj_command = ( 59 "suppressWarnings(suppressMessages(library(learningmachine))); " 60 + "Classifier$new(method = " 61 + str(format_value(self.method)) 62 + ", " 63 + "pi_method = " 64 + str(format_value(self.pi_method)) 65 + ", " 66 + "level = " 67 + str(format_value(self.level)) 68 + ", " 69 + "type_prediction_set = " 70 + str(format_value(self.type_prediction_set)) 71 + ", " 72 + "B = " 73 + str(format_value(self.B)) 74 + ", " 75 + "nb_hidden = " 76 + str(format_value(self.nb_hidden)) 77 + ", " 78 + "nodes_sim = " 79 + str(format_value(self.nodes_sim)) 80 + ", " 81 + "activ = " 82 + str(format_value(self.activ)) 83 + ", " 84 + "seed = " 85 + str(format_value(self.seed)) 86 + ")" 87 ) 88 self.obj = r(r_obj_command) 89 except Exception: 90 try: 91 self.obj = r( 92 f"suppressWarnings(suppressMessages(library(learningmachine))); Classifier$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, type_prediction_set = {format_value(self.type_prediction_set)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})" 93 ) 94 except Exception: 95 self.obj = r( 96 f"learningmachine::Classifier$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, type_prediction_set = {format_value(self.type_prediction_set)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})" 97 ) 98 99 def fit(self, X, y, **kwargs): 100 """ 101 Fit the model according to the given training data. 102 """ 103 params_dict = {} 104 105 for k, v in kwargs.items(): 106 if k == "lambda_": 107 params_dict["lambda"] = v 108 elif "__" in k: 109 params_dict[k.replace("__", ".")] = v 110 else: 111 params_dict[k] = v 112 113 if isinstance(X, pd.DataFrame): 114 self.column_names = X.columns 115 X_r = r.matrix( 116 FloatVector(X.values.ravel()), 117 byrow=True, 118 ncol=X.shape[1], 119 nrow=X.shape[0], 120 ) 121 X_r.colnames = StrVector(self.column_names) 122 else: 123 X_r = r.matrix( 124 FloatVector(X.ravel()), 125 byrow=True, 126 ncol=X.shape[1], 127 nrow=X.shape[0], 128 ) 129 130 if isinstance(y, pd.DataFrame) or isinstance(y, pd.Series): 131 y = y.values.ravel() 132 133 self.obj["fit"](X_r, FactorVector(IntVector(y)), **params_dict) 134 self.classes_ = np.unique(y) # /!\ do not remove 135 return self 136 137 def predict_proba(self, X): 138 """ 139 Predict using the model. 140 """ 141 142 if isinstance(X, pd.DataFrame): 143 X_r = r.matrix( 144 FloatVector(X.values.ravel()), 145 byrow=True, 146 ncol=X.shape[1], 147 nrow=X.shape[0], 148 ) 149 X_r.colnames = StrVector(self.column_names) 150 else: 151 X_r = r.matrix( 152 FloatVector(X.ravel()), 153 byrow=True, 154 ncol=X.shape[1], 155 nrow=X.shape[0], 156 ) 157 158 if self.pi_method == "none": 159 if isinstance(X, pd.DataFrame): 160 res = self.obj["predict_proba"](X_r) 161 return np.asarray(res) 162 if isinstance(X, pd.DataFrame): 163 return r_list_to_namedtuple(self.obj["predict_proba"](X_r)) 164 165 def predict(self, X): 166 """ 167 Predict using the model. 168 """ 169 170 if isinstance(X, pd.DataFrame): 171 X_r = r.matrix( 172 FloatVector(X.values.ravel()), 173 byrow=True, 174 ncol=X.shape[1], 175 nrow=X.shape[0], 176 ) 177 X_r.colnames = StrVector(self.column_names) 178 else: 179 X_r = r.matrix( 180 FloatVector(X.ravel()), 181 byrow=True, 182 ncol=X.shape[1], 183 nrow=X.shape[0], 184 ) 185 186 if self.pi_method == "none": 187 return np.asarray(self.obj["predict"](X_r)) - 1 188 189 return r_list_to_namedtuple(self.obj["predict"](X_r))
Classifier.
def
fit(self, X, y, **kwargs):
99 def fit(self, X, y, **kwargs): 100 """ 101 Fit the model according to the given training data. 102 """ 103 params_dict = {} 104 105 for k, v in kwargs.items(): 106 if k == "lambda_": 107 params_dict["lambda"] = v 108 elif "__" in k: 109 params_dict[k.replace("__", ".")] = v 110 else: 111 params_dict[k] = v 112 113 if isinstance(X, pd.DataFrame): 114 self.column_names = X.columns 115 X_r = r.matrix( 116 FloatVector(X.values.ravel()), 117 byrow=True, 118 ncol=X.shape[1], 119 nrow=X.shape[0], 120 ) 121 X_r.colnames = StrVector(self.column_names) 122 else: 123 X_r = r.matrix( 124 FloatVector(X.ravel()), 125 byrow=True, 126 ncol=X.shape[1], 127 nrow=X.shape[0], 128 ) 129 130 if isinstance(y, pd.DataFrame) or isinstance(y, pd.Series): 131 y = y.values.ravel() 132 133 self.obj["fit"](X_r, FactorVector(IntVector(y)), **params_dict) 134 self.classes_ = np.unique(y) # /!\ do not remove 135 return self
Fit the model according to the given training data.
def
predict_proba(self, X):
137 def predict_proba(self, X): 138 """ 139 Predict using the model. 140 """ 141 142 if isinstance(X, pd.DataFrame): 143 X_r = r.matrix( 144 FloatVector(X.values.ravel()), 145 byrow=True, 146 ncol=X.shape[1], 147 nrow=X.shape[0], 148 ) 149 X_r.colnames = StrVector(self.column_names) 150 else: 151 X_r = r.matrix( 152 FloatVector(X.ravel()), 153 byrow=True, 154 ncol=X.shape[1], 155 nrow=X.shape[0], 156 ) 157 158 if self.pi_method == "none": 159 if isinstance(X, pd.DataFrame): 160 res = self.obj["predict_proba"](X_r) 161 return np.asarray(res) 162 if isinstance(X, pd.DataFrame): 163 return r_list_to_namedtuple(self.obj["predict_proba"](X_r))
Predict using the model.
def
predict(self, X):
165 def predict(self, X): 166 """ 167 Predict using the model. 168 """ 169 170 if isinstance(X, pd.DataFrame): 171 X_r = r.matrix( 172 FloatVector(X.values.ravel()), 173 byrow=True, 174 ncol=X.shape[1], 175 nrow=X.shape[0], 176 ) 177 X_r.colnames = StrVector(self.column_names) 178 else: 179 X_r = r.matrix( 180 FloatVector(X.ravel()), 181 byrow=True, 182 ncol=X.shape[1], 183 nrow=X.shape[0], 184 ) 185 186 if self.pi_method == "none": 187 return np.asarray(self.obj["predict"](X_r)) - 1 188 189 return r_list_to_namedtuple(self.obj["predict"](X_r))
Predict using the model.
16class Regressor(Base, RegressorMixin): 17 """ 18 Regressor. 19 """ 20 21 def __init__( 22 self, 23 method="ranger", 24 pi_method="none", 25 level=95, 26 B=100, 27 nb_hidden=0, 28 nodes_sim="sobol", 29 activ="relu", 30 seed=123, 31 ): 32 """ 33 Initialize the model. 34 """ 35 super().__init__( 36 name="Regressor", 37 type="regression", 38 method=method, 39 pi_method=pi_method, 40 level=level, 41 B=B, 42 nb_hidden=nb_hidden, 43 nodes_sim=nodes_sim, 44 activ=activ, 45 seed=seed, 46 ) 47 48 try: 49 r_obj_command = ( 50 "suppressWarnings(suppressMessages(library(learningmachine))); " 51 + "Regressor$new(method = " 52 + str(format_value(self.method)) 53 + ", " 54 + "pi_method = " 55 + str(format_value(self.pi_method)) 56 + ", " 57 + "level = " 58 + str(format_value(self.level)) 59 + ", " 60 + "B = " 61 + str(format_value(self.B)) 62 + ", " 63 + "nb_hidden = " 64 + str(format_value(self.nb_hidden)) 65 + ", " 66 + "nodes_sim = " 67 + str(format_value(self.nodes_sim)) 68 + ", " 69 + "activ = " 70 + str(format_value(self.activ)) 71 + ", " 72 + "seed = " 73 + str(format_value(self.seed)) 74 + ")" 75 ) 76 self.obj = r(r_obj_command) 77 except Exception: 78 try: 79 self.obj = r( 80 f"suppressWarnings(suppressMessages(library(learningmachine))); Regressor$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})" 81 ) 82 except Exception: 83 self.obj = r( 84 f"learningmachine::Regressor$new(method = {format_value(self.method)}, pi_method = {format_value(self.pi_method)}, level = {format_value(self.level)}, B = {format_value(self.B)}, nb_hidden = {format_value(self.nb_hidden)}, nodes_sim = {format_value(self.nodes_sim)}, activ = {format_value(self.activ)}, seed = {format_value(self.seed)})" 85 ) 86 87 def fit(self, X, y, **kwargs): 88 """ 89 Fit the model according to the given training data. 90 """ 91 params_dict = {} 92 93 for k, v in kwargs.items(): 94 if k == "lambda_": 95 params_dict["lambda"] = v 96 elif "__" in k: 97 params_dict[k.replace("__", ".")] = v 98 else: 99 params_dict[k] = v 100 101 if isinstance(X, pd.DataFrame): 102 self.column_names = X.columns 103 X_r = r.matrix( 104 FloatVector(X.values.ravel()), 105 byrow=True, 106 ncol=X.shape[1], 107 nrow=X.shape[0], 108 ) 109 X_r.colnames = StrVector(self.column_names) 110 else: 111 X_r = r.matrix( 112 FloatVector(X.ravel()), 113 byrow=True, 114 ncol=X.shape[1], 115 nrow=X.shape[0], 116 ) 117 118 if isinstance(y, pd.DataFrame) or isinstance(y, pd.Series): 119 y = y.values.ravel() 120 121 self.obj["fit"](X_r, FloatVector(y), **params_dict) 122 return self 123 124 def predict(self, X): 125 """ 126 Predict using the model. 127 """ 128 129 if isinstance(X, pd.DataFrame): 130 X_r = r.matrix( 131 FloatVector(X.values.ravel()), 132 byrow=True, 133 ncol=X.shape[1], 134 nrow=X.shape[0], 135 ) 136 X_r.colnames = StrVector(self.column_names) 137 else: 138 X_r = r.matrix( 139 FloatVector(X.ravel()), 140 byrow=True, 141 ncol=X.shape[1], 142 nrow=X.shape[0], 143 ) 144 145 if self.pi_method == "none": 146 return np.asarray(self.obj["predict"](X_r)) 147 return r_list_to_namedtuple(self.obj["predict"](X_r)) 148 149 def update(self, newx, newy, **kwargs): 150 """ 151 update the model. 152 """ 153 params_dict = {} 154 155 for k, v in kwargs.items(): 156 if k == "lambda_": 157 params_dict["lambda"] = v 158 elif "__" in k: 159 params_dict[k.replace("__", ".")] = v 160 else: 161 params_dict[k] = v 162 163 newx_r = base.as_vector(FloatVector(newx)) 164 165 self.obj["update"]( 166 newx_r, base.as_numeric(FloatVector([newy])), **params_dict 167 ) 168 169 return self
Regressor.
def
fit(self, X, y, **kwargs):
87 def fit(self, X, y, **kwargs): 88 """ 89 Fit the model according to the given training data. 90 """ 91 params_dict = {} 92 93 for k, v in kwargs.items(): 94 if k == "lambda_": 95 params_dict["lambda"] = v 96 elif "__" in k: 97 params_dict[k.replace("__", ".")] = v 98 else: 99 params_dict[k] = v 100 101 if isinstance(X, pd.DataFrame): 102 self.column_names = X.columns 103 X_r = r.matrix( 104 FloatVector(X.values.ravel()), 105 byrow=True, 106 ncol=X.shape[1], 107 nrow=X.shape[0], 108 ) 109 X_r.colnames = StrVector(self.column_names) 110 else: 111 X_r = r.matrix( 112 FloatVector(X.ravel()), 113 byrow=True, 114 ncol=X.shape[1], 115 nrow=X.shape[0], 116 ) 117 118 if isinstance(y, pd.DataFrame) or isinstance(y, pd.Series): 119 y = y.values.ravel() 120 121 self.obj["fit"](X_r, FloatVector(y), **params_dict) 122 return self
Fit the model according to the given training data.
def
predict(self, X):
124 def predict(self, X): 125 """ 126 Predict using the model. 127 """ 128 129 if isinstance(X, pd.DataFrame): 130 X_r = r.matrix( 131 FloatVector(X.values.ravel()), 132 byrow=True, 133 ncol=X.shape[1], 134 nrow=X.shape[0], 135 ) 136 X_r.colnames = StrVector(self.column_names) 137 else: 138 X_r = r.matrix( 139 FloatVector(X.ravel()), 140 byrow=True, 141 ncol=X.shape[1], 142 nrow=X.shape[0], 143 ) 144 145 if self.pi_method == "none": 146 return np.asarray(self.obj["predict"](X_r)) 147 return r_list_to_namedtuple(self.obj["predict"](X_r))
Predict using the model.