邹博机器学习代码分析(1)-线性回归

1.导入各种需要的包

import csv
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from pprint import pprint
2.读入数据

path = 'Advertising.csv'
data = pd.read_csv(path)    # TVRadioNewspaperSales
# x = data[['TV', 'Radio', 'Newspaper']]
x = data[['TV', 'Radio']]
y = data['Sales']
print x
print y

3.训练模型

# 训练和测试 分开 train0.8 默认为75  25
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.8, random_state=1)
print type(x_test ) #DataFrame 类型
print x_train.shape, y_train.shape
linreg = LinearRegression() # 调用线性回归模型
model = linreg.fit(x_train, y_train) # 进行拟合
print model # 打印模型信息
# coef_ 对应Theta_1 ........;intercept_ 对应Theta_0
print linreg.coef_, linreg.intercept_
输出结果为:[ 0.04686997  0.1800065 ] 2.94751503603;表示Theta_0 = 2.94751503603
 4.进行预测 

# print y_test
order = y_test.argsort(axis=0) #order  y_test的值按大小排序的索引号 原来的index不变
# print 'order:',order
y_test = y_test.values[order] # 取出order中的索引号 作为y_test的排序方式
# print x_test
x_test = x_test.values[order, :] # x_test 的第一列按 order排序
# print x_test
y_hat = linreg.predict(x_test) # 对测试集进行预测

argsort 是按y_tset 的值从小到大排序, 返回每个值对应的index ,y_test自带的index不变.

5.计算MES和R2

mse = np.average((y_hat - np.array(y_test)) ** 2)  # Mean Squared Error
rmse = np.sqrt(mse)  # Root Mean Squared Error
print 'MSE = ', mse,
print 'RMSE = ', rmse
print 'R2 = ', linreg.score(x_train, y_train)
print 'R2 = ', linreg.score(x_test, y_test)
R^2 = 1-RSS/TSS 越大效果越好。1为最优值。用score计算R^2

6.画图

plt.figure(facecolor='w')
t = np.arange(len(x_test))
plt.plot(t, y_test, 'r-', linewidth=2, label=u'真实数据')
plt.plot(t, y_hat, 'g-', linewidth=2, label=u'预测数据')
plt.legend(loc='upper right')
plt.title(u'线性回归预测销量', fontsize=18)
plt.grid(b=True)
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值