拟合线性模型
现在我们来拟合一个线性模型,因为它简单且易于理解。模型拟合完成后,您可以查看哪些自变量与目标值存在线性相关关系,以及它们对目标的影响大小。我们判断自变量是否显著,依据是系数的 p 值。这相当于使用 t 检验来统计检验系数是否显著偏离 0。p 值表示某个特征对应的系数不偏离 0 的概率(以百分比理解)。通常,当 p 值小于 0.05 时,我们认为该系数与 0 显著不同。
本练习是课程的一部分
Python 金融机器学习
练习说明
- 拟合线性模型(使用
.fit()方法),并将结果保存到变量results。 - 使用
.summary()函数打印结果摘要。 - 打印结果的 p 值(
results的.pvalues属性)。 - 使用
results对象的.predict()函数,对train_features和test_features进行预测。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create the linear model and complete the least squares fit
model = sm.OLS(train_targets, train_features)
results = model.____ # fit the model
print(results.____)
# examine pvalues
# Features with p <= 0.05 are typically considered significantly different from 0
print(results.____)
# Make predictions from our model for train and test sets
train_predictions = results.predict(train_features)
test_predictions = ____