【发布时间】:2019-08-26 16:25:48
【问题描述】:
在使用 Robust Scaler 在缩放数据上拟合模型后,是否可以对 LASSO 回归中的截距和系数进行逆变换?
我正在使用 LASSO 回归来预测未标准化的数据值,并且除非事先进行缩放,否则 LASSO 的性能不佳。在缩放数据并拟合 LASSO 模型之后,理想情况下,我希望能够看到模型截距和系数是什么,但以原始单位(不是缩放版本)。我问了一个类似的问题here,看来这是不可能的。如果不是,为什么?谁可以给我解释一下这个?我正在努力扩大我对 LASSO 和 Robust Scaler 工作原理的理解。
下面是我使用的代码。在这里,我尝试使用transformer_x对系数进行逆变换,使用transformer_y对截距进行逆变换。但是,这听起来是不正确的。
import pandas as pd
from sklearn.preprocessing import RobustScaler
from sklearn.linear_model import Lasso
df = pd.DataFrame({'Y':[5, -10, 10, .5, 2.5, 15], 'X1':[1., -2., 2., .1, .5, 3], 'X2':[1, 1, 2, 1, 1, 1],
'X3':[6, 6, 6, 5, 6, 4], 'X4':[6, 5, 4, 3, 2, 1]})
X = df[['X1','X2', 'X3' ,'X4']]
y = df[['Y']]
#Scaling
transformer_x = RobustScaler().fit(X)
transformer_y = RobustScaler().fit(y)
X_scal = transformer_x.transform(X)
y_scal = transformer_y.transform(y)
#LASSO
lasso = Lasso()
lasso = lasso.fit(X_scal, y_scal)
def pred_val(X1,X2,X3,X4):
print('X1 entered: ', X1)
#Scale X value that user entered - by hand
med_X = X.median()
Q1_X = X.quantile(0.25)
Q3_X = X.quantile(0.75)
IQR_X = Q3_X - Q1_X
X_scaled = (X1 - med_X)/IQR_X
print('X1 scaled by hand: ', X_scaled[0].round(2))
#Scale X value that user entered - by function
X_scaled2 = transformer_x.transform(np.array([[X1,X2]]))
print('X1 scaled by function: ', X_scaled2[0][0].round(2))
#Intercept by hand
med_y = y.median()
Q1_y = y.quantile(0.25)
Q3_y = y.quantile(0.75)
IQR_y = Q3_y - Q1_y
inv_int = med_y + IQR_y*lasso.intercept_[0]
#Intercept by function
inv_int2 = transformer_y.inverse_transform(lasso.intercept_.reshape(-1, 1))[0][0]
#Coefficient by hand
inv_coef = lasso.coef_[0]*IQR_y
#Coefficient by function
inv_coef2 = transformer_x.inverse_transform(reg.coef_.reshape(1,-1))[0]
#Prediction by hand
preds = inv_int + inv_coef*X_scaled[0]
#Prediction by function
preds_inner = lasso.predict(X_scaled2)
preds_f = transformer_y.inverse_transform(preds_inner.reshape(-1, 1))[0][0]
print('\nIntercept by hand: ', inv_int[0].round(2))
print('Intercept by function: ', inv_int2.round(2))
print('\nCoefficients by hand: ', inv_coef[0].round(2))
print('Coefficients by function: ', inv_coef2[0].round(2))
print('\nYour predicted value by hand is: ', preds[0].round(2))
print('Your predicted value by function is: ', preds_f.round(2))
print('Perfect Prediction would be 80')
pred_val(10,1,1,1)
更新:我更新了我的代码以显示我正在尝试创建的预测函数的类型。我只是想创建一个完全符合.predict 的函数,而且还以未缩放的单位显示截距和系数。
当前输出:
Out[1]:
X1 entered: 10
X1 scaled by hand: 5.97
X1 scaled by function: 5.97
Intercept by hand: 34.19
Intercept by function: 34.19
Coefficients by hand: 7.6
Coefficients by function: 8.5
Your predicted value by hand is: 79.54
Your predicted value by function is: 79.54
Perfect Prediction would be 80
理想输出:
Out[1]:
X1 entered: 10
X1 scaled by hand: 5.97
X1 scaled by function: 5.97
Intercept by hand: 34.19
Intercept by function: 34.19
Coefficients by hand: 7.6
Coefficients by function: 7.6
Your predicted value by hand is: 79.54
Your predicted value by function is: 79.54
Perfect Prediction would be 80
【问题讨论】:
-
澄清一下:你想要原始单位中的系数和截距,还是想要原始单位中的预测(如 Stelgos 的回答)?
-
我想要原始单位的系数和截距
-
例如,如果您有以美元为单位的预测值,而 X 值以 [yr, m^2, USD] 为单位(例如),您需要以 USD/yr、USD/m^2 为单位的系数和纯数字,截取美元?
-
@Itamar Mushkin 对不起,我不太明白你的问题。我想要原始单位的系数,是的。
标签: python machine-learning lasso-regression