在使用鲁棒定标器对标度数据进行模型拟合后,是否可以对LASSO回归中的截距和系数进行反变换?在
我用套索回归来预测数据的值,这些数据不是标准化的,除非事先对其进行了缩放,否则它在套索中的表现并不好。在缩放数据和拟合套索模型之后,我理想地希望能够看到模型的截距和系数是多少,但以原始单位(不是按比例缩放的版本)。我问了一个类似的问题here,但似乎这是不可能的。如果没有,为什么?有人能给我解释一下吗?我想扩大我对套索和强力刮片是如何工作的理解。在
下面是我使用的代码。在这里,我试图用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
所做的事情,同时也能以它们的未标度单位显示截距和系数。在
电流输出:
^{2}$理想输出:
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
基于链接的SO线程,您所要做的就是获得未缩放的预测值。对吗?在
如果是,那么您需要做的就是:
更新:
假设列车数据只包含一个名为
^{pr2}$X
的特性。下面是RobustScaler将要做的:然后,套索回归将给出如下预测:
您必须计算出方程式,以查看未缩放数据上的模型系数:
如果你试着用这个来做一个
a_new * x + b_new = y
式的等式,你会得到:您可以看到,未标度系数(
a_new
)依赖于X
。因此,您可以使用unscaledX
直接进行预测,但在这两者之间,您是间接地应用转换。在更新2
我已经修改了你的代码,现在它展示了如何获得原始比例的系数。这个脚本只是我上面展示的公式的实现。在
您可以看到自定义预测使用原始值(
X_test.values
)。在结果:
如上所述,新系数取决于
X_test
。这意味着您不能将它们的当前值用于另一个测试样本。对于不同的输入,它们的值将不同。在相关问题 更多 >
编程相关推荐