我正在尝试使用 sklearn 使用 RFE 构建一个简单的线性回归模型。我选择了 49 个功能。当我检查系数时,我发现其中一些非常大,而另一些则为零。数据是能源需求时间序列,一些应该影响预测的特征的系数为零,如图所示。感谢您的帮助。
这是RFECV代码:
regr_cv = RFECV(estimator=regr,
cv=tscv,
scoring='neg_mean_squared_error',
verbose=2,
n_jobs=-1)
和拟合的结果
Pipeline(memory=None, steps=[('onehot', OneHotEncoder(categorical_features=[2, 3, 4], dtype=<class 'numpy.float64'>, handle_unknown='error', n_values='auto', sparse=False)), ('rfecv', RFECV(cv=TimeSeriesSplit(max_train_size=None, n_splits=3),
estimator=LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False), n_jobs=-1, scoring='neg_mean_squared_error', step=1, verbose=2))])
交叉验证结果为
cv_results = pd.DataFrame.from_dict({'cv_score':
regr_pipe.named_steps['rfecv'].grid_scores_})
cv_results['mean_squared_error'] = cv_results['cv_score']
plt.figure(figsize=(15, 5))
plt.plot(cv_results.index, cv_results['mean_squared_error'])
plt.xlabel('number of features')
plt.title('CV negative mean squared error')
plt.show()
交叉验证结果图(抱歉,Stackoveflow 不允许我在此处内联它们(
最后,根据其相应特征显示的系数
coefs = supported_features.loc[supported_features['supported'], ].copy()
coefs['coefficients'] = regr_pipe.named_steps['rfecv'].estimator_.coef_
coefs.plot.bar('feature', 'coefficients', figsize=(15, 3), legend=False)
plt.show()
特征与系数图
非常感谢
除了处理分类数据变量(多亏了@caseWestern(之外,通过在训练中添加能量数据信号自相关的一些特征,我得到了更好的结果。
具有自相关特征的训练数据表