forked from dsdl-club/DSDL-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AkshitGulyan_AIML_LinearRegression.py
106 lines (53 loc) · 1.56 KB
/
AkshitGulyan_AIML_LinearRegression.py
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
#!/usr/bin/env python
# coding: utf-8
# In[7]:
import numpy as np
import math
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import linear_model
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import mean_squared_error
data=pd.read_csv('heart.csv')
# In[8]:
data.head()
# In[9]:
data.info()
# In[10]:
from sklearn.preprocessing import LabelEncoder
# In[11]:
le=LabelEncoder()
# In[12]:
data.Age=le.fit_transform(data.Age)
data.Sex=le.fit_transform(data.Sex)
data.ChestPainType=le.fit_transform(data.ChestPainType)
data.RestingBP=le.fit_transform(data.RestingBP)
data.Cholesterol=le.fit_transform(data.Cholesterol)
data.FastingBS=le.fit_transform(data.FastingBS)
data.RestingECG=le.fit_transform(data.RestingECG)
data.MaxHR=le.fit_transform(data.MaxHR)
data.ExerciseAngina=le.fit_transform(data.ExerciseAngina)
data.Oldpeak=le.fit_transform(data.Oldpeak)
data.ST_Slope=le.fit_transform(data.ST_Slope)
# In[13]:
data.HeartDisease=le.fit_transform(data.HeartDisease)
# In[14]:
data.head()
# In[16]:
x=data.iloc[ : ,:11]
y=data['HeartDisease']
# In[17]:
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.4,random_state=2)
# In[18]:
reg = linear_model.LinearRegression()
reg.fit(x_train, y_train)
y_pred = reg.predict(x_test)
print('Coefficients: ', reg.coef_)
rmse = math.sqrt(mean_squared_error(y_test,y_pred))
rmse
# In[19]:
plt.scatter(x_test.Cholesterol, y_pred, label = 'Predicted')
plt.scatter(x_test.Cholesterol, y_test, label = 'Actual')
plt.legend()
# In[ ]: