Python线性回归预测形状误差

时间:2018-07-15 09:38:23

标签: python regression linear predict

我有1230个数据。我想做个预测。我什至无法预测前10行。我使用线性回归。当我转向多项式回归时,我犯了一个错误。我永远无法做出预测。多项式等级通常应为1230。即使这样,我也无法预测。我该怎么办?

  #import
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression

# Data Extraction
with open("a (1).txt","r+",encoding = "utf-8") as file:
    f=file.read().split("\n")
    f=f[::-1]
with open("a (2).txt","r+",encoding = "utf-8") as file:
    sonuc=file.read().split("\n")
    sonuc=sonuc[::-1]
for i in range(0,len(sonuc)):
    sonuc[i]=int(sonuc[i])
for i in range(0,len(f)):
    f[i]=int(f[i])


x=np.arange(1,len(sonuc)+1).reshape(len(sonuc),1)
y=np.array(sonuc).reshape(len(sonuc),1)
plt.plot(x,y,'ro')
plt.show()

model=LinearRegression()
model.fit(x,y)

model.coef_
np.array([1.77575758])

model.intercept_
np.array([4.93333333])

a=model.coef_ * x + model.intercept_

axes=plt.gca()
axes.set_ylim([0,100])


print(model.score(x,y))

x=np.c_[x,x**2]
print(x)

model.fit(x,y)
u=np.arange(1,len(sonuc)+1,1)
u=np.c_[u,u**2]
a=np.dot(x,model.coef_.transpose())+model.intercept_
plt.plot(x[:,0],y,'ro',x[:,0],a)
plt.show()

t=[]


x=np.arange(1,len(sonuc)+1)
x=np.c_[x**1,x**2,x**3,x**4,x**5,x**6,x**7,x**8,x**9]
s=[]


u=np.arange(1,len(sonuc)+1,1)
u=np.c_[u**1,u**2,u**3,u**4,u**5,u**6,u**7,u**8,u**9]
y=np.arange(1,len(sonuc)+1,1)
y=np.c_[y**1,y**2,y**3,y**4,y**5,y**6,y**7,y**8,y**9]
model.fit(x,y)
a=np.dot(u,model.coef_.transpose())+model.intercept_
plt.plot(x[:,0],y,'ro',u[:,0],a)
axes=plt.gca()
axes.set_ylim([0,50])
plt.show()

#print(model.predict(11))

a(1).txt:

10
9
8
7
6
5
4
3
2
1

a(2).txt

5
4
3
4
2
2
7
6
3
5

0 个答案:

没有答案