<jupyter_start><jupyter_text>Importing Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Importing Dataset<jupyter_code>df = pd.read_csv("heart.csv")
df.head(30)
df.columns
max(df["chol"])
#Spliting Features and Dependent variables
X = df.iloc[:, :-1]
y = df.iloc[:, -1]
X.head()
X.columns<jupyter_output><empty_output><jupyter_text>Visualization of Data<jupyter_code>#Finding Correlation between Features and dropping any features whose correlation is high (>0.8)
import seaborn as sns
plt.rcParams['figure.figsize'] = (15.0, 10.0)
dataplot = sns.heatmap(X.corr(), cmap="YlGnBu", annot=True)
plt.show()<jupyter_output><empty_output><jupyter_text>Since we find the correlation between any two columns is less than 0.8, we are not dropping any columns<jupyter_code>print(y.value_counts())
countNoDisease = len(df[df.target == 0])
countHaveDisease = len(df[df.target == 1])

print("Percentage of Patients not having Heart Disease: {:.2f}%".format((countNoDisease / (len(df.target))*100)))
print("Percentage of Patients having Heart Disease: {:.2f}%".format((countHaveDisease / (len(df.target))*100)))
plt.rcParams['figure.figsize'] = (5.0, 5.0)
sns.countplot(x="target", data=df, palette="YlGnBu")
plt.show()<jupyter_output><empty_output><jupyter_text>Plotting the variation of heart disease with age<jupyter_code>pd.crosstab(df.age,df.target).plot(kind="bar",figsize=(20,6),color=['#ff0000','#0B4CF2' ])
plt.title('Heart Disease Frequency for Ages')
plt.legend(['Having Heart Disease', 'Not Having Heart Disease'])
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.savefig('heartDiseaseAndAges.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Plotting the variation of heart disease with age and maximum Heart Rate<jupyter_code>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 Rate")
plt.show()<jupyter_output><empty_output><jupyter_text>Training and Testing of regression Models Spliting the data into Training and Testing data<jupyter_code>from sklearn.model_selection import train_test_split
X=X.values
y=y.values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)<jupyter_output><empty_output><jupyter_text>Standardising the data<jupyter_code>from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_trainSd = sc.fit_transform(X_train)
X_testSd = sc.transform(X_test)<jupyter_output><empty_output><jupyter_text>KNN Classifier<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knnc = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
knnc.fit(X_trainSd, y_train)
y_pred1 = knnc.predict(X_testSd)
print(np.concatenate((y_pred1.reshape(len(y_pred1),1), y_test.reshape(len(y_test),1)),1))
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred1)
print(cm)
accuracy_score(y_test, y_pred1)<jupyter_output>[[21  6]
 [ 5 29]]<jupyter_text>SVM Model<jupyter_code>from sklearn.svm import SVC
SVMc = SVC(kernel = 'linear', random_state = 0)
SVMc.fit(X_trainSd, y_train)
y_pred2 = SVMc.predict(X_testSd)
cm = confusion_matrix(y_test, y_pred2)
print(cm)
accuracy_score(y_test, y_pred2)<jupyter_output>[[20  7]
 [ 4 30]]<jupyter_text>Decision Tree Classifier<jupyter_code>from sklearn.tree import DecisionTreeClassifier
DTC = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
DTC.fit(X_train, y_train)
y_pred3 = DTC.predict(X_test)
cm = confusion_matrix(y_test, y_pred3)
print(cm)
accuracy_score(y_test, y_pred3)<jupyter_output>[[22  5]
 [ 7 27]]<jupyter_text>Random Forest Classifier<jupyter_code>from sklearn.ensemble import RandomForestClassifier
RFC = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
RFC.fit(X_train, y_train)
y_pred4 = RFC.predict(X_test)
cm = confusion_matrix(y_test, y_pred4)
print(cm)
accuracy_score(y_test, y_pred4)<jupyter_output>[[23  4]
 [ 5 29]]<jupyter_text>XGBoost Classifier<jupyter_code>from xgboost import XGBClassifier
XGBc = XGBClassifier()
XGBc.fit(X_train, y_train)
y_pred5 = XGBc.predict(X_test)
cm = confusion_matrix(y_test, y_pred5)
print(cm)
accuracy_score(y_test, y_pred5)<jupyter_output>[[21  6]
 [ 7 27]]<jupyter_text>We will be ensembling to get more precise result. Therefore, all te models are trained on the whole dataset  Ensembling  Combining the results of all the models to get the output<jupyter_code>yavg=[]
for i in range(len(y_pred1)):
  temp=y_pred1[i]+y_pred2[i]+y_pred3[i]+y_pred4[i]+y_pred5[i]
  temp=temp/5
  if temp>=0.5:
    yavg.append(1)
  else:
    yavg.append(0)

cm = confusion_matrix(y_test, yavg)
print(cm)
accuracy_score(y_test, yavg)<jupyter_output>[[21  6]
 [ 4 30]]<jupyter_text>Creating and Saving the Final Model for hosting in Heroku   Training KNN Classifier on the whole dataset<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
finalmodel1 = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
finalmodel1.fit(X, y)<jupyter_output><empty_output><jupyter_text>Training SVM Classifier on the whole dataset<jupyter_code>from sklearn.svm import SVC
finalmodel2 = SVC(kernel = 'linear', random_state = 0)
finalmodel2.fit(X, y)<jupyter_output><empty_output><jupyter_text>Training Decision Tree Classifier on the whole dataset<jupyter_code>from sklearn.tree import DecisionTreeClassifier
finalmodel3 = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
finalmodel3.fit(X, y)<jupyter_output><empty_output><jupyter_text>Training Random Forest Classifier on the whole dataset<jupyter_code>from sklearn.ensemble import RandomForestClassifier
finalmodel4 = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
finalmodel4.fit(X, y)<jupyter_output><empty_output><jupyter_text>Training XGBoost Classifier on the whole dataset<jupyter_code>from xgboost import XGBClassifier
finalmodel5 = XGBClassifier()
finalmodel5.fit(X, y)
import pickle
filename = 'model1.pkl'
pickle.dump(finalmodel1, open(filename, 'wb'))
filename = 'model2.pkl'
pickle.dump(finalmodel2, open(filename, 'wb'))
filename = 'model3.pkl'
pickle.dump(finalmodel3, open(filename, 'wb'))
filename = 'model4.pkl'
pickle.dump(finalmodel4, open(filename, 'wb'))
filename = 'model5.pkl'
pickle.dump(finalmodel5, open(filename, 'wb'))
filename = 'standard.pkl'
pickle.dump(sc, open(filename, 'wb'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12795125666412796, 'fraction_numerical': 0.02223914699162224, 'mean_word_length': 8.326704545454545, 'pattern_counts': {'":': 0, '<': 71, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '293725', 'license': 'MIT', 'n_tokens_mistral': 2630, 'n_tokens_neox': 2466, 'n_words': 485, 'path': 'Heart Disease.ipynb', 'repo_name': 'RahulJax/-Heart-Disease-Prediction', 'size': '6565'}