-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple linear regression.py
More file actions
121 lines (58 loc) · 1.59 KB
/
Simple linear regression.py
File metadata and controls
121 lines (58 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
# Loading the data
# In[ ]:
df = pd.read_csv('C:/Users/3yearb1/Desktop/ML_datasets/Salary_dataset.csv')
# In[ ]:
display(df.head())
# In[8]:
df.info()
# In[15]:
df.describe()
# In[16]:
sns.regplot(x=df['YearsExperience'],y=df['Salary'])
plt.xlabel("Years Of Experience")
plt.ylabel("Salary")
plt.title("Salary vs Experience")
plt.show()
# Preparing X and Y
# In[37]:
X=df['YearsExperience'].values.reshape(-1, 1)
y=df['Salary']
# Splitting X and Y into training and testing sets
# In[39]:
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)
# Model Building
# In[40]:
model = LinearRegression()
# Model Training
# In[ ]:
model.fit(X_train, y_train)
# Testing Model
# In[44]:
y_pred=model.predict(X_test)
# Calculating MSE and Accuracy
# In[45]:
mse =mean_squared_error(y_test,y_pred)
r2=r2_score(y_test,y_pred)
print(f"Mean Squared Error:{mse}")
print(f"R-squared score: {r2}")
# In[47]:
plt.scatter(X_test, y_test, color='blue', label='Actual')
plt.plot(X_test, y_pred, color='red', linewidth=2, label='Predicted')
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.title("Linear Regression: Salary vs Experience")
plt.legend()
plt.show()
# In[48]:
print(f"intercept: {model.intercept_}")
print(f"Cofficient:{model.coef_[0]}" )