<jupyter_start><jupyter_text>HEART DISEASE CLASSIFICATION **We have a data which classified if patients have heart disease or not according to features in it. We will try to use this data to create a model which tries predict if a patient has this disease or not. We will use Machine Learning Algorithms.** Data contains;- age - age in years- sex - (1 = male; 0 = female)- cp - chest pain type- trestbps - resting blood pressure (in mm Hg on admission to the hospital)- chol - serum cholestoral in mg/dl- fbs - (fasting blood sugar > 120 mg/dl) (1 = true; 0 = false)- restecg - resting electrocardiographic results- thalach - maximum heart rate achieved- exang - exercise induced angina (1 = yes; 0 = no)- oldpeak - ST depression induced by exercise relative to rest- slope - the slope of the peak exercise ST segment- ca - number of major vessels (0-3) colored by flourosopy- thal - 3 = normal; 6 = fixed defect; 7 = reversable defect- target - have disease or not (1=yes, 0=no)  Importing Datasets and Libraries<jupyter_code>import os
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np<jupyter_output><empty_output><jupyter_text>READ DATA<jupyter_code>df = pd.read_csv('heart.csv')
df.head(10)<jupyter_output><empty_output><jupyter_text>Data Exploration<jupyter_code>df.target.value_counts()
sns.countplot(x='target', data=df, palette='bwr')
plt.show()
countNoDisease = len(df[df.target==0])
countHaveDisease = len(df[df.target==1])
print("Percentage of Patients Haven't Heart Disease: {:.2f}%".format((countNoDisease / (len(df.target))*100)))
print("Percentage of Patients Hav Heart Disease: {:.2f}%".format((countHaveDisease / (len(df.target))*100)))
df.describe()
df.info()
df.groupby('target').mean()
# MALE vs FEMALE
pd.crosstab(df.sex, df.target).plot(kind='bar', figsize=(15,6),color=['green', 'red'])
plt.title('Heart Disease Frequency for Sex')
plt.xlabel('Sex(0 = Female, 1 = Male)')
plt.xticks(rotation=0)
plt.legend(["Haven't Disease", "Have Disease"])
plt.ylabel('Frequency')
plt.show()
plt.scatter(x=df.age[df.target==1], y=df.thalach[(df.target==1)], c='red')
plt.scatter(x=df.age[df.target==0], y=df.thalach[(df.target==0)])
plt.legend(['Disease', 'Not Disease'])
plt.xlabel('Age')
plt.ylabel('Maximum Heart Age')
plt.show()<jupyter_output><empty_output><jupyter_text>**Creating Dummy Variable**Since 'cp', 'thal'and 'slope' are categorical variables we'll turn them into dummy variables.<jupyter_code>a = pd.get_dummies(df['cp'], prefix='cp')
b = pd.get_dummies(df['thal'], prefix='thal')
c = pd.get_dummies(df['slope'], prefix='slope')
frames = [df,a,b,c]
df = pd.concat(frames, axis=1)
df.head()
df = df.drop(columns = ['cp', 'thal', 'slope'])
df.head()<jupyter_output><empty_output><jupyter_text>Splitting the Data<jupyter_code>X = df.drop(['target'], axis=1)
y = df['target']
X.columns
y
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
print(X.shape, X_train.shape, X_test.shape)<jupyter_output>(303, 21) (242, 21) (61, 21)<jupyter_text>Machine Learning Model **Let's play with differet Machine Learning Algorithms,  from data we can say that this is a classification problem** 1. Logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(solver='liblinear')
lr.fit(X_train, y_train)
acc_lr = round(lr.score(X_train, y_train)*100, 2)
print(str(acc_lr)+ ' Percentage')<jupyter_output>86.36 Percentage<jupyter_text>2. Support Vector Classifier<jupyter_code>from sklearn.svm import SVC
svc = SVC()
svc.fit(X_train, y_train)
acc_svc = round(svc.score(X_train, y_train)*100, 2)
print(str(acc_svc)+' Percentage')<jupyter_output>66.53 Percentage<jupyter_text>3. K-Nearest Classifier<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
acc_knn = round(knn.score(X_train, y_train)*100, 2)
print(str(acc_knn)+' Percentage')<jupyter_output>78.1 Percentage<jupyter_text>4. Decision Tree Classifier<jupyter_code>from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier()
dt.fit(X_train, y_train)
acc_dt = round(dt.score(X_train, y_train)*100, 2)
print(str(acc_dt)+' Percentage')<jupyter_output>100.0 Percentage<jupyter_text>5. Random Forest Classifier<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=100)
rf.fit(X_train, y_train)
acc_rf = round(rf.score(X_train, y_train)*100, 2)
print(str(acc_rf)+' Percentage')<jupyter_output>100.0 Percentage<jupyter_text>6. Naive Bayes<jupyter_code>from sklearn.naive_bayes import GaussianNB
nb = GaussianNB()
nb.fit(X_train, y_train)
acc_nb = round(nb.score(X_train, y_train)*100, 2)
print(str(acc_nb)+' Percentage')<jupyter_output>84.71 Percentage<jupyter_text>Comparing ModelaLet's compare the accuracy score of all the models used above<jupyter_code>models = pd.DataFrame({
    'Models':['Logistic Regression', 'Support Vector', 'KNN', 'Decision Tree', 'Random Forest', 'Naive Bayes'],
    'Score':[acc_lr, acc_svc, acc_knn, acc_dt, acc_rf, acc_nb]
})

models.sort_values(by='Score', ascending=False)<jupyter_output><empty_output><jupyter_text>From above table we can see that Decision Tree and RandomForest have 100% accuracy, but above scores are based on Train datasets. So, our task is now to check accuracy_score on TEST data.So let's check<jupyter_code>from sklearn.metrics import accuracy_score<jupyter_output><empty_output><jupyter_text>Applying predict method to all algorithms<jupyter_code>lr_pred = lr.predict(X_test) #Logistic Regression
svm_pred = svc.predict(X_test) #Support Vector
knn_pred = knn.predict(X_test) #K-Nearest
dt_pred = dt.predict(X_test) #Decision Tree
rf_pred = rf.predict(X_test) #Random Forest
nb_pred = rf.predict(X_test) #Naive Bayes<jupyter_output><empty_output><jupyter_text>Time to check accuracy_score on test<jupyter_code>test_lr = round(accuracy_score(lr_pred, y_test)*100,2)
test_svm = round(accuracy_score(svm_pred, y_test)*100,2)
test_knn = round(accuracy_score(knn_pred, y_test)*100,2)
test_dt = round(accuracy_score(dt_pred, y_test)*100,2)
test_rf = round(accuracy_score(rf_pred, y_test)*100,2)
test_nb = round(accuracy_score(nb_pred, y_test)*100,2)

test_models = pd.DataFrame({
    'Models':['Logistic Regression', 'Support Vector', 'KNN', 'Decision Tree', 'Random Forest', 'Naive Bayes'],
    'Score(Test Data)':[test_lr, test_svm, test_knn, test_dt, test_rf, test_nb]
})

test_models.sort_values(by='Score(Test Data)', ascending=False)<jupyter_output><empty_output><jupyter_text>From Above two tables, we can see that **Random Forest** has better score on both test and train scores. So will evaluate our model on **Random Forest**.  Model Evaluations<jupyter_code>from sklearn.metrics import classification_report, confusion_matrix<jupyter_output><empty_output><jupyter_text>**Classification Report**<jupyter_code>print(classification_report(y_test, rf_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.85      0.85      0.85        27
           1       0.88      0.88      0.88        34

    accuracy                           0.87        61
   macro avg       0.87      0.87      0.87        61
weighted avg       0.87      0.87      0.87        61<jupyter_text>**Confusion Matrix**<jupyter_code>cm = confusion_matrix(y_test, rf_pred)
cm<jupyter_output><empty_output><jupyter_text>Visualize<jupyter_code>plt.figure(figsize=(6,4))
sns.heatmap(cm, annot=True)
plt.title('Confusion Matrix')<jupyter_output><empty_output><jupyter_text>Building Predictive Model<jupyter_code>input_data = (34, 1, 140,230,0,1,170,1, 3.2, 1, 1,0,0,0,0,1,0,0,0,0,1)

# change the input data to a numpy array
input_data_as_numpy_array= np.asarray(input_data)

# reshape the numpy array as we are predicting for only on instance
input_data_reshaped = input_data_as_numpy_array.reshape(1,-1)

prediction = rf.predict(input_data_reshaped)
print(prediction)

if (prediction[0]== 0):
  print('The Person does not have a Heart Disease')
else:
  print('The Person has Heart Disease')<jupyter_output>[0]
The Person does not have a Heart Disease
{'fraction_non_alphanumeric': 0.12850866487673906, 'fraction_numerical': 0.027947278496460826, 'mean_word_length': 6.2844444444444445, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '175657', 'license': 'Apache-2.0', 'n_tokens_mistral': 3156, 'n_tokens_neox': 2928, 'n_words': 691, 'path': 'Heart Disease Prediction/heart_disease_prediction_various_algorithms.ipynb', 'repo_name': 'xbabs/Data-Science-Projects', 'size': '8194'}