In the last post by Atharva Peshkar on Linear Regression, you learned what is linear regression. Now let’s see how to implement Linear regression in One Variable.
Prerequisites:
- Basic Knowledge of Python
- Python Software
Instead of python software, I recommend you to use Spyder tool by Anaconda which is also an open source software. Click here to download Anaconda’s Spyder.
Let’s Begin.
As you know a Linear Regression program undergoes the following steps:
- Importing the dataset
- Dividing Training and Test Data’
- Creating a Simple Linear Regression Model
- Predicting the Test Results
- Plotting the Results (Optional)
Let’s start. Before importing the data set, we need to install and import some basic python libraries by executing the following code in python:
import matplotlib.pyplot as plt
import pandas as pd
After importing these libraries, we will import the data set. For this download the data set from this link to the same folder where you have saved the python file.
Download Dataset: http://bit.do/datasetlinear
Import data set in your python file by code:
dataset= pd.read_csv(‘Salary_Data.csv’)
X= dataset.iloc[:,:-1].values
Y= dataset.iloc[:,1].values
Now, it’s time to split the dataset into training data and testing data. Implement and learn the following code for that:
from sklearn.cross_validation import train_test_split
X_train, X_test, Y_train, Y_test= train_test_split(X,Y,test_size= 1/3)
Now, it’s time to execute the most important part of the program. That is, Linear regression model.
from sklearn.linear_model import LinearRegression
regressor= LinearRegression()
regressor.fit(X_train,Y_train)
Finally, testing the results using this model.
Y_pred= regressor.predict(X_test)
Look at Variable Explorer tab in your python window. You will find the Y_pred values as the output of test data.
You can plot these values using the plotter as follow:
plt.scatter(X_train, Y_train, color=’red’)
plt.plot(X_train, regressor.predict(X_train), color=’blue’)
plt/title(‘Salary Vs Experience(Training set)’)
plt.xlabel(‘Years of experience’)
plt.ylabel(‘Salary’)
plt.show()
plt.scatter(X_test, Y_test, color=’red’)
plt.plot(X_test, regressor.predict(X_test), color=’blue’)
plt/title(‘Salary Vs Experience(Training set)’)
plt.xlabel(‘Years of experience’)
plt.ylabel(‘Salary’)
plt.show()
Now run the code all together and visualise the graphs.
Hope you understood the concept of Linear Regression.
If you have any query, please feel free to use Contact us tab.