Reputation: 380
This is my code and the output below. I am trying it with sklearn-lib, witch works. Is maybe x.reshape false ?
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sympy.stats import E
#My Data
y = np.array([1.88,3.65,5.86,8.43,11.47,15.98])
x = np.array([1,2,3,4,5,6])
linreg = LinearRegression()
#Grafikgrösse einstellen
x = x.reshape(-1,1)
linreg.fit(x,y)
y_pred = linreg.predict(x)
plt.scatter(x,y)
plt.plot(x,y_pred, color="red")
plt.title("'Linearer Verlauf' durch t=0")
plt.xlabel("Anzahl Umdrehungen")
plt.ylabel("Periode(s)")
plt.legend(['t1'])
plt.show()
print("r^2 =",linreg.score(x,y))
print(linreg.coef_)
Upvotes: 1
Views: 643
Reputation: 860
y = a*x + b
Your a
is calculated by linreg.coef_
term and it has one value (since only one term dependent on x
. The +b
term is accessed by linreg.intercept_
, which gives -1.7746
.
Upvotes: 3