0% found this document useful (0 votes)
57 views2 pages

Linear Regression Salary Prediction

This document contains code for a linear regression model to predict salary based on years of experience using scikit-learn. It loads salary data from a CSV file, fits a linear regression model to predict salary for a given years of experience value, and displays the intercept, coefficient, and predicted salary. It also shows a scatter plot of the data with the linear regression line.

Uploaded by

Vaibhav Singh
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)
57 views2 pages

Linear Regression Salary Prediction

This document contains code for a linear regression model to predict salary based on years of experience using scikit-learn. It loads salary data from a CSV file, fits a linear regression model to predict salary for a given years of experience value, and displays the intercept, coefficient, and predicted salary. It also shows a scatter plot of the data with the linear regression line.

Uploaded by

Vaibhav Singh
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

Assignment 1

-Submitted by: Vaibhav Singh


(CSC)
(14B00033)

Task: Linear Regression for predicting salary for a given random test
sample
Code:

import [Link] as plt


import pandas as pd
from sklearn import datasets, linear_model

def get_data(filename):
data = pd.read_csv(filename)
x = []
y = []
for yearExperience, salary in zip(data['YearsExperience'], data['Salary']):
[Link]([float(yearExperience)])
[Link](float(salary))
return x, y

def print_data(filename):
data = pd.read_csv(filename)
return data

print(print_data('Salary_Data.csv'))

def linear_regression_model(x, y, predict_value):


regression = linear_model.LinearRegression()
[Link](x, y)
predict_output = [Link](predict_value)
predictions = {}
predictions['intercept'] = regression.intercept_
predictions['coefficient'] = regression.coef_
predictions['predicted_value'] = predict_output
return predictions

x, y = get_data('Salary_Data.csv')
predict_value = 2.03
res = linear_regression_model(x, y, predict_value)
print("Intercept value ", res['intercept'])
print("coefficient ", res['coefficient'])
print("Predicted value: ", res['predicted_value'])

def show_graph(x, y):


regression = linear_model.LinearRegression()
[Link](x, y)
[Link](x, y, color='blue')
[Link](x, [Link](x), color='black', linewidth=1)
[Link](())
[Link](())
[Link]()

show_graph(x, y)

Graph:

You might also like