python 线性回归数据分析画图小练习

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# 读取数据
data = pd.read_csv('Advertising.csv')

# 显示数据头
# print(data.head())

x = data[['TV','Radio','Newspaper']]
y = data['Sales']
"""

plt.figure(figsize=(9,12))
plt.subplot(311)
plt.plot(data['TV'], y, 'ro')
plt.title('TV')
plt.grid()
plt.subplot(312)
plt.plot(data['Radio'], y, 'g^')
plt.title('Radio')
plt.grid()
plt.subplot(313)
plt.plot(data['Newspaper'], y, 'b*')
plt.title('Newspaper')
plt.grid()
plt.tight_layout()
plt.show()
"""
seed = 1  # 随机种子
validation = 0.25      # 测试与训练比

X_train, X_test,y_train, y_test = train_test_split(x,y,random_state=1,test_size=validation)
linreg = LinearRegression()
model = linreg.fit(X_train, y_train)
#  print(linreg.intercept_)       常数b
#  print(linreg.coef_)            系数w

y_pred = linreg.predict(X_test)

# 画图比较预测结果
plt.figure()
plt.plot(range(len(y_pred)),y_pred,'b',label="predict")
plt.plot(range(len(y_pred)),y_test,'r',label='test')
plt.legend(loc='upper right')          # 右上角显示标签
plt.xlabel('the number of sales')
plt.ylabel('value of sales')
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值