Hey! As you can see that i want to show you that “how you can create linear regression model on python”. Don’t worry about it, because it is very easy.

Don’t forget, linear regression is one of the basic statistical and machine learning techniques. Almost it is most important technique. There are many regression methods. But i want to mention just simple linear regression implementation. Linear regression just one of them.

Regression searches for relationships among variables. For example, salary and work experience. If your experience upper, your salary is can be good. But if you don’t have any experience, your salary is less. Is it simple, okay?

Salary vs Experience Visualization

First Step : It’s time to start import packages or libraries.

[python] 
import numpy as np
from sklearn.linear_model import LinearRegression
[/python]  

Second Step : Is your data ready?

[python] 
experience = np.array([0, 1, 2, 3, 4, 6, 7, 9, 12]).reshape((-1, 1))
salary = np.array([1000, 1300, 1500, 1550, 1700, 2500, 3000, 4000, 4500]) [/python]   

As you can see, experience has two dimensions, and experience.shape is (9, 1), while salary has a single dimension, and salary.shape is (9,).

Third Step : Create and fit Model.

[python]  
model = LinearRegression()
model.fit(x, y)
model = LinearRegression().fit(experience, salary)
[/python] 

Fourth Step : You can get results.

[python]
r_sq = model.score(x, y)
print('coefficient of determination:', r_sq)
print('intercept:', model.intercept_)
print('slope:', model.coef_)
[/python]

coefficient of determination: 0.9663587534392144
intercept: 818.5053380782913
slope: [310.98754448]

Fifth Step : You can predict.

[python]
y_pred = model.predict(experience)
print('predicted response:', y_pred, sep='\n')
[/python] 

predicted response: [ 818.50533808 1129.49288256 1440.48042705 1751.46797153 2062.45551601 2684.43060498 2995.41814947 3617.39323843 4550.35587189]

Step Six : You can learn logic.

[python] 
y_pred = model.intercept_ + model.coef_ * x
print('predicted response:', y_pred, sep='\n')
[/python] 

predicted response:
[[ 2373.4430605 ]
[ 5483.31850534]
[ 8593.19395018]
[11703.06939502]
[14812.94483986]
[17922.8202847 ]]

If you have any question, i will be happy to hear it. See you soon.