0% found this document useful (0 votes)
6 views

Assignment 1

Uploaded by

krishnaanikam911
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment 1

Uploaded by

krishnaanikam911
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

10/17/24, 4:05 Assignment

PM 1

In [1]: #Pranav Kulkarni(T512004)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

In [3]: dataFrame=pd.read_csv('heart.csv')

In [5]: dataFrame.shape

Out[5]: (303, 15)

In dataFrame.head()
[7]:
Unnamed: Age Sex ChestPain RestBP Chol Fbs RestECG MaxHR
Out[7]: 0 ExAng O

0 1 63 1 typical 145 233 1 2 150 0

1 2 67 1 asymptomatic 160 286 0 2 108 1

2 3 67 1 asymptomatic 120 229 0 2 129 1

3 4 37 1 nonanginal 130 250 0 0 187 0

4 5 41 0 nontypical 130 204 0 2 172 0

In [9]: dataFrame.tail()

Out[9]: Unnamed
Age Sex ChestPain RestB Chol Fbs RestEC MaxHR ExAng
:
P G
0
298 299 45 1 typical 110 264 0 0 132 0

299 300 68 1 asymptomati 144 193 1 0 141 0


c
300 301 57 1 asymptomati 130 131 0 0 115 1
c
301 302 57 0 nontypical 130 236 0 2 174 0

302 303 38 1 nonanginal 138 175 0 0 173 0

In [11]: dataFrame=dataFrame.drop("Unnamed: 0",axis =1)

In [13]: dataFrame.dtypes

file:///C:/Users/Student/Downloads/Assignment1 1/11
(5).html
10/17/24, 4:05 Assignment
PM 1

Out[13]: Age int64


Sex int64
ChestPain object
RestBP int64
Chol int64
Fbs int64
RestECG int64
MaxHR int64
ExAng int64
Oldpeak float6
4
Slope int64
Ca float6
4
Thal object
AHD object
dtype:
object

In [15]: dataFrame.describe()

Out[15]: Age Sex RestBP Chol Fbs RestECG Max

count 303.00000 303.00000 303.00000 303.00000 303.00000 303.00000 303.00


0 0 0 0 0 0 0
mean 54.438944 0.679868 131.68976 246.69306 0.148515 0.990099 149.60
9 9 7
std 9.038662 0.467299 17.599748 51.776918 0.356198 0.994971 22.875

min 29.000000 0.000000 94.000000 126.00000 0.000000 0.000000 71.000


0
25% 48.000000 0.000000 120.00000 211.00000 0.000000 0.000000 133.50
0 0 0
50% 56.000000 1.000000 130.00000 241.00000 0.000000 1.000000 153.00
0 0 0
75% 61.000000 1.000000 140.00000 275.00000 0.000000 2.000000 166.00
0 0 0
max 77.000000 1.000000 200.00000 564.00000 1.000000 2.000000 202.00
0 0 0

In [17]: dataFrame.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 303 entries, 0 to 302
Data columns (total 14 columns):
# Column Non-Null Count Dtype

0 Age 30 non-null int64


3
1 Sex 30 non-null int64
3
2 ChestPain 30 non-null object
3
3 RestBP 30 non-null int64
3
4 Chol 30 non-null int64
3
5 Fbs 30 non-null int64
3
6 RestECG 30 non-null int64
3

file:///C:/Users/Student/Downloads/Assignment1 2/11
(5).html
10/17/24, 4:05 Assignment
PM 1
7 MaxHR 30 non-null int64
3
8 ExAng 30 non-null int64
3
9 Oldpeak 30 non-null float64
3
1 Slope 30 non-null int64
0 3
1 Ca 29 non-null float64
1 9
1 Thal 30 non-null object
2 1
1 AHD 30 non-null object
3 3
dtypes: float64(2), int64(9), object(3)
memory usage: 33.3+ KB

file:///C:/Users/Student/Downloads/Assignment1 3/11
(5).html
10/17/24, 4:05 Assignment
PM 1

In [19]: dataFrame.Ca.value_counts()

Out[19]: Ca
0.0 176
1.0 65
2.0 38
3.0 20
Name: count, dtype:
int64

In [21]: dataFrame.Sex.value_counts()

Out[21] Sex
:
1 206
0 97
Name: count, dtype: int64

dataFrame.isnull()
In
[23]:

Out[23]: Age Sex ChestPai RestB Chol Fbs RestEC MaxHR ExAng Oldpea S
n P G k
0 False False False False False False False False False False

1 False False False False False False False False False False

2 False False False False False False False False False False

3 False False False False False False False False False False

4 False False False False False False False False False False

... ... ... ... ... ... ... ... ... ... ...

298 False False False False False False False False False False

299 False False False False False False False False False False

300 False False False False False False False False False False

301 False False False False False False False False False False

302 False False False False False False False False False False

303 rows × 14 columns

In [25]: dataFrame.isnull().sum()

file:///C:/Users/Student/Downloads/Assignment1 4/11
(5).html
10/17/24, 4:05 Assignment
PM 1

Out[25]: Age 0
Sex 0
ChestPain 0
RestBP 0
Chol 0
Fbs 0
RestECG 0
MaxHR 0
ExAng 0
Oldpeak 0
Slope 0
Ca 4
Thal 2
AHD 0
dtype:
int64

In [27]: dataFrame.Age.mean()

Out[27]: 54.43894389438944

In [29]: dataFrame.describe()

Out[29]: Age Sex RestBP Chol Fbs RestECG Max

count 303.00000 303.00000 303.00000 303.00000 303.00000 303.00000 303.00


0 0 0 0 0 0 0
mean 54.438944 0.679868 131.68976 246.69306 0.148515 0.990099 149.60
9 9 7
std 9.038662 0.467299 17.599748 51.776918 0.356198 0.994971 22.875

min 29.000000 0.000000 94.000000 126.00000 0.000000 0.000000 71.000


0
25% 48.000000 0.000000 120.00000 211.00000 0.000000 0.000000 133.50
0 0 0
50% 56.000000 1.000000 130.00000 241.00000 0.000000 1.000000 153.00
0 0 0
75% 61.000000 1.000000 140.00000 275.00000 0.000000 2.000000 166.00
0 0 0
max 77.000000 1.000000 200.00000 564.00000 1.000000 2.000000 202.00
0 0 0

In [31]: dataFrame["Age"].mean(axis=0)

Out[31]: 54.43894389438944

In [33]: var=dataFrame.loc[:,['Age','Sex','ChestPain','RestBP','Chol']]

In [35]: var

file:///C:/Users/Student/Downloads/Assignment1 5/11
(5).html
10/17/24, 4:05 Assignment
PM 1

Out[35]: Age Sex ChestPain RestB Chol


P
0 63 1 typical 145 233

1 67 1 asymptomati 160 286


c
2 67 1 asymptomati 120 229
c
3 37 1 nonanginal 130 250

4 41 0 nontypical 130 204

... ... ... ... ... ...

298 45 1 typical 110 264

299 68 1 asymptomati 144 193


c
300 57 1 asymptomati 130 131
c
301 57 0 nontypical 130 236

302 38 1 nonanginal 138 175

303 rows × 5 columns

In [37]: from sklearn.model_selection import train_test_split

X_train, X_test = train_test_split(var, test_size = 0.25, random_state =


42) X_train.shape, X_test.shape

Out[37]: ((227, 5), (76, 5))

In [39]: tp=9
0
fp=1
1
fn=1
9
tn=4
0
acc=(tp+tn)/(tp+fp+fn+tn)
pre=tp/(tp+fp)
rec=tp/(tp+fn)
print("Accuracy is : {}".format(acc))
Accuracy is : 0.8125
Precision is : 0.8910891089108911
Recall is : 0.8256880733944955
F1-Score is : 0.8571428571428572

In [43]: dataFrame.plot();

file:///C:/Users/Student/Downloads/Assignment1 6/11
(5).html
10/17/24, 4:05 Assignment
PM 1

In [45]: dataFrame.hist(bins = 10,figsize = (15,11));

In [47]: sns.pairplot(var);

file:///C:/Users/Student/Downloads/Assignment1 7/11
(5).html
10/17/24, 4:05 Assignment
PM 1

In [49]: labels=['Male','Female']
dataFrame.Sex.value_counts().plot(kind= "pie",labels
=labels,startangle=90,shado
explode=(0,0),autopct='%1.1f%%' );

file:///C:/Users/Student/Downloads/Assignment1 8/11
(5).html
10/17/24, 4:05 Assignment
PM 1

In [51]: plt.hist(dataFrame["Age"],bins=15,label="Age Count")


plt.title("Age Histogram")
plt.xlabel("Age range")
plt.ylabel("Count according to
Age") plt.legend(loc="upper
left");

In [53]: sns.barplot(x = "Slope", y = "Age", hue = "Sex", data = dataFrame)


plt.title("Slope Group - Count Bar Plot Grouped by Sex")

file:///C:/Users/Student/Downloads/Assignment1 9/11
(5).html
10/17/24, 4:05 Assignment
PM 1
plt.show()

In [55]: sns.barplot(x = "Ca", y = "Chol", hue = "Slope", data = dataFrame)


plt.title("Ca Group - Count Bar Plot Grouped by Slope")
plt.show()

file:///C:/Users/Student/Downloads/Assignment1 10/
(5).html 11
10/17/24, 4:05 Assignment
PM 1

In [57]: plt.scatter(dataFrame["RestBP"],dataFrame["Age"],label="BP level according


to Ag plt.title("BP vs Age Scatterplot")
plt.xlabel("Rest BP level")
plt.ylabel("Age")
plt.legend(loc="lower right");

In [ ]:

file:///C:/Users/Student/Downloads/Assignment1 11/
(5).html 11

You might also like