Simple Linear Regression in Python – Step 3.) Training the Simple Linear Regression Model

with No Comments

Once we have the data ready and separated in training set and testing set, we can move on to training a simple linear regression model.

In python it is very simple to create a linear regression model.

We are going to use the scikit-learn library, which comes with linear regression function.

Fitting and Training the Simple Linear Regression Model

#Import Libraries

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#Import data
dataset = pd.read_csv(‘Data.csv’)
x = dataset.iloc[:,:-1].values
y =dataset.iloc[:,1].values

#Splitting training set and testing set
from sklearn.cross_validation import train_test_split
xtrain, xtest, ytrain, ytest =train_test_split(x,y,test_size=0.25)

#Training and Fitting model
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(xtrain, ytrain)

 

Using these codes, we have created a variable name ‘regressor’, which learnt the mathematical relationship of xtrain and ytrain.

Now we can use the values stored in ‘regressor’ to predict the result of testing set.

 

 

 

Leave a Reply