guest_blog — Published On September 1, 2020 and Last Modified On August 26th, 2022
Beginner Machine Learning Programming Python Structured Data Technique

Image for post

Problem Definition

Data Collection

 

Data Preparation and Exploration

Python Code:

The header of the heart disease datase

#Check null values
df.isnull().sum()
Image for post
     Null values in the dataset
web application - data distribution

People with and without heart disease in the dataset

Attributes Correlation

web application - correlation

web application - Scatterplot between age and heart rate

web application - chest pain

web application - angina

Image for post

web application - thalessemia

web application - correlation between opeak and slope

web application -

Data Modeling

# Initialize data and target
target = df[‘target’]
features = df.drop([‘target’], axis = 1)

– Data Splitting

# Split the data into training set and testing set
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size = 0.2, random_state = 0)

– Machine Learning Model

def fit_eval_model(model, train_features, y_train, test_features, y_test):    results = {}    # Train the model
    model.fit(train_features, y_train)    # Test the model
    train_predicted = model.predict(train_features)
    test_predicted = model.predict(test_features)    # Classification report and Confusion Matrix
    results[‘classification_report’] = classification_report(y_test,    test_predicted)
    results[‘confusion_matrix’] = confusion_matrix(y_test, test_predicted)    return results
# Initialize the models
sv = SVC(random_state = 1)
rf = RandomForestClassifier(random_state = 1)
ab = AdaBoostClassifier(random_state = 1)
gb = GradientBoostingClassifier(random_state = 1)# Fit and evaluate models
results = {}
for cls in [sv, rf, ab, gb]:
    cls_name = cls.__class__.__name__
    results[cls_name] = {}
    results[cls_name] = fit_eval_model(cls, X_train, y_train, X_test, y_test)
# Print classifiers results
for result in results:
    print (result)
    print()for i in results[result]:
    print (i, ‘:’)
    print(results[result][i])
    print()
    print (‘ — — -’)
    print()
Image for post

Gradient Boosting Result

– Save the Prediction Model

# Save the model as serialized object pickle
with open(‘model.pkl’, ‘wb’) as file:
pickle.dump(gb, file)

 

Model Deployment

/
├── model.pkl
├── heart_disease_app.py
├── templates/
 └── Heart Disease Classifier.html

 

Web App Python Code

import numpy as np
import pickle
from flask import Flask, request, render_template
# Create application
app = Flask(__name__)
# Load machine learning model
model = pickle.load(open(‘model.pkl’, ‘rb’))
# Bind home function to URL
@app.route(‘/’)
def home():
    return render_template(‘Heart Disease Classifier.html’)
# Bind predict function to URL
@app.route(‘/predict’, methods =[‘POST’])
def predict():
 
    # Put all form entries values in a list 
    features = [float(i) for i in request.form.values()]
    # Convert features to array
    array_features = [np.array(features)]
    # Predict features
    prediction = model.predict(array_features)
 
    output = prediction
 
    # Check the output values and retrieve the result with html tag based on the value
 if output == 1:
    return render_template(‘Heart Disease Classifier.html’, 
 result = ‘The patient is not likely to have heart disease!’)
 else:
    return render_template(‘Heart Disease Classifier.html’, 
 result = ‘The patient is likely to have heart disease!’)
if __name__ == ‘__main__’:
#Run the application
app.run()

 

HTML Template

Image for post

<form action = “{{url_for(‘predict’)}}” method =”POST” >
<strong style="color:red">{{result}}</strong>

Image for post

Summary

About the Author

Nada Alay

I am working in the data analytics field and passionate about data science, machine learning, and scientific research.

About the Author

guest_blog

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

Leave a Reply Your email address will not be published. Required fields are marked *

Top Resources