<jupyter_start><jupyter_text>MINI PROJECT ON HEART DISEASE PREDICTION our "goal" predict the presence of heart disease in the patient **bold text**  Importing Library for Operations<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import itertools
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression 
from sklearn.ensemble import GradientBoostingClassifier 
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn import tree

from sklearn.metrics import classification_report
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve
from sklearn import metrics
from sklearn.metrics import confusion_matrix
import seaborn as sns
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Import and read our DataSet<jupyter_code>df = pd.read_csv('/content/heart.csv')
#print first 10 rows
df.head(10)<jupyter_output><empty_output><jupyter_text>Describe  dataset<jupyter_code>df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 303 entries, 0 to 302
Data columns (total 14 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   age       303 non-null    int64  
 1   sex       303 non-null    int64  
 2   cp        303 non-null    int64  
 3   trestbps  303 non-null    int64  
 4   chol      303 non-null    int64  
 5   fbs       303 non-null    int64  
 6   restecg   303 non-null    int64  
 7   thalach   303 non-null    int64  
 8   exang     303 non-null    int64  
 9   oldpeak   303 non-null    float64
 10  slope     303 non-null    int64  
 11  ca        303 non-null    int64  
 12  thal      303 non-null    int64  
 13  target    303 non-null    int64  
dtypes: float64(1), int64(13)
memory usage: 33.3 KB<jupyter_text>**we can see that we have a small 303 rows data set. our data has no nulls and no other chars to represent it. all our data is numeric - therefore, no enumeration needed** **So here we getting the people count who have heart deacise and who have chance for heart deacise**<jupyter_code>healthy = df[(df['target'] ==0) ].count()[1]
sick = df[(df['target'] ==1) ].count()[1]
print ("Number of pepole without heart deacise: "+ str(healthy))
print ("Number of pepole with chance for heart deacise: "+ str(sick))<jupyter_output>Number of pepole without heart deacise: 138
Number of pepole with chance for heart deacise: 165<jupyter_text>we will nurmaize the data and split it to test and train.--- we choose to splite 30-70 because we have a small data set and we want to have enught validetionn examples. split data table into data X and class labels y<jupyter_code>X = df.iloc[:,0:13].values
y = df.iloc[:,13].values<jupyter_output><empty_output><jupyter_text>nurmalize the data<jupyter_code>from sklearn.preprocessing import StandardScaler
X_std = StandardScaler().fit_transform(X)
dfNorm = pd.DataFrame(X_std, index=df.index, columns=df.columns[0:13])<jupyter_output><empty_output><jupyter_text>Add non-feature target column to dataframe<jupyter_code>dfNorm['target'] = df['target']
dfNorm.head(10)

X = dfNorm.iloc[:,0:13].values
y = dfNorm.iloc[:,13].values<jupyter_output><empty_output><jupyter_text>Here we using the sklearn.model_selection for analyze data to new structre data<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=0)

X_train.shape, y_train.shape, X_test.shape , y_test.shape<jupyter_output><empty_output><jupyter_text>calculate the correlation matrix<jupyter_code>corr = dfNorm.corr()

# plot the heatmap
fig = plt.figure(figsize=(5,4))
sns.heatmap(corr, 
        xticklabels=corr.columns,
        yticklabels=corr.columns,
            linewidths=.75)<jupyter_output><empty_output><jupyter_text>Here we are using Some Helping Fuctions for predicting and visualization<jupyter_code>results_test = {}
results_train = {}
list_algos=[]
def prdict_date(algo_name,X_train,y_train,X_test,y_test,atype='',verbose=0):
    algo_name.fit(X_train, y_train)
    Y_pred = algo_name.predict(X_test)
    acc_train = round(algo_name.score(X_train, y_train) * 100, 2)
    acc_val = round(algo_name.score(X_test, y_test) * 100, 2)
    
    results_test[str(algo_name)[0:str(algo_name).find('(')]+'_'+str(atype)] = acc_val
    results_train[str(algo_name)[0:str(algo_name).find('(')]+'_'+str(atype)] = acc_train
    list_algos.append(str(algo_name)[0:str(algo_name).find('(')])
    if verbose ==0:
        print("acc train: " + str(acc_train))
        print("acc test: "+ str(acc_val))
    else:
        return Y_pred
def print_fitures(sfs_name='sfs1',verbose=0):
    a= (sfs_name.k_feature_idx_[0],sfs_name.k_feature_idx_[1],sfs_name.k_feature_idx_[2])
    if verbose ==0:
        print('Selected features:', sfs_name.k_feature_idx_)
        for i in range (len (sfs_name.k_feature_idx_)):
            print (df.iloc[:,sfs_name.k_feature_idx_[i]].name)
    return a
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import StratifiedKFold
from scipy import interp

def roc_graph_cv(algo_name,X,y,cvn=5):
    # Run classifier with cross-validation and plot ROC curves
    cv = StratifiedKFold(n_splits=cvn)
    classifier =algo_name

    tprs = []
    aucs = []
    mean_fpr = np.linspace(0, 1, 100)

    i = 0
    for train, test in cv.split(X, y):
        probas_ = classifier.fit(X[train], y[train].ravel()).predict_proba(X[test])
        # Compute ROC curve and area the curve
        fpr, tpr, thresholds = roc_curve(y[test].ravel(), probas_[:, 1])
        tprs.append(interp(mean_fpr, fpr, tpr))
        tprs[-1][0] = 0.0
        roc_auc = auc(fpr, tpr)
        aucs.append(roc_auc)
        plt.plot(fpr, tpr, lw=1, alpha=0.3,
                 label='ROC fold %d (AUC = %0.2f)' % (i, roc_auc))

        i += 1
    plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r',
             label='Luck', alpha=.8)

    mean_tpr = np.mean(tprs, axis=0)
    mean_tpr[-1] = 1.0
    mean_auc = auc(mean_fpr, mean_tpr)
    std_auc = np.std(aucs)
    plt.plot(mean_fpr, mean_tpr, color='b',
             label=r'Mean ROC (AUC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc),
             lw=2, alpha=.8)

    std_tpr = np.std(tprs, axis=0)
    tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
    tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
    plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2,
                     label=r'$\pm$ 1 std. dev.')

    plt.xlim([-0.05, 1.05])
    plt.ylim([-0.05, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Receiver operating characteristic example')
    plt.legend(loc="lower right")
    plt.show()<jupyter_output><empty_output><jupyter_text>here we create the visualization for predicted and true class<jupyter_code>def conf(algo_name,X_test, y_test):
    y_pred = algo_name.predict(X_test)
    forest_cm = metrics.confusion_matrix(y_pred, y_test, [1,0])
    sns.heatmap(forest_cm, annot=True, fmt='.2f',xticklabels = ["1", "0"] , yticklabels = ["1", "0"] )
    plt.ylabel('True class')
    plt.xlabel('Predicted class')
    plt.title(str(algo_name)[0:str(algo_name).find('(')])<jupyter_output><empty_output><jupyter_text>Principal component analysis (PCA) is a technique used to emphasize variation and bring out strong patterns in a dataset.**we use it first make data easy to explore and visualize.**<jupyter_code>from sklearn.decomposition import PCA
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(X)
principalDf = pd.DataFrame(data = principalComponents
             , columns = ['principal component 1', 'principal component 2'])
finalDf = pd.concat([principalDf, df[['target']]], axis = 1)

fig = plt.figure(figsize = (8,6))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)
targets = [1,0]
colors = ['r',  'b']
for target, color in zip(targets,colors):
    indicesToKeep = finalDf['target'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'principal component 1']
               , finalDf.loc[indicesToKeep, 'principal component 2']
               , c = color
               , s = 50)
ax.legend(targets)
ax.grid()
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
lda = LinearDiscriminantAnalysis()
prdict_date(lda,X_train,y_train,X_test,y_test)<jupyter_output>acc train: 85.38
acc test: 80.22<jupyter_text>RANDOM FOREST<jupyter_code># Train: Random Forest
random_forest = RandomForestClassifier(n_estimators=50, random_state = 0)
prdict_date(random_forest,X_train,y_train,X_test,y_test)<jupyter_output>acc train: 100.0
acc test: 84.62<jupyter_text>Here we plot te graph for feature_importance<jupyter_code>feature_importance = random_forest.feature_importances_
feat_importances = pd.Series(random_forest.feature_importances_, index=df.columns[:-1])
feat_importances = feat_importances.nlargest(13)

feature = df.columns.values.tolist()[0:-1]
importance = sorted(random_forest.feature_importances_.tolist())


x_pos = [i for i, _ in enumerate(feature)]

plt.barh(x_pos, importance , color='dodgerblue')
plt.ylabel("feature")
plt.xlabel("importance")
plt.title("feature_importances")

plt.yticks(x_pos, feature)

plt.show()<jupyter_output><empty_output><jupyter_text>getting  all histograms with its results<jupyter_code>sns.set(font_scale=1.5)
df.hist(edgecolor='black', linewidth=1.2, figsize=(20, 20));<jupyter_output><empty_output><jupyter_text>Histogram for Age only<jupyter_code>df['age'].plot.hist()<jupyter_output><empty_output><jupyter_text>Visualization for Chest Pain<jupyter_code>df["cp"].value_counts()
# "cp:chest pain" - 4 types counts
sns.countplot(x="cp",data=df)<jupyter_output><empty_output><jupyter_text>Chest pain catagorization in male and Female<jupyter_code>df["sex"].value_counts()
# 1 - Male , 0 - Female
# "cp:chest pain" - 4 types counts as per "sex(1-male, 0-Female)"
sns.countplot(x="cp", hue="sex",data=df)<jupyter_output><empty_output><jupyter_text>Prediction chart as per age<jupyter_code># "cp:chest pain" - 4 types counts as per "age"
sns.countplot(x="cp", hue="age",data=df)<jupyter_output><empty_output><jupyter_text>Data Cleaning for Structure Dataset<jupyter_code># check if any dataset contain missing values
df.isnull().sum()
df.dropna(inplace=True)
df.isnull().sum()
df.shape<jupyter_output><empty_output><jupyter_text>Value counts in columns<jupyter_code>df["age"].value_counts()
df["fbs"].value_counts()
df["target"].value_counts()<jupyter_output><empty_output><jupyter_text>Dummy values to convert categorial value using pandas<jupyter_code>df = pd.get_dummies(df,columns= ["cp","restecg"])
df.head()<jupyter_output><empty_output><jupyter_text>here we are using categorials aggregated into groups rather than being in numeric format<jupyter_code>data_numericals = ["age","sex","trestbps","chol","thalach","oldpeak"]
categorials = list(set(df.columns)- set(data_numericals) -{"target"})
df.head()
categorials

data_numericals<jupyter_output><empty_output><jupyter_text>Splitting Data into features and target<jupyter_code>#droping operation performed
x = df.drop(columns='target',axis=1)
y = df['target']
x_train, x_test, y_train, y_test = train_test_split( x, y, test_size=0.2, random_state=0)
len(x_train)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)<jupyter_output>(237, 18) (60, 18) (237,) (60,)<jupyter_text>Here we import the KNeighborsClassifier for represents the k nearest neighbors, where k is an integer value specified by the user<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
neighbour = KNeighborsClassifier(n_neighbors = 4).fit(x_train,y_train)
x_preaditon = neighbour.predict(x_test)
prediction = pd.DataFrame(x_preaditon)
prediction.head()
from sklearn import metrics
metrics.accuracy_score(y_test, prediction)<jupyter_output><empty_output><jupyter_text>Using SVM Algorithm (Support Vector Machine )is one of the most popular Supervised Learning algorithms, which is used for Classification as well as Regression problems.<jupyter_code># Now Using SVM Algorithm 
from sklearn.svm import SVC
classifier = SVC(kernel='rbf',random_state=0)
classifier.fit(x_train,y_train)
#Predict
y_preaditon = classifier.predict(x_test)
prediction2 = pd.DataFrame(y_preaditon)
prediction2.head()
#Now check the Accuracy
metrics.accuracy_score(y_test, prediction2)
#Importing confusion_matrix
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test,prediction)
confusion_matrix(y_test,prediction2)<jupyter_output><empty_output><jupyter_text>**Decision Tree Classifier, repetitively divides the working area (plot) into sub part by identifying lines**<jupyter_code>#Using DecisionTreeClassifier
from sklearn.tree import DecisionTreeClassifier
HeartTree = DecisionTreeClassifier(criterion="entropy", max_depth = 4)
HeartTree.fit(x_train,y_train)<jupyter_output><empty_output><jupyter_text>Here we got Final predictions<jupyter_code>y_preaditon = HeartTree.predict(x_test)
prediction3 = pd.DataFrame(y_preaditon)
prediction3.head()
metrics.accuracy_score(y_test,prediction3)
confusion_matrix(y_test,prediction3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11918756044937133, 'fraction_numerical': 0.023807752399375045, 'mean_word_length': 5.257914338919925, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '28', 'dir': 'jupyter-structured-clean-dedup', 'id': '548224', 'license': 'MIT', 'n_tokens_mistral': 5087, 'n_tokens_neox': 4761, 'n_words': 1058, 'path': 'Heart_Disease_Predictions.ipynb', 'repo_name': 'prernask/Heart_Disease_Prediction', 'size': '13441'}