Challenge

Project 1

Project 1:

Suppose you are appointed as a Data scientist in any Pharma Company. That company makes medicine for heart disease. Your senior manager has given several clinical parameters about a patient, can you predict whether or not the patient has heart disease?
There are following thirteens clinical parameters of the patient.

1. age - age in years
2. sex - (1 = male; 0 = female)
3. cp - chest pain type

  • 0: Typical angina: chest pain related decrease blood supply to the heart
  • 1: Atypical angina: chest pain not related to heart
  • 2: Non-anginal pain: typically esophageal spasms (non heart related)
  • 3: Asymptomatic: chest pain not showing signs of disease

4. trtbps - resting blood pressure (in mm Hg on admission to the hospital) anything above 130-140 is typically cause for concern
5. chol - serum cholestoral in mg/dl

  • serum = LDL + HDL + .2 * triglycerides
  • above 200 is cause for concern

6. fbs - (fasting blood sugar > 120 mg/dl) (1 = true; 0 = false)

  • '>126' mg/dL signals diabetes

7. restecg - resting electrocardiographic results

  • 0: Nothing to note
  • 1: ST-T Wave abnormality
    • 1. can range from mild symptoms to severe problems
    • 2. signals non-normal heart beat
  • 2: Possible or definite left ventricular hypertrophy
    • 1. Enlarged heart's main pumping chamber

8. thalachh - maximum heart rate achieved

9. exng - exercise induced angina (1 = yes; 0 = no)

10. oldpeak - ST depression induced by exercise relative to rest looks at stress of heart during excercise unhealthy heart will stress more

11. slp - the slope of the peak exercise ST segment

  • 0: Upsloping: better heart rate with excercise (uncommon)
  • 1: Flatsloping: minimal change (typical healthy heart)
  • 2: Downslopins: signs of unhealthy heart

12. caa - number of major vessels (0-3) colored by flourosopy

  • colored vessel means the doctor can see the blood passing through
  • the more blood movement the better (no clots)

13. thall - thalium stress result

  • 1,3: normal
  • 6: fixed defect: used to be defect but ok now
  • 7: reversable defect: no proper blood movement when excercising

14. output - have disease or not (1=yes, 0=no) (= the predicted attribute)

When you working on the health of patients then accuracy is deciding factor, Apply different machine learning algorithms and check the accuracy about predicting whether or not the patient has heart disease.

Apply all five different machine learning models Logistic Regression, K-Nearest Neighbours Classifier, Support Vector machine, Decision Tree Classifier, Random Forest Classifier on
the given dataset.

Solution:

# -*- coding: utf-8 -*-
"""
Created on Mon Aug 15 21:00:06 2022

@author: TUF
"""

"""
Logistic regressio n
"""
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Importing Library for logistic regression
from sklearn.linear_model import LogisticRegression

# Importing performance metrics - accuracy_score, confusion_matrix
from sklearn.metrics import accuracy_score,confusion_matrix



###############################################################################
# Reading Data Set


crash_data = pd.read_csv('heart.csv')

"""
unique class
"""
print(np.unique(crash_data['sex'],return_counts=True))

# Storing the column names
crash_data_columns_list = list(crash_data.columns)

print(crash_data_columns_list)


# Separating the unput names from species
features = list(set(crash_data_columns_list)- set(['sex']))
print(features)

# Storing the output values in y
target = list(['sex'])
y= crash_data[target].values

y= crash_data['sex'].values
print(y)


# Storing the values from input features
x = crash_data[features].values

# Splitting the data into train and test
train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.3,random_state=0)

# Data scaling

scaler= StandardScaler()

# Fit on training set only
scaler.fit(train_x)

# Apply transform to both the training set and the test set.
train_x = scaler.transform(train_x)

test_x = scaler.transform(test_x)

# Make an instance of the model
logistic = LogisticRegression()

# Fitting the values frof x and y
logistic.fit(train_x,train_y)


# Prediction from test data
prediction= logistic.predict(test_x)


# Confusion matrix
confusion_matrix = confusion_matrix(prediction,test_y)
print(confusion_matrix)

# Calculating the accuracy
accuracy_score= accuracy_score(prediction,test_y)
print(accuracy_score)


# Printing the misclassified valuesfrom prediction
print('Misclassified samples: %d' % (test_y != prediction).sum())

Output:

runfile('D:/3. skill lync/Challanges/7.ML/project1/project1.py', wdir='D:/3. skill lync/Challanges/7.ML/project1')
(array([0, 1], dtype=int64), array([ 96, 207], dtype=int64))
['age', 'sex', 'cp', 'trtbps', 'chol', 'fbs', 'restecg', 'thalachh', 'exng', 'oldpeak', 'slp', 'caa', 'thall', 'output']
['chol', 'restecg', 'slp', 'output', 'exng', 'cp', 'thalachh', 'age', 'thall', 'oldpeak', 'trtbps', 'fbs', 'caa']
[1 1 0 1 0 1 0 1 1 1 1 0 1 1 0 0 0 0 1 0 1 1 1 1 1 0 1 1 0 1 0 1 1 1 1 0 0
 1 0 0 0 1 1 0 1 1 1 1 0 0 0 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 1 0 1 0 1 1 1 1
 0 0 1 1 1 1 1 1 0 1 0 0 1 1 0 0 1 1 1 0 0 1 0 1 1 1 1 1 0 1 1 0 1 0 0 0 0
 1 0 1 1 0 1 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 1 1 0 1 0 0 0 1 0 0
 1 1 1 0 1 0 0 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1
 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 0 0 1 1 1 0 1
 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0
 1 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1
 0 1 0 1 1 1 0]
[[ 7 12]
 [14 58]]
0.7142857142857143
Misclassified samples: 26
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.