0% found this document useful (0 votes)
7 views7 pages

Practical 1

Psq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Practical 1

Psq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Name:Harshad Kamble

Roll No:23

Aim:
Perform following operation on given dataset. a) Find Shape of Data b) Find Missing
Values c) Find data type of each column d) Finding out Zero's e) Find Mean age of
patients f) Now extract only Age, Sex, ChestPain, RestBP, Chol. Randomly divide dataset
in training (75%) and testing (25%).

In [4]: import pandas as pd


import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import ConfusionMatrixDisplay

In [5]: df = pd.read_csv("C:\\Users\\samruddhi\\Downloads\\Heart.csv")

In [4]: df.head()

Out[4]: Unnamed:
Age Sex ChestPain RestBP Chol Fbs RestECG MaxHR ExAng O
0

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 [5]: df.shape

Out[5]: (303, 15)

In [6]: df.isnull().sum()

file:///C:/Users/maaka/AppData/Local/Microsoft/Windows/INetCache/IE/4F19044N/harsh.html 1/7
Out[6]: Unnamed: 0 0
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 [7]: df.info

Out[7]: <bound method DataFrame.info of Unnamed: 0 Age Sex ChestPain RestBP


Chol Fbs RestECG MaxHR \
0 1 63 1 typical 145 233 1 2 150
1 2 67 1 asymptomatic 160 286 0 2 108
2 3 67 1 asymptomatic 120 229 0 2 129
3 4 37 1 nonanginal 130 250 0 0 187
4 5 41 0 nontypical 130 204 0 2 172
.. ... ... ... ... ... ... ... ... ...
298 299 45 1 typical 110 264 0 0 132
299 300 68 1 asymptomatic 144 193 1 0 141
300 301 57 1 asymptomatic 130 131 0 0 115
301 302 57 0 nontypical 130 236 0 2 174
302 303 38 1 nonanginal 138 175 0 0 173

ExAng Oldpeak Slope Ca Thal AHD


0 0 2.3 3 0.0 fixed No
1 1 1.5 2 3.0 normal Yes
2 1 2.6 2 2.0 reversable Yes
3 0 3.5 3 0.0 normal No
4 0 1.4 1 0.0 normal No
.. ... ... ... ... ... ...
298 0 1.2 2 0.0 reversable Yes
299 0 3.4 2 2.0 reversable Yes
300 1 1.2 2 1.0 reversable Yes
301 0 0.0 2 1.0 normal Yes
302 0 0.0 1 NaN normal No

[303 rows x 15 columns]>

In [8]: df.dtypes

file:///C:/Users/maaka/AppData/Local/Microsoft/Windows/INetCache/IE/4F19044N/harsh.html 2/7
Out[8]: Unnamed: 0 int64
Age int64
Sex int64
ChestPain object
RestBP int64
Chol int64
Fbs int64
RestECG int64
MaxHR int64
ExAng int64
Oldpeak float64
Slope int64
Ca float64
Thal object
AHD object
dtype: object

In [9]: df==0

Out[9]: Unnamed:
Age Sex ChestPain RestBP Chol Fbs RestECG MaxHR ExAng
0

0 False False False False False False False False False True

1 False False False False False False True False False False

2 False False False False False False True False False False

3 False False False False False False True True False True

4 False False True False False False True False False True

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

298 False False False False False False True True False True

299 False False False False False False False True False True

300 False False False False False False True True False False

301 False False True False False False True False False True

302 False False False False False False True True False True

303 rows × 15 columns

In [10]: df[df==0]

file:///C:/Users/maaka/AppData/Local/Microsoft/Windows/INetCache/IE/4F19044N/harsh.html 3/7
Out[10]: Unnamed:
Age Sex ChestPain RestBP Chol Fbs RestECG MaxHR ExAng
0

0 NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0

1 NaN NaN NaN NaN NaN NaN 0.0 NaN NaN NaN

2 NaN NaN NaN NaN NaN NaN 0.0 NaN NaN NaN

3 NaN NaN NaN NaN NaN NaN 0.0 0.0 NaN 0.0

4 NaN NaN 0.0 NaN NaN NaN 0.0 NaN NaN 0.0

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

298 NaN NaN NaN NaN NaN NaN 0.0 0.0 NaN 0.0

299 NaN NaN NaN NaN NaN NaN NaN 0.0 NaN 0.0

300 NaN NaN NaN NaN NaN NaN 0.0 0.0 NaN NaN

301 NaN NaN 0.0 NaN NaN NaN 0.0 NaN NaN 0.0

302 NaN NaN NaN NaN NaN NaN 0.0 0.0 NaN 0.0

303 rows × 15 columns

In [11]: (df==0).sum()

Out[11]: Unnamed: 0 0
Age 0
Sex 97
ChestPain 0
RestBP 0
Chol 0
Fbs 258
RestECG 151
MaxHR 0
ExAng 204
Oldpeak 99
Slope 0
Ca 176
Thal 0
AHD 0
dtype: int64

In [12]: df.Age.mean()

Out[12]: 54.43894389438944

In [13]: df.columns

Out[13]: Index(['Unnamed: 0', 'Age', 'Sex', 'ChestPain', 'RestBP', 'Chol', 'Fbs',


'RestECG', 'MaxHR', 'ExAng', 'Oldpeak', 'Slope', 'Ca', 'Thal', 'AHD'],
dtype='object')

In [18]: data = df[['Age', 'Sex', 'ChestPain', 'RestBP', 'Chol']]

file:///C:/Users/maaka/AppData/Local/Microsoft/Windows/INetCache/IE/4F19044N/harsh.html 4/7
In [20]: train,test = train_test_split(data,test_size=0.25,random_state=1)

In [21]: train.shape

Out[21]: (227, 5)

In [22]: test.shape

Out[22]: (76, 5)

In [24]: actual = np.concatenate((np.ones(45),np.zeros(450),np.ones(5)))


actual

Out[24]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 1., 1., 1., 1., 1.])

In [25]: predicted = np.concatenate((np.ones(100),np.zeros(400)))


predicted

file:///C:/Users/maaka/AppData/Local/Microsoft/Windows/INetCache/IE/4F19044N/harsh.html 5/7
Out[25]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0.])

In [26]: type(predicted)

Out[26]: numpy.ndarray

In [27]: ConfusionMatrixDisplay.from_predictions(actual,predicted)

Out[27]: <sklearn.metrics._plot.confusion_matrix.ConfusionMatrixDisplay at 0x26621a9afd0


>

file:///C:/Users/maaka/AppData/Local/Microsoft/Windows/INetCache/IE/4F19044N/harsh.html 6/7
In [28]: from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score

In [29]: print(classification_report(actual,predicted))

precision recall f1-score support

0.0 0.99 0.88 0.93 450


1.0 0.45 0.90 0.60 50

accuracy 0.88 500


macro avg 0.72 0.89 0.76 500
weighted avg 0.93 0.88 0.90 500

In [30]: accuracy_score(actual,predicted)

Out[30]: 0.88

In [ ]:

file:///C:/Users/maaka/AppData/Local/Microsoft/Windows/INetCache/IE/4F19044N/harsh.html 7/7

You might also like