I want to do a linear regression in Python and Machine Learning with Error Analysis. I have the following lines of code and the following errors

The code is:

X_train, X_test, y_train, y_test = train_test_split(X, y)
try:
scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
except ValueError:
pass
try:
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:
pass

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)

try:
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:
pass

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)
in
----> 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.:

try:
    X_train_scaled = # something
    ...
except ValueError:
    X_train_scaled = # something else
    ...