Skip to content

Instantly share code, notes, and snippets.

@srajan-jha srajan-jha/Linear_Regression.py Secret
Created Sep 14, 2018

Embed
What would you like to do?
"""
Created on Sat Aug 18 15:17:48 2018
@author: srajan23
"""
# importing the libraries
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# importing data set
dataset = pd.read_csv('../input/Height_Weight_single_variable_data_101_series_1.0.csv')
# checking for null values if any
dataset.isnull().any()
# Splitting the dataset into test set and training set
X_train, X_test, y_train, y_test = train_test_split(dataset.iloc[:, 1:],
dataset.iloc[:, 0:1],
test_size=1/3,random_state = 0)
# Fitting the Model
regressor = LinearRegression()
regressor.fit(X_train,y_train)
# Calculating R square score
print('R square = ',regressor.score(X_train,y_train))
# Predicting Height
y_pred = regressor.predict(X_test)
# Visualising the Training set
plt.scatter(X_train,y_train,color='blue')
plt.plot(X_train, regressor.predict(X_train),color='red')
plt.title('Training Set')
plt.xlabel('Height')
plt.ylabel('Weight')
plt.show()
# Visualising the test set and plotting the regression line predicted by our model
plt.scatter(X_test,y_test,color='blue')
plt.plot(X_test, regressor.predict(X_test),color='red')
plt.title('Test Set')
plt.xlabel('Height')
plt.ylabel('Weight')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.