Project 1
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
In [3]:
df = pd.read_csv('heart.csv')
In [4]:
df
Out[4]:
| age | sex | cp | trtbps | chol | fbs | restecg | thalachh | exng | oldpeak | slp | caa | thall | output | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 63 | 1 | 3 | 145 | 233 | 1 | 0 | 150 | 0 | 2.3 | 0 | 0 | 1 | 1 |
| 1 | 37 | 1 | 2 | 130 | 250 | 0 | 1 | 187 | 0 | 3.5 | 0 | 0 | 2 | 1 |
| 2 | 41 | 0 | 1 | 130 | 204 | 0 | 0 | 172 | 0 | 1.4 | 2 | 0 | 2 | 1 |
| 3 | 56 | 1 | 1 | 120 | 236 | 0 | 1 | 178 | 0 | 0.8 | 2 | 0 | 2 | 1 |
| 4 | 57 | 0 | 0 | 120 | 354 | 0 | 1 | 163 | 1 | 0.6 | 2 | 0 | 2 | 1 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 298 | 57 | 0 | 0 | 140 | 241 | 0 | 1 | 123 | 1 | 0.2 | 1 | 0 | 3 | 0 |
| 299 | 45 | 1 | 3 | 110 | 264 | 0 | 1 | 132 | 0 | 1.2 | 1 | 0 | 3 | 0 |
| 300 | 68 | 1 | 0 | 144 | 193 | 1 | 1 | 141 | 0 | 3.4 | 1 | 2 | 3 | 0 |
| 301 | 57 | 1 | 0 | 130 | 131 | 0 | 1 | 115 | 1 | 1.2 | 1 | 1 | 3 | 0 |
| 302 | 57 | 0 | 1 | 130 | 236 | 0 | 0 | 174 | 0 | 0.0 | 1 | 1 | 2 | 0 |
303 rows × 14 columns
In [5]:
df.isnull().sum()
Out[5]:
age 0 sex 0 cp 0 trtbps 0 chol 0 fbs 0 restecg 0 thalachh 0 exng 0 oldpeak 0 slp 0 caa 0 thall 0 output 0 dtype: int64
In [6]:
df.dtypes
Out[6]:
age int64 sex int64 cp int64 trtbps int64 chol int64 fbs int64 restecg int64 thalachh int64 exng int64 oldpeak float64 slp int64 caa int64 thall int64 output int64 dtype: object
In [7]:
x = df.drop(['output'], axis = 1)
y = df['output']
In [10]:
df.shape
Out[10]:
(303, 14)
In [8]:
x
Out[8]:
| age | sex | cp | trtbps | chol | fbs | restecg | thalachh | exng | oldpeak | slp | caa | thall | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 63 | 1 | 3 | 145 | 233 | 1 | 0 | 150 | 0 | 2.3 | 0 | 0 | 1 |
| 1 | 37 | 1 | 2 | 130 | 250 | 0 | 1 | 187 | 0 | 3.5 | 0 | 0 | 2 |
| 2 | 41 | 0 | 1 | 130 | 204 | 0 | 0 | 172 | 0 | 1.4 | 2 | 0 | 2 |
| 3 | 56 | 1 | 1 | 120 | 236 | 0 | 1 | 178 | 0 | 0.8 | 2 | 0 | 2 |
| 4 | 57 | 0 | 0 | 120 | 354 | 0 | 1 | 163 | 1 | 0.6 | 2 | 0 | 2 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 298 | 57 | 0 | 0 | 140 | 241 | 0 | 1 | 123 | 1 | 0.2 | 1 | 0 | 3 |
| 299 | 45 | 1 | 3 | 110 | 264 | 0 | 1 | 132 | 0 | 1.2 | 1 | 0 | 3 |
| 300 | 68 | 1 | 0 | 144 | 193 | 1 | 1 | 141 | 0 | 3.4 | 1 | 2 | 3 |
| 301 | 57 | 1 | 0 | 130 | 131 | 0 | 1 | 115 | 1 | 1.2 | 1 | 1 | 3 |
| 302 | 57 | 0 | 1 | 130 | 236 | 0 | 0 | 174 | 0 | 0.0 | 1 | 1 | 2 |
303 rows × 13 columns
In [11]:
y
Out[11]:
0 1
1 1
2 1
3 1
4 1
..
298 0
299 0
300 0
301 0
302 0
Name: output, Length: 303, dtype: int64
In [14]:
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.3,random_state = 1)
In [15]:
x_train.shape,y_train.shape
Out[15]:
((212, 13), (212,))
In [16]:
x_test.shape,y_test.shape
Out[16]:
((91, 13), (91,))
In [17]:
x_train.head()
Out[17]:
| age | sex | cp | trtbps | chol | fbs | restecg | thalachh | exng | oldpeak | slp | caa | thall | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 34 | 51 | 1 | 3 | 125 | 213 | 0 | 0 | 125 | 1 | 1.4 | 2 | 1 | 2 |
| 185 | 44 | 1 | 0 | 112 | 290 | 0 | 0 | 153 | 0 | 0.0 | 2 | 1 | 2 |
| 187 | 54 | 1 | 0 | 124 | 266 | 0 | 0 | 109 | 1 | 2.2 | 1 | 1 | 3 |
| 147 | 60 | 0 | 3 | 150 | 240 | 0 | 1 | 171 | 0 | 0.9 | 2 | 0 | 2 |
| 202 | 58 | 1 | 0 | 150 | 270 | 0 | 0 | 111 | 1 | 0.8 | 2 | 0 | 3 |
In [18]:
# NUMERICAL COLUMNS
x_train_num = x_train.select_dtypes(include=['int64','float64'])
x_train_num
x_train_num.head()
Out[18]:
| age | sex | cp | trtbps | chol | fbs | restecg | thalachh | exng | oldpeak | slp | caa | thall | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 34 | 51 | 1 | 3 | 125 | 213 | 0 | 0 | 125 | 1 | 1.4 | 2 | 1 | 2 |
| 185 | 44 | 1 | 0 | 112 | 290 | 0 | 0 | 153 | 0 | 0.0 | 2 | 1 | 2 |
| 187 | 54 | 1 | 0 | 124 | 266 | 0 | 0 | 109 | 1 | 2.2 | 1 | 1 | 3 |
| 147 | 60 | 0 | 3 | 150 | 240 | 0 | 1 | 171 | 0 | 0.9 | 2 | 0 | 2 |
| 202 | 58 | 1 | 0 | 150 | 270 | 0 | 0 | 111 | 1 | 0.8 | 2 | 0 | 3 |
In [19]:
# scaling the numerical features
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
# column names are (annoying) lost after scaling
# i.e., the datframe is converted into a numpy ndarray)
x_train_num_rescaled = pd.DataFrame(scaler.fit_transform(x_train_num),
columns = x_train_num.columns,
index = x_train_num.index)
x_train_num_rescaled.head()
Out[19]:
| age | sex | cp | trtbps | chol | fbs | restecg | thalachh | exng | oldpeak | slp | caa | thall | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 34 | -0.334915 | 0.635589 | 1.992234 | -0.332611 | -0.655815 | -0.413849 | -1.030043 | -1.074173 | 1.439605 | 0.356309 | 0.924204 | 0.295586 | -0.535052 |
| 185 | -1.097207 | 0.635589 | -0.947582 | -1.071867 | 1.000106 | -0.413849 | -1.030043 | 0.121905 | -0.694635 | -0.931786 | 0.924204 | 0.295586 | -0.535052 |
| 187 | -0.008219 | 0.635589 | -0.947582 | -0.389477 | 0.483975 | -0.413849 | -1.030043 | -1.757646 | 1.439605 | 1.092364 | -0.722277 | 0.295586 | 1.133051 |
| 147 | 0.645174 | -1.573344 | 1.992234 | 1.089034 | -0.075168 | -0.413849 | 0.902427 | 0.890812 | -0.694635 | -0.103725 | 0.924204 | -0.699084 | -0.535052 |
| 202 | 0.427376 | 0.635589 | -0.947582 | 1.089034 | 0.569996 | -0.413849 | -1.030043 | -1.672212 | 1.439605 | -0.195731 | 0.924204 | -0.699084 | 1.133051 |
In [20]:
#CATEGORICAL COLUMNS
x_test_num = x_test.select_dtypes(include=['int64','float64'])
x_test_num
x_test_num.head()
Out[20]:
| age | sex | cp | trtbps | chol | fbs | restecg | thalachh | exng | oldpeak | slp | caa | thall | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 204 | 62 | 0 | 0 | 160 | 164 | 0 | 0 | 145 | 0 | 6.2 | 0 | 3 | 3 |
| 159 | 56 | 1 | 1 | 130 | 221 | 0 | 0 | 163 | 0 | 0.0 | 2 | 0 | 3 |
| 219 | 48 | 1 | 0 | 130 | 256 | 1 | 0 | 150 | 1 | 0.0 | 2 | 2 | 3 |
| 174 | 60 | 1 | 0 | 130 | 206 | 0 | 0 | 132 | 1 | 2.4 | 1 | 2 | 3 |
| 184 | 50 | 1 | 0 | 150 | 243 | 0 | 0 | 128 | 0 | 2.6 | 1 | 0 | 3 |
In [21]:
x_test_num_rescaled = pd.DataFrame(scaler.transform(x_test_num),
columns=x_test_num.columns,
index = x_test_num.index)
x_test_num_rescaled.head()
Out[21]:
| age | sex | cp | trtbps | chol | fbs | restecg | thalachh | exng | oldpeak | slp | caa | thall | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 204 | 0.862972 | -1.573344 | -0.947582 | 1.657692 | -1.709583 | -0.413849 | -1.030043 | -0.219832 | -0.694635 | 4.772635 | -2.368758 | 2.284925 | 1.133051 |
| 159 | 0.209579 | 0.635589 | 0.032356 | -0.048282 | -0.483772 | -0.413849 | -1.030043 | 0.549075 | -0.694635 | -0.931786 | 0.924204 | -0.699084 | 1.133051 |
| 219 | -0.661612 | 0.635589 | -0.947582 | -0.048282 | 0.268920 | 2.416342 | -1.030043 | -0.006246 | 1.439605 | -0.931786 | 0.924204 | 1.290255 | 1.133051 |
| 174 | 0.645174 | 0.635589 | -0.947582 | -0.048282 | -0.806354 | -0.413849 | -1.030043 | -0.775153 | 1.439605 | 1.276377 | -0.722277 | 1.290255 | 1.133051 |
| 184 | -0.443814 | 0.635589 | -0.947582 | 1.089034 | -0.010651 | -0.413849 | -1.030043 | -0.946022 | -0.694635 | 1.460391 | -0.722277 | -0.699084 | 1.133051 |
In [23]:
from sklearn.linear_model import LogisticRegression
regressor = LogisticRegression()
regressor.fit(x_train_num_rescaled,y_train)
Out[23]:
LogisticRegression()
In [25]:
y_test_pred = regressor.predict(x_test_num_rescaled)
In [26]:
temp_df = pd.DataFrame({'Actual': y_test, 'Predicted': y_test_pred})
temp_df.head()
Out[26]:
| Actual | Predicted | |
|---|---|---|
| 204 | 0 | 0 |
| 159 | 1 | 1 |
| 219 | 0 | 0 |
| 174 | 0 | 0 |
| 184 | 0 | 0 |