#!/usr/bin/env python # coding: utf-8 # In[14]: #Import the libraries and packages we need import numpy as np from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt # In[3]: #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]) # In[4]: print(x) print(y) # In[5]: print(x.shape) print(y.shape) # In[8]: #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) # In[11]: #now we can see the results #first our r-squared r_sq = model.score(x,y) print("r-squared: ",r_sq) # In[13]: #we can also find our intercept and slope intercept = model.intercept_ slope = model.coef_ print("intercept: ",intercept) print("slope: ",slope) # In[15]: #now we can find our predicted values y_predicted = model.predict(x) # In[16]: y_predicted # In[17]: #we can now create our regression equation y_predicted = intercept + slope*x # In[20]: #now we can plot the fig,ax = plt.subplots(figsize=(10,10)) plt.scatter(x,y) plt.plot(x,y_predicted) # In[ ]: