Simple Linear Regression Code
Simple Linear Regression Code
Shalini Gambhir
AIML-Fundamentals of Machine Learning
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
Splitting the dataset into the Training set and Test set
y_pred = regressor.predict(X_test)
plt.ylabel('Salary')
plt.show()
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
Making a single prediction (for example the salary of an employee with 12 years of experience)
print(regressor.predict([[12]]))
[138967.5015615]
Therefore, our model predicts that the salary of an employee with 12 years of experience is $
138967,5.
Notice that the value of the feature (12 years) was input in a double pair of square brackets. That's
because the "predict" method always expects a 2D array as the format of its inputs. And putting 12
into a double pair of square brackets makes the input exactly a 2D array. Simply put:
12→scalar
[12]→1D array
[[12]]→2D array
Getting the final linear regression equation with the values of the coefficients
print(regressor.coef_)
print(regressor.intercept_)
[9345.94244312]
26816.192244031183
Salary=9345.94×YearsExperience+26816.19
To get these coefficients we called the "coef_" and "intercept_" attributes from our regressor object.
Attributes in Python are different than methods and usually return a simple value or an array of
values.