[go: up one dir, main page]

0% found this document useful (0 votes)
12 views2 pages

Python ML Script

The document outlines a Python script for predicting heart disease using a decision tree classifier. It preprocesses a dataset by encoding categorical variables, splits the data into training and testing sets, trains the model, and evaluates its accuracy. Additionally, it demonstrates how to save and load the trained model for making predictions on new data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Python ML Script

The document outlines a Python script for predicting heart disease using a decision tree classifier. It preprocesses a dataset by encoding categorical variables, splits the data into training and testing sets, trains the model, and evaluates its accuracy. Additionally, it demonstrates how to save and load the trained model for making predictions on new data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import numpy as np

import pandas as pd

from sklearn.model_selection import train_test_split


from sklearn import tree

dataset = pd.read_csv('C:/Users/a424427/OneDrive - Ashland/Desktop/Work/BI


Reporting Samples for Practice/PowerBI/Disease_Prediction_ML/Heart Failure
Prediction/heart.csv')

dataset['Sex'] = dataset['Sex'].replace({'M':1,'F':2})
dataset['ChestPainType'] =
dataset['ChestPainType'].replace({'ATA':1,'NAP':2,'ASY':3,'TA':4})
dataset['RestingECG'] = dataset['RestingECG'].replace({'LVH':1,'Normal':2,'ST':3})
dataset['ExerciseAngina'] = dataset['ExerciseAngina'].replace({'N':1,'Y':2})
dataset['ST_Slope'] = dataset['ST_Slope'].replace({'Up':1,'Flat':2,'Down':3})

Y = dataset['HeartDisease']
features =
['Age','Sex','ChestPainType','RestingBP','Cholesterol','FastingBS','RestingECG','Ma
xHR','ExerciseAngina','Oldpeak','ST_Slope']
X = dataset[features]

x_train,x_test,y_train,y_test = train_test_split(X,Y)

dc = tree.DecisionTreeClassifier()
dc.fit(x_train,y_train)

y_pred = dc.predict(X)

dataset['Predict'] = y_pred

from sklearn.metrics import accuracy_score

y_pred = dc.predict(x_test)
dataset['accuracy'] = accuracy_score(y_test, y_pred)

import joblib
joblib.dump(dc,'C:/Users/a424427/Downloads/Heart_Predict.pkl')

####################################################################

import numpy as np
import pandas as pd
import joblib

dataset = pd.read_csv('C:/Users/a424427/OneDrive - Ashland/Desktop/Work/BI


Reporting Samples for Practice/PowerBI/Disease_Prediction_ML/Heart Failure
Prediction/Test_Model.csv')

dataset['Sex'] = dataset['Sex'].replace({'M':1,'F':2})
dataset['ChestPainType'] =
dataset['ChestPainType'].replace({'ATA':1,'NAP':2,'ASY':3,'TA':4})
dataset['RestingECG'] = dataset['RestingECG'].replace({'LVH':1,'Normal':2,'ST':3})
dataset['ExerciseAngina'] = dataset['ExerciseAngina'].replace({'N':1,'Y':2})
dataset['ST_Slope'] = dataset['ST_Slope'].replace({'Up':1,'Flat':2,'Down':3})
features =
['Age','Sex','ChestPainType','RestingBP','Cholesterol','FastingBS','RestingECG','Ma
xHR','ExerciseAngina','Oldpeak','ST_Slope']
X = dataset[features]

Model_Path = "C:/Users/a424427/OneDrive - Ashland/Desktop/Work/BI Reporting Samples


for Practice/PowerBI/Disease_Prediction_ML/Heart Failure
Prediction/Heart_Predict.pkl"
Load_Model = joblib.load(Model_Path)

pred = Load_Model.predict(X)

dataset['Heart_Disease'] = pred

You might also like