0% found this document useful (0 votes)
137 views

Simple - Linear - Regression - Ipynb - Colaboratory

This document discusses simple linear regression using a salary data set. It loads the data, splits it into training and testing sets, fits a linear regression model to the training set, makes predictions on the testing set, and calculates the covariance and Pearson's correlation between the actual and predicted values on the testing set, finding a very high correlation of 0.990.

Uploaded by

dharani v
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

Simple - Linear - Regression - Ipynb - Colaboratory

This document discusses simple linear regression using a salary data set. It loads the data, splits it into training and testing sets, fits a linear regression model to the training set, makes predictions on the testing set, and calculates the covariance and Pearson's correlation between the actual and predicted values on the testing set, finding a very high correlation of 0.990.

Uploaded by

dharani v
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

6/30/22, 2:08 PM simple_linear_regression.

ipynb - Colaboratory

Simple Linear Regression

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

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)

LinearRegression()

y_pred = regressor.predict(X_test)

[ 40835.10590871 123079.39940819 65134.55626083 63265.36777221

115602.64545369 108125.8914992 116537.23969801 64199.96201652

76349.68719258 100649.1375447 ]

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()

https://colab.research.google.com/drive/1FTA1PEwJX-omLT3WitBHjFEn17S6BCpZ#scrollTo=Rn_sTJ2o9smm&printMode=true 1/2
6/30/22, 2:08 PM simple_linear_regression.ipynb - Colaboratory

from numpy import cov

from scipy.stats import pearsonr

covariance = cov(y_test,y_pred)

corr,_ = pearsonr(y_test,y_pred)

print('covariance:', covariance)

print('Pearsons correlation: %.3f' % corr)

covariance: [[9.31340411e+08 8.67841681e+08]

[8.67841681e+08 8.25483981e+08]]
Pearsons correlation: 0.990

https://colab.research.google.com/drive/1FTA1PEwJX-omLT3WitBHjFEn17S6BCpZ#scrollTo=Rn_sTJ2o9smm&printMode=true 2/2

You might also like