多元线性回归

import pandas as pd
from sklearn import model_selection
import statsmodels.api as sm

Profit = pd.read_excel('Predict to Profit.xlsx')
Profit.head()

train, test = model_selection.train_test_split(Profit, test_size = 0.2, random_state=1234)
model = sm.formula.ols('Profit~RD_Spend+Administration+Marketing_Spend+C(State)',data=train).fit()
print('模型的偏回归系数分别为:n', model.params)
test_X = test.drop(labels = 'Profit', axis=1)
pred = model.predict(exog = test_X)
print('对比预测值和实际值的差异:n',pd.DataFrame({'prediction':pred,'Real':test.Profit}))
model.summary()

首先将数据集分为训练集和测试集;
接着用sm进行多元回归;
计算预测值;
计算统计量。

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权,转载请注明出处。
文章名称:《多元线性回归》
文章来自:泰恩数据
文章链接:https://tyne.cc/834.html
本站资源仅供个人学习使用,请勿用于商业用途。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址