Created
April 28, 2021 22:25
-
-
Save StrikingLoo/9f01647b0babd1bce7f1c2b0c320c946 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.metrics import mean_squared_error | |
from sklearn.model_selection import train_test_split | |
from sklearn.linear_model import Lasso, Ridge, LinearRegression,ElasticNet | |
from sklearn.model_selection import cross_val_score | |
def train_eval_model(train_housing_prepared, train_labels, strategy, alpha = None, l1_ratio = None): | |
if alpha != None: | |
print('alpha: ', alpha) | |
if l1_ratio != None: | |
print('l1_ratio: ', l1_ratio) | |
lin_reg = strategy(alpha = alpha, l1_ratio = l1_ratio, max_iter=100000) #LinearRegression() | |
else: | |
lin_reg = strategy(alpha = alpha, max_iter=50000) | |
else: | |
lin_reg = strategy() | |
lin_reg.fit(train_housing_prepared, train_labels) | |
#print('coefs: ', lin_reg.coef_) | |
housing_predictions = lin_reg.predict(train_housing_prepared) | |
train_mse = mean_squared_error(housing_predictions, train_labels) | |
train_rmse = np.sqrt(train_mse) | |
print('train rmse: ', train_rmse) | |
test_housing_predictions = lin_reg.predict(test_housing_prepared) | |
test_mse = mean_squared_error(test_housing_predictions, test_labels) | |
test_rmse = np.sqrt(test_mse) | |
print('test rmse: ', test_rmse) | |
return test_rmse | |
train_set, test_set = train_test_split(housing.loc[housing_labels_filtered.index], test_size=0.2, random_state=42) | |
train_housing_prepared = full_pipeline.fit_transform(train_set) | |
test_housing_prepared = full_pipeline.transform(test_set) | |
train_labels = train_set['median_house_value'] | |
test_labels = test_set['median_house_value'] | |
cute_results = '***** \n' | |
for strategy, strategy_name in [(Lasso, 'Lasso'), (Ridge, 'Ridge')]: | |
print('---*---*---') | |
print(strategy_name) | |
min_test_rmse = float('inf') | |
results = '' | |
for alpha in [.1, .5, 1, 5, 10, 50, 100, 500, 1000, 5000, 10000, 15000]: | |
new_test_rmse = train_eval_model(train_housing_prepared, train_labels, strategy, alpha) | |
if new_test_rmse < min_test_rmse : | |
min_test_rmse = new_test_rmse | |
results = str([strategy_name, ': rmse: ' , min_test_rmse, 'alpha: ', alpha]) | |
cute_results+=results | |
cute_results+='\n' | |
print('---*---*---') | |
for strategy, strategy_name in [(ElasticNet, 'ElasticNet')]: | |
print(strategy_name) | |
results = '' | |
min_test_rmse = float('inf') | |
for alpha in [.1, .5, 1, 5, 10, 50, 100, 500, 1000, 5000, 10000, 15000]: | |
for l1_ratio in [.1, .25, .5, .75, .9]: | |
new_test_rmse = train_eval_model(train_housing_prepared, train_labels, strategy, alpha, l1_ratio) | |
if new_test_rmse < min_test_rmse : | |
min_test_rmse = new_test_rmse | |
results = str([strategy_name, ': rmse: ' , min_test_rmse, 'alpha: ', alpha, 'l1_ratio:', l1_ratio]) | |
cute_results+=results | |
cute_results+='\n' | |
print('---*---*---') | |
results = train_eval_model(train_housing_prepared, train_labels, LinearRegression) | |
cute_results += str(['linearRegression: rmse: ', results]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment