添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
无邪的猴子  ·  过渡transition · FE-booklet·  5 天前    · 
读研的人字拖  ·  Java 读取 .properties ...·  3 天前    · 
阳刚的伏特加  ·  Postman _ API error ...·  2 天前    · 
博学的仙人掌  ·  How to hide a <div> ...·  19 小时前    · 
温柔的楼梯  ·  Slack Test connection ...·  11 小时前    · 
帅呆的毛衣  ·  Kapt issues with ...·  3 月前    · 
听话的牛腩  ·  ASP.NET Core MVC 项目 ...·  10 月前    · 
慈祥的紫菜汤  ·  The Hub’s Database — ...·  1 年前    · 
害羞的豆芽  ·  清欢序-🌈️包子漫畫·  1 年前    · 
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.