In [1]:
import pandas as pd
heart_df = pd.read_csv("heart2.csv")
heart_df.head()
Out[1]:
age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal heart_disease
0 70 1 4 130 322 0 2 109 0 2.4 2 3 3 0
1 67 0 3 115 564 0 2 160 0 1.6 2 0 7 1
2 57 1 2 124 261 0 0 141 0 0.3 1 0 7 1
3 64 1 4 128 263 0 0 105 1 0.2 2 1 7 1
4 74 0 2 120 269 0 2 121 1 0.2 1 1 3 0
In [2]:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set()
sns.set_style('whitegrid')
sns.set_palette('Set1')
x0=heart_df[heart_df['heart_disease'] == 0]['age']
x1=heart_df[heart_df['heart_disease'] == 1]['age']
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.hist(x0, bins=50, normed=True, color='blue', alpha=0.6)
ax.hist(x1, bins=50, normed=True, color='red', alpha=0.6)
ax.set_title('Distribution of '+'age'+'(blue:normal ,red:heart disease)')
ax.set_xlabel('age')
plt.show()
In [3]:
x0=heart_df[heart_df['heart_disease'] == 0]['trestbps']
x1=heart_df[heart_df['heart_disease'] == 1]['trestbps']
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.hist(x0, bins=50, normed=True, color='blue', alpha=0.6)
ax.hist(x1, bins=50, normed=True, color='red', alpha=0.6)
ax.set_title('diagnosis of heart disease(blue:<50% diameter narrowing ,red:>50% diameter narrowing)')
ax.set_xlabel('trestbps')
plt.show()
In [4]:
for w in ['chol','thalach','oldpeak']:
    x0=heart_df[heart_df['heart_disease'] == 0][w]
    x1=heart_df[heart_df['heart_disease'] == 1][w]
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.hist(x0, bins=50, normed=True, color='blue', alpha=0.6)
    ax.hist(x1, bins=50, normed=True, color='red', alpha=0.6)
    ax.set_title('Distribution of '+ w +'(blue:normal ,red:heart disease)')
    ax.set_xlabel(w)
    plt.show()
In [5]:
cr1=pd.crosstab(heart_df['heart_disease'], heart_df['sex'])
print(cr1)

x = np.array(cr1)
female=x[:,0]
male=x[:,1]
print(female)
print(male)
sex             0    1
heart_disease         
0              74   78
1              13  105
[74 13]
[ 78 105]
In [6]:
sns.set()
sns.set_style('whitegrid')
sns.set_palette('Paired')
x = np.array(['normal', 'hdisease'])
x_position = np.arange(len(x))

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.bar(x_position, female, width=0.4, label='female')
ax.bar(x_position + 0.4, male, width=0.4, label='male')
ax.legend()
ax.set_xticks(x_position + 0.2)
ax.set_xticklabels(x)
plt.show()