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()
python 线性回归数据分析画图小练习
最新推荐文章于 2024-02-26 10:31:04 发布