Skip to content

Commit

Permalink
Merge pull request #88 from Aditi22Bansal/main
Browse files Browse the repository at this point in the history
Adding Linear Regression Model- Solved issue #86
  • Loading branch information
sanjay-kv authored Jun 1, 2024
2 parents 6ccf340 + b2ea46f commit 3471233
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Linear Regression Model/Salary_dataset.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
,YearsExperience,Salary
0,1.2000000000000002,39344.0
1,1.4000000000000001,46206.0
2,1.6,37732.0
3,2.1,43526.0
4,2.3000000000000003,39892.0
5,3.0,56643.0
6,3.1,60151.0
7,3.3000000000000003,54446.0
8,3.3000000000000003,64446.0
9,3.8000000000000003,57190.0
10,4.0,63219.0
11,4.1,55795.0
12,4.1,56958.0
13,4.199999999999999,57082.0
14,4.6,61112.0
15,5.0,67939.0
16,5.199999999999999,66030.0
17,5.3999999999999995,83089.0
18,6.0,81364.0
19,6.1,93941.0
20,6.8999999999999995,91739.0
21,7.199999999999999,98274.0
22,8.0,101303.0
23,8.299999999999999,113813.0
24,8.799999999999999,109432.0
25,9.1,105583.0
26,9.6,116970.0
27,9.7,112636.0
28,10.4,122392.0
29,10.6,121873.0
41 changes: 41 additions & 0 deletions Linear Regression Model/linear_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

df=pd.read_csv("/kaggle/input/salary-dataset-simple-linear-regression/Salary_dataset.csv")

plt.scatter(df['YearsExperience'],df['Salary'])
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot")
plt.show()

X=df[['YearsExperience']]
y=df['Salary']

model=LinearRegression()
model.fit(X,y) #calculates best fitting relation between x and y
slope = model.coef_[0] # represents coeff of x in y = mx + b
intercept = model.intercept_ # point where regression lines crosses y axis

def linear_regression(x):
return slope * x + intercept

plt.scatter(df['YearsExperience'],df['Salary'], label='Data Points')
plt.plot(df['YearsExperience'], [linear_regression(xi) for xi in df['YearsExperience']], color='red')
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.title("Linear Regression")
plt.show()

new_x = 6
prediction = linear_regression(new_x)
print(f"Prediction for x = {new_x}: {prediction}")

new_x = pd.DataFrame({"YearsExperience": [6]})
print(model.predict(new_x))


new_x = pd.DataFrame({'YearsExperience': [0]})
print(model.predict(new_x))

0 comments on commit 3471233

Please sign in to comment.