#Import the libraries and packages we need
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
#we are going to create two arrays that have our data. These should be the same length
x = np.array([10,40,25,30,55,18,7]).reshape(-1,1) #we have to use reshape because we are required to pass in x
#as a 2-dimensional array. One column and as many rows as necessary
y = np.array([5,18,22,45,60,34,19])
print(x)
print(y)
[[10] [40] [25] [30] [55] [18] [ 7]] [ 5 18 22 45 60 34 19]
print(x.shape)
print(y.shape)
(7, 1) (7,)
#we now create our model instance. We are just going to use the default parameters
#we also are going to fit the model
#this calculates the values for the slope and intercept in our regression equation
model = LinearRegression().fit(x,y)
#now we can see the results
#first our r-squared
r_sq = model.score(x,y)
print("r-squared: ",r_sq)
r-squared: 0.5034536744482078
#we can also find our intercept and slope
intercept = model.intercept_
slope = model.coef_
print("intercept: ",intercept)
print("slope: ",slope)
intercept: 8.420731707317064 slope: [0.77867502]
#now we can find our predicted values
y_predicted = model.predict(x)
y_predicted
array([16.20748187, 39.56773237, 27.88760712, 31.7809822 , 51.24785761, 22.436882 , 13.87145682])
#we can now create our regression equation
y_predicted = intercept + slope*x
#now we can plot the
fig,ax = plt.subplots(figsize=(10,10))
plt.scatter(x,y)
plt.plot(x,y_predicted)
[<matplotlib.lines.Line2D at 0x1114dcd0>]