gpt4 book ai didi

python - 更新 Python sklearn Lasso(normalize=True) 以使用管道

转载 作者:行者123 更新时间:2023-12-04 17:08:06 37 4
gpt4 key购买 nike

我是 Python 新手。我正在尝试通过使用此 CSV 进行 DataCamp 练习来练习基本正则化: https://assets.datacamp.com/production/repositories/628/datasets/a7e65287ebb197b1267b5042955f27502ec65f31/gm_2008_region.csv

# Import numpy and pandas
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Read the CSV file into a DataFrame: df
df = pd.read_csv('gm_2008_region.csv')

# Create arrays for features and target variable
X = df.drop(['life','Region'], axis=1)
y = df['life'].values.reshape(-1,1)
df_columns = df.drop(['life','Region'], axis=1).columns

我在 DataCamp 练习中使用的代码如下:

# Import Lasso
from sklearn.linear_model import Lasso

# Instantiate a lasso regressor: lasso
lasso = Lasso(alpha=0.4, normalize=True)

# Fit the regressor to the data
lasso.fit(X, y)

# Compute and print the coefficients
lasso_coef = lasso.coef_
print(lasso_coef)

# Plot the coefficients
plt.plot(range(len(df_columns)), lasso_coef)
plt.xticks(range(len(df_columns)), df_columns.values, rotation=60)
plt.margins(0.02)
plt.show()

Original

我得到上面的输出,表明 child_mortality 是预测预期生命周期的最重要特征,但由于使用了“normalize”,此代码也会导致弃用警告。

我想使用当前的最佳实践更新此代码。我尝试了以下方法,但得到了不同的输出。我希望有人可以帮助确定我需要在更新的代码中修改什么以产生相同的输出。

# Modified based on https://scikit-learn.org/stable/modules/preprocessing.html#preprocessing-scaler
# and https://stackoverflow.com/questions/28822756/getting-model-attributes-from-pipeline
# Import Lasso
from sklearn.linear_model import Lasso
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

# Instantiate a lasso regressor: lasso
#lasso = Lasso(alpha=0.4, normalize=True)
pipe = Pipeline(steps=[
('scaler',StandardScaler()),
('lasso',Lasso(alpha=0.4))
])

# Fit the regressor to the data
#lasso.fit(X, y)
pipe.fit(X, y)

# Compute and print the coefficients
#lasso_coef = lasso.coef_
#print(lasso_coef)
lasso_coef = pipe.named_steps['lasso'].coef_
print(lasso_coef)

# Plot the coefficients
plt.plot(range(len(df_columns)), lasso_coef)
plt.xticks(range(len(df_columns)), df_columns.values, rotation=60)
plt.margins(0.02)
plt.show()

Updated

如您所见,我得出了相同的结论,但如果输出图像更相似,我会更自在地正确执行此操作。我对管道做错了什么?

最佳答案

当您设置 Lasso(..normalize=True) 时,规范化不同于 StandardScaler() 中的规范化。它除以 l2 范数而不是标准差。如果您阅读 help page :

normalize bool, default=False This parameter is ignored whenfit_intercept is set to False. If True, the regressors X will benormalized before regression by subtracting the mean and dividing bythe l2-norm. If you wish to standardize, please use StandardScalerbefore calling fit on an estimator with normalize=False.

Deprecated since version 1.0: normalize was deprecated in version 1.0and will be removed in 1.2.

post 中也提到了这一点.由于它将被弃用,我认为最好只使用 StandardScaler 规范化。只要您以相同的方式缩放它,您就可以看到它是可重现的:

lasso = Lasso(alpha=0.4,random_state=99)
lasso.fit(StandardScaler().fit_transform(X),y)
print(lasso.coef_)

[-0. -0.30409556 -2.33203165 -0. 0.51040194 1.45942351
-1.02516505 -4.57678764]

pipe = Pipeline(steps=[
('scaler',StandardScaler()),
('lasso',Lasso(alpha=0.4,random_state=99))
])

pipe.fit(X, y)
lasso_coef = pipe.named_steps['lasso'].coef_
print(lasso_coef)

[-0. -0.30409556 -2.33203165 -0. 0.51040194 1.45942351
-1.02516505 -4.57678764]

关于python - 更新 Python sklearn Lasso(normalize=True) 以使用管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70085731/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com