<jupyter_start><jupyter_text>Find the empty value in a column<jupyter_code>dataset.isnull().sum()<jupyter_output><empty_output><jupyter_text>Change the value target from 0, 1, 2, 3, 4 to 0 or 1. In other word simplify the dataset whether the patient have a heart disease or not<jupyter_code>dataset['target'] = dataset['target'].map({0: 0, 1: 1, 2: 1, 3: 1, 4: 1})<jupyter_output><empty_output><jupyter_text>Fill the empty value in thal column with sum method<jupyter_code>dataset['thal'] = dataset['thal'].fillna(dataset['thal'].mean())<jupyter_output><empty_output><jupyter_text>Fill the empty value in thal column with sum method<jupyter_code>dataset['ca'] = dataset['ca'].fillna(dataset['ca'].mean())<jupyter_output><empty_output><jupyter_text>EDA Age<jupyter_code>plt.figure(figsize = (16, 8))
sns.countplot(x = 'age', data = dataset, hue = 'target', palette = 'pastel')<jupyter_output><empty_output><jupyter_text>Gender<jupyter_code>gender = dataset['sex'].map({0: 'female', 1: 'male'})
plt.figure(figsize = (16, 8))
sns.countplot(x = gender ,hue = dataset['target'], palette = 'pastel')<jupyter_output><empty_output><jupyter_text>Maximum Heart Rate vs AgeFrom the scatter plot below, we can say that most of the patient age between 50 to 60 have a higher chance getting a CVD<jupyter_code>plt.figure(figsize = (16, 10))

sns.scatterplot(x = 'age', y = 'thalach', data = dataset, hue = 'target', palette = 'pastel', s = 95)

plt.ylabel('maximum heart rate')<jupyter_output><empty_output><jupyter_text>Export Cleaned Dataset<jupyter_code>dataset.to_csv('../datasets/cleaned_cleveland.csv', index = False)<jupyter_output><empty_output><jupyter_text>Data Pre-processing Split the dataset into train and test<jupyter_code>from sklearn.model_selection import train_test_split
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].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>Apply standard scaler<jupyter_code>from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>Import KNN from SKLearn<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
sns.pairplot(dataset, hue = 'target', diag_kind = 'hist', palette = 'pastel')<jupyter_output><empty_output><jupyter_text>Finding the best K Value<jupyter_code>error_rate = []

for i in range(1,40):
    
    knn = KNeighborsClassifier(n_neighbors = i)
    knn.fit(X_train,y_train)
    pred_i = knn.predict(X_test)
    error_rate.append(np.mean(pred_i != y_test))
plt.figure(figsize=(10,6))
plt.plot(range(1,40),error_rate,color = 'red', linestyle = 'dashed', marker = 'o', markersize = 10)
plt.xlabel('K Value')
plt.ylabel('Error Rate')<jupyter_output><empty_output><jupyter_text>Applying KNN to the dataset<jupyter_code>classifier = KNeighborsClassifier(n_neighbors = 21)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)<jupyter_output><empty_output><jupyter_text>Accuracy of the prediction<jupyter_code>from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
from sklearn.decomposition import PCA
from mlxtend.plotting import plot_decision_regions
pca = PCA(n_components = 2)
X_trains = pca.fit_transform(X_train)
classifier.fit(X_trains, y_train)
plot_decision_regions(X_trains, y_train, clf = classifier)
plt.xlabel('X')
plt.ylabel('y')
plt.title('KNN with 21 as K Value')
plt.show()<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>from sklearn.tree import DecisionTreeClassifier
dtree = DecisionTreeClassifier()
dtree.fit(X_train, y_train)
from IPython.display import Image
from sklearn import tree
import pydotplus
features = list(dataset.drop('target', axis = 1))
features
result = dataset['target'].map({0: 'Negative', 1: 'Positive'})
dot_data = tree.export_graphviz(dtree, out_file = None, feature_names = features, class_names = result)
graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())
graph.write_png("Target.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13019719648372535, 'fraction_numerical': 0.014017581373247802, 'mean_word_length': 8.273127753303966, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '430679', 'license': 'CC0-1.0', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1502, 'n_words': 307, 'path': 'classification/Exploratory Data Analysis.ipynb', 'repo_name': 'DanishDanialZurkanain/Cardiovascular', 'size': '4209'}