Linear Regression in 3 Steps

1- Import your packages


In [1]:
import numpy as nb
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

2- Create your data


In [2]:
cigarettes = nb.array([[1], [30], [60]])
hours = nb.array([[7], [210], [420]])

plt.scatter(cigarettes, hours)
plt.show()


3- Modeling Data


In [3]:
model = LinearRegression()
model.fit(cigarettes, hours)


Out[3]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

Results


In [4]:
model.predict(80)


Out[4]:
array([[ 560.]])

In [5]:
cigarettes_test = nb.linspace(1,80)
hours_pred = model.predict(cigarettes_test[:,None])

plt.scatter(cigarettes, hours)
plt.plot(cigarettes_test, hours_pred,'r')
plt.legend(['Predicted line','Observed data'])
plt.show()