运行完整的代码,就可以看到所有的数据,包含了预测的数据在图表中展现出来,完整的代码如下:
#导入依赖库
import csv
import sys
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets,linear_model
#读取cvs文件的数据的函数
def read_data(file_name):
data=pd.read_csv(file_name)
#文件的数据模型
shandianxia_x_param=[]
shandianxia_y_param=[]
lvjianxia_x_param=[]
lvjianxia_y_param=[]
#数据填充
for x1,y1,x2,y2 in zip(data['sdx'],data['sdx_number'],data['ljx'],data['ljx_number']):
shandianxia_x_param.append([float(x1)])
shandianxia_y_param.append([float(y1)])
lvjianxia_x_param.append([float(x2)])
lvjianxia_y_param.append([float(y2)])
return shandianxia_x_param,shandianxia_y_param,lvjianxia_x_param,lvjianxia_y_param
#预测并比对
def compare_viewers(x1,y1,x2,y2):
reg1=linear_model.LinearRegression()
reg1.fit(x1,y1)
shandianxia_predicated_value=reg1.predict(10)
#将预测值加入到数据模型中,统一展现
x1.append([10])
y1.append([shandianxia_predicated_value])
plt.figure()
plt.title("Jiemu Number View")
plt.xlabel("Week")
plt.ylabel("Numbers")
plt.axis([0,10,0.0,10.0]) #x,y轴的范围值
plt.grid(True)
plt.plot(x1,y1,'r.')
reg2=linear_model.LinearRegression()
reg2.fit(x2,y2)
lvjianxia_predicated_value=reg2.predict(10)
x2.append([10])
y2.append([lvjianxia_predicated_value])
plt.plot(x2,y2,'g*')
plt.show()
if shandianxia_predicated_value>lvjianxia_predicated_value:
print("闪电侠在下一周会有更多观众")
else:
print("绿箭侠在下周会有更多观众")
#进行数据预测和比对
all_data=read_data('e:/jiemudata.csv')
compare_viewers(all_data[0],all_data[1],all_data[2],all_data[3])