scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
except ValueError:
baseline = y_train.median()
#median
train
print('If we just take the median value, our baseline, we would say that an overnight stay in Brasov costs: ’ + str(baseline))
except AttributeError:
baseline_error = np.sqrt(mean_squared_error(y_pred=np.ones_like(y_test) * baseline, y_true=y_test))
print('And we will be right ± ’ + str(baseline_error))
lr = LinearRegression()
alphas = [1000, 100, 50, 20, 10, 1, 0.1, 0.01]
l1_ratios = [0.001, 0.01, 0.05, 0.1, 0.3, 0.5, 0.7, 0.9]
ridge = RidgeCV(alphas=alphas)
lasso = LassoCV(alphas=alphas, max_iter=10000)
elastic = ElasticNetCV(alphas=alphas, l1_ratio=l1_ratios)
for model, name in zip([lr, ridge, lasso, elastic], [‘LinearRegression’, ‘Ridge’, ‘Lasso’, ‘ElasticNet’]):
model.fit(X_train, y_train)
y_pred_train = model.predict(X_train_scaled)
mrse_train = np.sqrt(mean_squared_error(y_pred=y_pred_train, y_true=y_train))
y_pred = model.predict(X_test_scaled)
mrse_test = np.sqrt(mean_squared_error(y_pred=y_pred, y_true=y_test))
best_alpha = ‘’
if name != ‘LinearRegression’:
best_alpha = ’ best alpha: ’ + str(model.alpha_)
best_l1 = ‘’
if name == ‘ElasticNet’:
best_l1 = ’ best l1: '+ str(model.l1_ratio_)
print(name + ’ mrse_train: ’ + str(mrse_train) + ', mrse_test: ’ + str(mrse_test) + best_alpha + best_l1)
except ValueError:
y_pred_train = lasso.predict(X_train_scaled)
diff = y_train - y_pred_train
plt.figure(figsize=(15,8))
plt.scatter(np.arange(len(diff)), diff, label = ‘residuals’)
plt.legend()
NameError Traceback (most recent call last)
----> 1 y_pred_train = lasso.predict(X_train_scaled)
2 diff = y_train - y_pred_train
3 plt.figure(figsize=(15,8))
4 plt.scatter(np.arange(len(diff)), diff, label = ‘residuals’)
5 plt.legend()
NameError: name ‘X_train_scaled’ is not defined or Search Results OR
AttributeError: LassoCV object has no attribute 'coef_
Line 5 this is where you first assign to
X_train_scaled
. A
NameError
is getting thrown later on because it’s not seeing this name, likely because the assignment is getting skipped during
try-except
block starting in line 2. To use this name later, you can reassign it after the
except
, e.g.:
X_train_scaled = # something
except ValueError:
X_train_scaled = # something else
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
except ValueError:
baseline = y_train.median()
#median
train
print('If we just take the median value, our baseline, we would say that an overnight stay in Brasov costs: ’ + str(baseline))
except AttributeError:
baseline_error = np.sqrt(mean_squared_error(y_pred=np.ones_like(y_test) * baseline, y_true=y_test))
print('And we will be right ± ’ + str(baseline_error))
lr = LinearRegression()
alphas = [1000, 100, 50, 20, 10, 1, 0.1, 0.01]
l1_ratios = [0.001, 0.01, 0.05, 0.1, 0.3, 0.5, 0.7, 0.9]
ridge = RidgeCV(alphas=alphas)
lasso = LassoCV(alphas=alphas, max_iter=10000)
elastic = ElasticNetCV(alphas=alphas, l1_ratio=l1_ratios)
for model, name in zip([lr, ridge, lasso, elastic], [‘LinearRegression’, ‘Ridge’, ‘Lasso’, ‘ElasticNet’]):
model.fit(X_train, y_train)
y_pred_train = model.predict(X_train_scaled)
mrse_train = np.sqrt(mean_squared_error(y_pred=y_pred_train, y_true=y_train))
y_pred = model.predict(X_test_scaled)
mrse_test = np.sqrt(mean_squared_error(y_pred=y_pred, y_true=y_test))
best_alpha = ‘’
if name != ‘LinearRegression’:
best_alpha = ’ best alpha: ’ + str(model.alpha_)
best_l1 = ‘’
if name == ‘ElasticNet’:
best_l1 = ’ best l1: '+ str(model.l1_ratio_)
print(name + ’ mrse_train: ’ + str(mrse_train) + ', mrse_test: ’ + str(mrse_test) + best_alpha + best_l1)
except ValueError:
y_pred_train = lasso.predict(X_train_scaled)
diff = y_train - y_pred_train
plt.figure(figsize=(15,8))
plt.scatter(np.arange(len(diff)), diff, label = ‘residuals’)
plt.legend()
NameError Traceback (most recent call last)
----> 1 y_pred_train = lasso.predict(X_train_scaled)
2 diff = y_train - y_pred_train
3 plt.figure(figsize=(15,8))
4 plt.scatter(np.arange(len(diff)), diff, label = ‘residuals’)
5 plt.legend()
NameError: name ‘X_train_scaled’ is not defined or Search Results OR
AttributeError: LassoCV object has no attribute 'coef_
Hello,
I already trying linear regression in Python and Machine Learning with Error Analysis. and Your code had indentation and variable scope issues. I have been trying the correcting code here:
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression, RidgeCV, LassoCV, ElasticNetCV
from sklearn.metrics import mean_squared_error
import numpy as np
import matplotlib.pyplot as plt
X_train, X_test, y_train, y_test = train_test_split(X, y)
scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
except ValueError:
baseline = y_train.median()
print('Baseline overnight stay cost: ' + str(baseline))
except AttributeError:
baseline_error = np.sqrt(mean_squared_error(y_pred=np.ones_like(y_test) * baseline, y_true=y_test))
print('Baseline error: ±' + str(baseline_error))
lr, alphas, l1_ratios = LinearRegression(), [1000, 100, 50, 20, 10, 1, 0.1, 0.01], [0.001, 0.01, 0.05, 0.1, 0.3, 0.5, 0.7, 0.9]
ridge, lasso, elastic = RidgeCV(alphas=alphas), LassoCV(alphas=alphas, max_iter=10000), ElasticNetCV(alphas=alphas, l1_ratio=l1_ratios)
for model, name in zip([lr, ridge, lasso, elastic], ['LinearRegression', 'Ridge', 'Lasso', 'ElasticNet']):
model.fit(X_train_scaled, y_train)
y_pred_train, y_pred = model.predict(X_train_scaled), model.predict(X_test_scaled)
mrse_train, mrse_test = np.sqrt(mean_squared_error(y_pred=y_pred_train, y_true=y_train)), np.sqrt(mean_squared_error(y_pred=y_pred, y_true=y_test))
best_alpha, best_l1 = '', ''
if name != 'LinearRegression':
best_alpha = ' best alpha: ' + str(model.alpha_)
if name == 'ElasticNet':
best_l1 = ' best l1: ' + str(model.l1_ratio_)
print(name + ' mrse_train: ' + str(mrse_train) + ', mrse_test: ' + str(mrse_test) + best_alpha + best_l1)
except ValueError:
y_pred_train = lasso.predict(X_train_scaled)
diff = y_train - y_pred_train
plt.figure(figsize=(15, 8))
plt.scatter(np.arange(len(diff)), diff, label='residuals')
plt.legend()
plt.show()
You can also check this : Linear Regression in Python – Real Python machine learning
I hope this will help you.