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

Simple Linear Regression

Uploaded by

Ajay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Simple Linear Regression

Uploaded by

Ajay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

import pandas as pd

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

data=pd.read_csv("sample_data/Salary_Data.csv")

data.head(10)

{"summary":"{\n \"name\": \"data\",\n \"rows\": 30,\n \"fields\":


[\n {\n \"column\": \"YearsExperience\",\n
\"properties\": {\n \"dtype\": \"number\",\n \"std\":
2.8378881576627184,\n \"min\": 1.1,\n \"max\": 10.5,\n
\"num_unique_values\": 28,\n \"samples\": [\n 3.9,\n
9.6,\n 3.7\n ],\n \"semantic_type\": \"\",\n
\"description\": \"\"\n }\n },\n {\n \"column\":
\"Salary\",\n \"properties\": {\n \"dtype\": \"number\",\n
\"std\": 27414,\n \"min\": 37731,\n \"max\": 122391,\n
\"num_unique_values\": 30,\n \"samples\": [\n 112635,\
n 67938,\n 113812\n ],\n
\"semantic_type\": \"\",\n \"description\": \"\"\n }\
n }\n ]\n}","type":"dataframe","variable_name":"data"}

from sklearn.model_selection import train_test_split


X = data.iloc[:, :-1].values
y = data.iloc[:, 1].values
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.3, random_state=0)

plt.scatter(X_train, y_train, color = 'red')


plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.title('Training Set')
plt.show()
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)

LinearRegression()

plt.scatter(X_test, y_test, color = 'red')


plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.title('Test Set')
plt.show()
from sklearn.metrics import r2_score
y_pred = regressor.predict(X_train)
r2_score(y_train, y_pred)

0.9423777652193379

y_pred = regressor.predict(X_test)
plt.scatter(X_test, y_test, color = 'red')
plt.plot(X_test, y_pred, color = 'blue')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.title('Test Set')
plt.show()
test_value = X_test[5]

predicted_salary = regressor.predict([test_value])

actual_salary = y_test[5]

print("Selected Years of Experience:", test_value[0])


print("Predicted Salary:", predicted_salary[0])
print("Actual Salary:", actual_salary)

Selected Years of Experience: 8.7


Predicted Salary: 108211.66453108242
Actual Salary: 109431

You might also like