Skip to content

Instantly share code, notes, and snippets.

@debonx
Created November 12, 2018 10:00
Show Gist options
  • Save debonx/1b6500bc09964deedbcdec882e03f6b1 to your computer and use it in GitHub Desktop.
Save debonx/1b6500bc09964deedbcdec882e03f6b1 to your computer and use it in GitHub Desktop.
Basic Example to work with Scikit-Learn for calculating Linear Regressions. More at https://scikit-learn.org/
#Import LinearRegression methods from Scikit-Learn
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import numpy as np
#Data of Soup sales with their temperature
temperature = np.array(range(60, 100, 2))
temperature = temperature.reshape(-1, 1)
sales = [65, 58, 46, 45, 44, 42, 40, 40, 36, 38, 38, 28, 30, 22, 27, 25, 25, 20, 15, 5]
plt.plot(temperature, sales, 'o')
plt.show()
#Call the class adn fitting with data
line_fitter = LinearRegression()
line_fitter.fit(temperature, sales)
#Predict sales on temperature
sales_predict = line_fitter.predict(temperature)
#Show Linear Regression
plt.plot(temperature, sales_predict, 'o')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment