使用 SciPy 提供的普通最小二乘法分别计算 3 个特征与目标之间的一元线性回归模型拟合参数。 加载数据集 import pandas as pd data = pd.read_csv( ‘http://labfile.oss.aliyuncs.com/courses/1211/Advertising.csv’, index_col=0) data.head() 数据集包含 4 列,共 200 行。每个样本代表某超市销售相应单位件商品所需要支出的广告费用。以第一行为例,表示该超市平均销售 22.1 件商品,需要支出的电视广告费用,广播广告费用以及报刊广告费用为:230.1 美元,37.8 美元和 69.2 美元。将前 3 列视作特征,最后一列视作目标值。 计算拟合参数 import numpy as np from scipy.optimize import leastsq ### 代码开始 ### (≈ 10 行代码) m=2 def func(w, x): return w[0]+w[1]*x def err_func(w, x, y): return y-func(w, x) params_tv = leastsq(err_func,[1, 1], args=(data[‘tv’], data[‘sales’])) params_radio = leastsq(err_func,[1, 1], args=(data[‘radio’], data[‘sales’])) params_newspaper = leastsq(err_func,[1, 1], args=(data[‘newspaper’], data[‘sales’])) ### 代码结束 ### 输出: (array([7.03259358, 0.04753664]), array([9.3116381 , 0.20249578]), array([12.35140707, 0.0546931 ])) 拟合结果绘图 from matplotlib import pyplot as plt %matplotlib inline ### 代码开始 ### (≈ 10 行代码) fig, axes=plt.subplots(1, 3, figsize=(20,5)) axes[0].scatter(data[‘tv’], data[‘sales’]) axes[0].plot(data[‘tv’], func(params_tv[0], data[‘tv’]), color=’red’) axes[0].set_xlabel(‘tv’) axes[0].set_ylabel(‘sales’) axes[1].scatter(data[‘radio’], data[‘sales’]) axes[1].plot(data[‘radio’], func(params_radio[0], data[‘radio’]), color=’red’) axes[1].set_xlabel(‘radio’) axes[1].set_ylabel(‘sales’) axes[2].scatter(data[‘newspaper’], data[‘sales’]) axes[2].plot(data[‘newspaper’], func(params_newspaper[0], data[‘newspaper’]), color=’red’) axes[2].set_xlabel(‘newspaper’) axes[2].set_ylabel(‘sales’) ### 代码结束 ### (1)请给出拟合结果绘图(10分) 多元线性回归模型 使用 scikit-learn 提供的线性回归方法建立由 3 个特征与目标组成的多元线性回归模型。 计算拟合参数 from sklearn.linear_model import LinearRegression ### 代码开始 ### (≈ 4 行代码) model = LinearRegression() model.fit(data[[‘tv’, ‘radio’, ‘newspaper’]], data[‘sales’]) ### 代码结束 ### model.coef_, model.intercept_ # 返回模型自变量系数和截距项 输出: (array([ 0.04576465, 0.18853002, -0.00103749]), 2.9388893694594103) 模型检验 使用 statsmodels 库提供的相关方法来完成上面多元回归模型的拟合优度检验和变量显著性检验。 import statsmodels.api as sm x=sm.add_constant(data[[‘tv’, ‘radio’, ‘newspaper’]]) model=sm.OLS(endog=data[‘sales’], exog=x) results=model.fit() ### 代码结束 ### results.summary2() # 输出模型摘要 (2)给出模型输出结果(10分) (3)对上述实验结果进行分析(10分) 江苏开放大学 391 问题反馈 反馈回复 推广有佣金 内容查看查看价格2 元VIP免费升级VIP立即购买申请退款 点点赞赏,手留余香 给TA打赏 AI创作 页内查找 0 猜你喜欢 茶文化在中国有着悠久的历史。相传,中国古代的神农氏最早发现了茶的药用价值。从此,茶逐渐成为中国人生活中不可或缺的一部分。无论是文人墨客还是普通百姓,都喜欢通过品茶来修身养性。如今,茶不仅是一种饮品,更是中国文化的象征之一。 2天前 2026年秋江苏开放大学习近平新时代中国特色社会主义思想概论060849实践作业:弘扬伟大长征精神,走好新时代长征路 3周前 China’s online literature and short-video sectors have seen explosive growth in recent years, with platforms hosting over 20 million original literary works and 500 million daily active users for short-video apps. To ensure healthy development, regulators have introduced guidelines to curb content and protect intellectual property rights. Content creators are now encouraged to produce works that promote traditional culture and positive values, such as historical stories and scientific popularization. Live-streaming e-commerce, another booming sector, has integrated with rural revitalization, helping farmers sell local products to national and global markets through real-time interactions. 2天前 2026年秋江苏开放大学计算机应用基础060019第二次形成作业(Word文档编辑与排版实践) 2026-09-14 部分要求在 30 分钟内,根据下面的内容用英语写一份申请信(词数 80-200 词)。 1周前 Part VI Writing (写作题。根据题目与写作要求写作。请不要用附件或者图片上传作文!禁止雷同作文!禁止直接用AI写作!) Directions: For this part, you are allowed 30 minutes to write a short essay on ensuring security in mobile payment according to the given Chinese outline. You should write at least 150 words but no more than 180 words. 3天前 评论0 请先 登录
评论0