Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.rf_trainer_metarf = Model(drop_rate=1,
verbose=3,
algo='RandomForestRegressor',
meta_algo='RF')
self.svc_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='SVC', meta_algo='RF')
self.km_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='KMeans',
meta_algo='RF')
self.rf_trainer_metann = Model(drop_rate=1,
verbose=3, algo='RandomForestRegressor',
meta_algo='NN')
self.svc_trainer_metann = Model(drop_rate=1,
verbose=3, algo='SVC', meta_algo='NN')
self.km_trainer_metann = Model(drop_rate=1,
verbose=3, algo='KMeans',
meta_algo='NN')
def setUp(self):
self.rf_trainer_metarf = Model(drop_rate=1,
verbose=3,
algo='RandomForestRegressor',
meta_algo='RF')
self.svc_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='SVC', meta_algo='RF')
self.km_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='KMeans',
meta_algo='RF')
self.rf_trainer_metann = Model(drop_rate=1,
verbose=3, algo='RandomForestRegressor',
meta_algo='NN')
self.svc_trainer_metann = Model(drop_rate=1,
verbose=3, algo='SVC', meta_algo='NN')
self.km_trainer_metann = Model(drop_rate=1,
verbose=3, algo='KMeans',
meta_algo='NN')
verbose=3,
algo='RandomForestRegressor',
meta_algo='RF')
self.svc_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='SVC', meta_algo='RF')
self.km_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='KMeans',
meta_algo='RF')
self.rf_trainer_metann = Model(drop_rate=1,
verbose=3, algo='RandomForestRegressor',
meta_algo='NN')
self.svc_trainer_metann = Model(drop_rate=1,
verbose=3, algo='SVC', meta_algo='NN')
self.km_trainer_metann = Model(drop_rate=1,
verbose=3, algo='KMeans',
meta_algo='NN')
def setUp(self):
self.rf_trainer_metarf = Model(drop_rate=1,
verbose=3,
algo='RandomForestRegressor',
meta_algo='RF')
self.svc_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='SVC', meta_algo='RF')
self.km_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='KMeans',
meta_algo='RF')
self.rf_trainer_metann = Model(drop_rate=1,
verbose=3, algo='RandomForestRegressor',
meta_algo='NN')
self.svc_trainer_metann = Model(drop_rate=1,
self.svc_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='SVC', meta_algo='RF')
self.km_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='KMeans',
meta_algo='RF')
self.rf_trainer_metann = Model(drop_rate=1,
verbose=3, algo='RandomForestRegressor',
meta_algo='NN')
self.svc_trainer_metann = Model(drop_rate=1,
verbose=3, algo='SVC', meta_algo='NN')
self.km_trainer_metann = Model(drop_rate=1,
verbose=3, algo='KMeans',
meta_algo='NN')
def setUp(self):
self.rf_trainer_metarf = Model(drop_rate=1,
verbose=3,
algo='RandomForestRegressor',
meta_algo='RF')
self.svc_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='SVC', meta_algo='RF')
self.km_trainer_metarf = Model(drop_rate=1,
verbose=3, algo='KMeans',
meta_algo='RF')
self.rf_trainer_metann = Model(drop_rate=1,
verbose=3, algo='RandomForestRegressor',
meta_algo='NN')
self.svc_trainer_metann = Model(drop_rate=1,
verbose=3, algo='SVC', meta_algo='NN')
self.km_trainer_metann = Model(drop_rate=1,
verbose=3, algo='KMeans',
meta_algo='NN')
def setUp(self):
self.estimator_metarf = Estimator(meta_algo='RF', verbose=0)
self.estimator_metann = Estimator(meta_algo='NN', verbose=0)
def setUp(self):
self.estimator_metarf = Estimator(meta_algo='RF', verbose=0)
self.estimator_metann = Estimator(meta_algo='NN', verbose=0)
train_test_split(X, y, test_size=0.20, random_state=42)
if self.meta_algo == 'NN':
X_train_scaled, X_test_scaled = \
self._scale_data(X_train, X_test, save_model)
meta_algo.fit(X_train_scaled, y_train)
else:
meta_algo.fit(X_train, y_train)
if save_model:
if self.verbose >= 2:
self.logger.info(f'''Saving {self.meta_algo} to {self.meta_algo}_{self.algo}_estimator.pkl''')
model_path = f'''{get_path("models")}/{self.meta_algo}_{self.algo}_estimator.pkl'''
joblib.dump(meta_algo, model_path)
json_path = f'''{get_path("models")}/{self.meta_algo}_{self.algo}_estimator.json'''
with open(json_path, 'w') as outfile:
json.dump({"dummy": list(cols),
"original": list(original_cols)}, outfile)
if self.meta_algo == 'NN':
if self.verbose >= 2:
self.logger.info(f'''R squared on train set is {r2_score(y_train, meta_algo.predict(X_train_scaled))}''')
# MAPE is the mean absolute percentage error
test_relu = [max(i, 0) for i in meta_algo.predict(X_test_scaled)]
train_relu = [max(i, 0) for i in meta_algo.predict(X_train_scaled)]
saves the scaler as a pkl file if specified
:param X_train: pd.DataFrame chosen as input for the training set
:param X_test: pd.DataFrame chosen as input for the test set
:param save_model: boolean set to True if the model needs to be saved
:return: X_train and X_test data scaled
:rtype: pd.DataFrame
"""
scaler = StandardScaler()
scaler.fit(X_train)
if save_model:
if self.verbose >= 2:
self.logger.info(f'''Saving scaler model to scaler_{self.algo}_estimator.pkl''')
model_path = f'''{get_path("models")}/scaler_{self.algo}_estimator.pkl'''
joblib.dump(scaler, model_path)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
return X_train_scaled, X_test_scaled