开始使用免费开始使用

回归中的拟合与预测

现在您已经了解了线性回归的工作原理,您的任务是使用 sales_df 数据集中的所有特征创建一个多元线性回归模型。该数据集已为您预加载。作为提示,前两行如下:

     tv        radio      social_media    sales
1    13000.0   9237.76    2409.57         46677.90
2    41000.0   15886.45   2913.41         150177.83

随后,您将使用该模型根据测试特征的取值来预测销售额。

LinearRegressiontrain_test_split 已分别从其模块中为您预加载。

本练习是课程的一部分

使用 scikit-learn 的监督学习

查看课程

练习说明

  • 创建 X,即包含 sales_df 中所有特征取值的数组;以及 y,即 "sales" 列中的所有取值。
  • 实例化一个线性回归模型。
  • 将模型拟合到训练数据。
  • 创建 y_pred,基于测试特征对 sales 进行预测。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create X and y arrays
X = sales_df.____("____", axis=____).____
y = sales_df["____"].____

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Instantiate the model
reg = ____

# Fit the model to the data
____

# Make predictions
y_pred = reg.____(____)
print("Predictions: {}, Actual Values: {}".format(y_pred[:2], y_test[:2]))
编辑并运行代码