<jupyter_start><jupyter_text>Importing Data<jupyter_code>df = pd.read_csv("C://Users/osmon/Documents/Heart Project/heart.csv")
df.head()
df.describe()<jupyter_output><empty_output><jupyter_text>Explaining the Variables  age sex chest pain type (4 values) resting blood pressure serum cholestoral in mg/dl fasting blood sugar > 120 mg/dl resting electrocardiographic results (values 0,1,2) maximum heart rate achieved exercise induced angina oldpeak = ST depression induced by exercise relative to rest the slope of the peak exercise ST segment 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)  Data Visualization  From The Variables, one of the most important to pay attention to would be the 'Target' as it indicates whether or not there is a presence of heart disease<jupyter_code>## Obtaining a count for the "Target" variable and visualizing the numbers of heart disease vs without heart disease

print('The counts of the Target variable:', df.target.value_counts())

sns.countplot(x="target", data=df, palette="rocket")
plt.title('Displaying the Counts of the Target Variable')
plt.show()
##Displaying the frequency distribution for presence/absence of heart disease for Sex
pd.crosstab(df.sex,df.target).plot(kind="bar",figsize=(20,10),color=['blue','green' ])
plt.title('Heart Disease Prsence / Absence Frequency for Sex')
plt.xlabel('Sex (0 = Female, 1 = Male)')
plt.xticks(rotation=0)
plt.legend(["Does Not Have Disease", "Have Disease"])
plt.ylabel('Frequency')
plt.show()<jupyter_output><empty_output><jupyter_text>There is a wide range of ages here, so let us see how heart disease vs no heart disease frequency is distributed across the ages<jupyter_code>##Plotting the frequency against the Age variable
pd.crosstab(df.age,df.target).plot(kind="bar",figsize=(20,10))
plt.title('Heart Disease Frequency for Ages')
plt.xlabel('Age')
plt.ylabel('Frequency')
plt.show()<jupyter_output><empty_output><jupyter_text>Exploring other variables<jupyter_code>#### The chest pain variable has different values
## Value 1: typical angina
## Value 2: atypical angina 
## Value 3: non-anginal pain 
## Value 4: asymptomatic)
##Plotting Heart Disease Frequency according to the different chest pain types
pd.crosstab(df.cp,df.target).plot(kind="bar",figsize=(20,10),color=['maroon','orange' ])
plt.title('Heart Disease Frequency By Chest Pain Type')
plt.xlabel('Chest Pain Type')
plt.xticks(rotation = 0)
plt.ylabel('Frequency')
plt.show()
##Plotting the scatterplot for the disease distribution against the various ages
plt.figure(figsize=(20,10))
plt.title("Scatterplot showing Disease Frequency Distribution")
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", "No Disease"])
plt.xlabel("Age")
plt.ylabel("Maximum Heart Rate")
plt.show()
## Plotting heatmap of our variables to show correlation

plt.rcParams['figure.figsize'] = (20, 10)
plt.style.use('ggplot')

sns.heatmap(df.corr(), annot = True, cmap = 'Blues')
plt.title('Heatmap for Variables', fontsize = 20)
plt.show()<jupyter_output><empty_output><jupyter_text>Exploring the FBS(Fasting Blood Sugar) Variable<jupyter_code>## Plotting the heart disease distrubution by the FBS
pd.crosstab(df.fbs,df.target).plot(kind="bar",figsize=(15,6),color=['blue','red' ])
plt.title('Heart Disease Frequency by FBS')
plt.xlabel('FBS - (Fasting Blood Sugar > 120 mg/dl) (1 = true; 0 = false)')
plt.xticks(rotation = 0)
plt.legend(["Does not Have Disease", "Have Disease"])
plt.ylabel('Frequency')
plt.show()
##Plotting the scatterplot for the disease distribution against the various Cholesterol levels
plt.figure(figsize=(20,10))
plt.title("Heart Disease Distribution against Cholesterol levels")
plt.scatter(x=df.chol[df.target==1], y=df.thalach[(df.target==1)], c="blue")
plt.scatter(x=df.chol[df.target==0], y=df.thalach[(df.target==0)])
plt.legend(["Disease", "No Disease"])
plt.xlabel("Cholesterol")
plt.ylabel("Maximum Heart Rate")
plt.show()
df.head()
scatters = df.drop(['trestbps','chol','thalach','target'], axis=1).columns
sns.set(font_scale=1.5)
#plt.rcParams.update({'font.size': 16})
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(20, 10))
fig.subplots_adjust(hspace=0.3, wspace=0.4)
sns.set_style(style='whitegrid')
for var, ax in zip(scatters, axes.flatten()):
    sns.scatterplot(x=var, y='thalach', data=df, ax=ax, color='darkslateblue')
    ax.set(ylabel='Max Heart Rate')
## The above plot did not show the desired outcome I had imagined, so I decided to take aa closer look at the plot using target as the hue
closer = ['age','cp']
sns.set(font_scale=1.5)
#plt.rcParams.update({'font.size': 16})
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20,10))
fig.subplots_adjust(hspace=0.2, wspace=0.4)
sns.set_style(style='whitegrid')
for var, ax in zip(closer, axes.flatten()):
    sns.scatterplot(x=var, y='thalach', hue = 'target', data=df, ax=ax)#, color='darkslateblue')
    ax.set(ylabel='Maximum Heart Rate)')<jupyter_output><empty_output><jupyter_text>Further Examining the Chest Pain variable<jupyter_code>sns.set(font_scale=1.5)
plt.figure(figsize=(20,10))
sns.set_style(style='whitegrid')
ax = sns.scatterplot(x='cp', y='thalach', hue = 'target', data=df)
ax.set(xlabel='CP', ylabel='Max Heart Rate')
plt.show()<jupyter_output><empty_output><jupyter_text>Building Logistic Regression Models<jupyter_code>#preparing the data
y = df.target.values
x_data = df.drop(['target'], axis = 1)
print("Shape of y:", y.shape)
#normalizing the data 
x = (x_data - np.min(x_data)) / (np.max(x_data) - np.min(x_data)).values
#splitting the data into dependent and independent variables 
#split to that 80% of our data is trainijg data and 20% is test data
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 0)
#transposing matrices
x_train = x_train.T
y_train = y_train.T
x_test = x_test.T
y_test = y_test.T<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn import metrics
logreg = LogisticRegression()
test_accuracy = {}

logreg = LogisticRegression()
logreg.fit(x_train.T,y_train.T)
acc = logreg.score(x_test.T,y_test.T)*100

test_accuracy['Logistic Regression'] = acc
print("Logistic Regression Test Accuracy is: {:.2f}%".format(acc))<jupyter_output>Logistic Regression Test Accuracy is: 83.61%<jupyter_text>The Logistic Regression showed a score of 83.61  Naive Bayes Classifier<jupyter_code>from sklearn.naive_bayes import GaussianNB
nb = GaussianNB()
nb.fit(x_train.T, y_train.T)

#to get the metrics
acc = nb.score(x_test.T,y_test.T)*100
test_accuracy['Naive Bayes'] = acc
print("Naive Bayes Classifier Test Accuracy is: {:.2f}%".format(acc))<jupyter_output>Naive Bayes Classifier Test Accuracy is: 85.25%<jupyter_text>The Naive Bayes Classifier Test Accuracy is: 85.25%  KNN Classifier<jupyter_code>from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors = 2)  
knn.fit(x_train.T, y_train.T)
prediction = knn.predict(x_test.T)

knn_sc = (knn.score(x_test.T, y_test.T)*100)
print("The Accuracy of the KNN Classifier is: {:.2f}% ".format(knn_sc))<jupyter_output>The Accuracy of the KNN Classifier is: 83.61%<jupyter_text>Optimizing K value to see best KNN result<jupyter_code># optimizing k value
scores = []
for i in range(1,20):
    knn2 = KNeighborsClassifier(n_neighbors = i)  # n_neighbors means k
    knn2.fit(x_train.T, y_train.T)
    scores.append(knn2.score(x_test.T, y_test.T))
    
plt.plot(range(1,20), scores)
plt.xticks(np.arange(1,20,1))
plt.xlabel("K value")
plt.ylabel("Score")
plt.title("Optimizing K Value")
plt.show()

acc = max(scores)*100
test_accuracy['KNN'] = acc
print("Optimal value of KNN Score is {:.2f}%".format(acc))<jupyter_output><empty_output><jupyter_text>Point of Elbow seen at k = 7, making k=7 the Optimal value of k<jupyter_code># This is to split the data into train, validation, and test data
x_train, x_test, y_train, y_test = train_test_split(df.drop(['target','thalach'], axis=1),
                                                    df['target'],
                                                    train_size = 0.50,
                                                    random_state=11)
x_val, x_test, y_val, y_test = train_test_split(x_test, 
                                                  y_test, 
                                                  train_size=0.50, 
                                                  random_state=11)
model = DecisionTreeRegressor()
# fit the model
model.fit(x_train, y_train)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
    print('Feature: {} - Score: {}'.format(x_train.columns[i],round(v,3)))
# plot feature importance
plt.rcParams.update({'font.size': 16})
plt.figure(figsize=(20, 10))
plt.bar([x for x in range(len(importance))], importance)
plt.xticks(ticks=np.arange(12), labels=x_train.columns, size=14)
plt.xlabel('variables', size=14)
plt.ylabel('importance', size=14)
plt.title('Showing the most important Features that Contributed the the Classification of Heart Disease')
plt.show()<jupyter_output>Feature: age - Score: 0.116
Feature: sex - Score: 0.025
Feature: cp - Score: 0.123
Feature: trestbps - Score: 0.012
Feature: chol - Score: 0.098
Feature: fbs - Score: 0.0
Feature: restecg - Score: 0.018
Feature: exang - Score: 0.0
Feature: oldpeak - Score: 0.172
Feature: slope - Score: 0.008
Feature: ca - Score: 0.097
Feature: thal - Score: 0.332<jupyter_text>It can be observed that cp, chol, oldpeak, ca, thal are the most important features  After evalutaion the different models, we must now pick which model would be the most ideal for out data.- This will be done using the accuracy Metric- A bar chart will be used to illustrate and allow us to visualize how the accuracies measure up<jupyter_code>#plotting the test_accuracy set

sns.set_style("whitegrid")
plt.figure(figsize=(20,10))
plt.yticks(np.arange(0,100,10))
plt.ylabel("Accuracy %")
plt.xlabel("Model")
sns.barplot(x=list(test_accuracy.keys()), y=list(test_accuracy.values()), palette='rocket')
plt.title("Plotting the Test Accuracy Results")
plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup