<jupyter_start><jupyter_text>Predicting Heart Disease<jupyter_code>import pandas
import matplotlib.pyplot as plot
from visuals import disease_stats
import numpy
%matplotlib inline
dataframe = pandas.read_csv('cleveland.csv')
dataframe.shape
dataframe.head(5)<jupyter_output><empty_output><jupyter_text>Check for null values<jupyter_code>dataframe.isnull().values.any()
print("Size before Dropping Rows with Missing Values:", len(dataframe))
dataframe = dataframe.dropna()
print("Size after Dropping Rows with Missing Values:", len(dataframe))<jupyter_output>Size after Dropping Rows with Missing Values: 297<jupyter_text>Only predict If they will or will not have heart disease<jupyter_code>dataframe.loc[dataframe['num'] != 0, 'num'] = 1<jupyter_output><empty_output><jupyter_text>Dataset Features<jupyter_code>figure, axes = plot.subplots(nrows=7, ncols=2, figsize=(20,50) )
plot.suptitle("Heart Disease Data", fontsize=20)

axes[0,0].hist(dataframe.age)
axes[0,0].set_xlabel("Age (years)")
axes[0,0].set_ylabel("Number of Patients")

axes[0,1].hist(dataframe.sex)
axes[0,1].set_xlabel("Sex (0=female,1=male)")
axes[0,1].set_ylabel("Number of Patients")

axes[1,0].hist(dataframe.cp, bins=4, range=(0.5,4.5), rwidth=0.80)
axes[1,0].set_xlim(0.0,5.0)
axes[1,0].set_xlabel("Type of Chest Pain [cp]")
axes[1,0].set_ylabel("Number of Patients")

axes[1,1].hist(dataframe.trestbps)
axes[1,1].set_xlabel("Resting Blood Pressure [trestbps]")
axes[1,1].set_ylabel("Number of Patients")

axes[2,0].hist(dataframe.chol)
axes[2,0].set_xlabel("Serum Cholesterol [chol]")
axes[2,0].set_ylabel("Number of Patients")

axes[2,1].hist(dataframe.fbs)
axes[2,1].set_xlabel("Fasting Blood Sugar [fbs]")
axes[2,1].set_ylabel("Number of Patients")

axes[3,0].hist(dataframe.restecg)
axes[3,0].set_xlabel("Resting Electrocardiography [restecg]")
axes[3,0].set_ylabel("Number of Patients")

axes[3,1].hist(dataframe.thalach)
axes[3,1].set_xlabel("Maximum Heart Rate Achieved [thalach]")
axes[3,1].set_ylabel("Number of Patients")

axes[4,0].hist(dataframe.exang)
axes[4,0].set_xlabel("Exercise Induced Angina [exang]")
axes[4,0].set_ylabel("Number of Patients")

axes[4,1].hist(dataframe.oldpeak)
axes[4,1].set_xlabel("Exercise Induced ST Depression [oldpeak]")
axes[4,1].set_ylabel("Number of Patients")

axes[5,0].hist(dataframe.slope)
axes[5,0].set_xlabel("Slope of Peak Exercise ST Segment [slope]")
axes[5,0].set_ylabel("Number of Patients")

axes[5,1].hist(dataframe.ca,bins=4,range=(-0.5,3.5),rwidth=0.8)
axes[4,1].set_xlim(-0.7,4.7)
axes[5,1].set_xlabel("Major Vessels colored by Fluoroscopy [ca]")
axes[5,1].set_ylabel("Number of Patients")

axes[6,0].hist(dataframe.thal)
axes[6,0].set_xlabel("Thal")
axes[6,0].set_ylabel("Number of Patients")

axes[6,1].hist(dataframe.num,bins=5,range=(-0.5,4.5),rwidth=0.8)
axes[6,1].set_xlabel("Heart Disease [num]")
axes[6,1].set_ylabel("Number of Patients")
predictions = dataframe.num
features = dataframe.drop('num', axis=1)
print("Cleveland dataset now has {} data points with {} variables each.".format(*dataframe.shape))<jupyter_output>Cleveland dataset now has 297 data points with 14 variables each.<jupyter_text>Correlation<jupyter_code>dataframe.corr()
def plot_correlation(dataframe, size=10):
    corr = dataframe.corr() # Data Frame correlation function
    figure, axes = plot.subplots(figsize=(size, size))
    axes.matshow(corr) # Color code the rectangles by correlation value
    plot.xticks(range(len(corr.columns)), corr.columns) # Draw x tick marks
    plot.yticks(range(len(corr.columns)), corr.columns) # Draw y tick marks
plot_correlation(dataframe)
for key in features.columns.values:
    disease_stats(features, predictions, key)
from sklearn.cross_validation import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(features, predictions, test_size=0.30, random_state=42)<jupyter_output>C:\Users\Nikhil\Anaconda3\lib\site-packages\sklearn\cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)<jupyter_text>Logistic Regression<jupyter_code>import itertools
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import cross_val_score

best = {"mean_score": 0}
for length in range(9, 14):
    for columns in itertools.combinations(features.columns.values, length):
        model = LogisticRegression(penalty="l1",C=0.8, random_state=37)
        scores = cross_val_score(model, features[list(columns)], predictions, cv=10, scoring='accuracy')
        result = {"columns": columns, "mean_score": scores.mean(), "scores": scores}
        if best["mean_score"] < result["mean_score"]:
            best = result
print(best)
columns = list(best['columns'])
model = LogisticRegression(penalty="l1",C=4.2, random_state=37)
accuracies = cross_val_score(model, features[list(columns)], predictions, cv=10, scoring='accuracy')
fscores = cross_val_score(model, features[list(columns)], predictions, cv=10, scoring='f1')
print("\nBest Results using:")
print(columns)

print(accuracies)
print(accuracies.mean())
print(fscores)
print(fscores.mean())<jupyter_output>Best Results using:
['sex', 'trestbps', 'chol', 'fbs', 'thalach', 'exang', 'oldpeak', 'ca', 'thal']
[ 0.9         0.83333333  0.86666667  0.93333333  0.86666667  0.73333333
  0.76666667  0.89655172  0.79310345  0.86206897]
0.845172413793
[ 0.88        0.81481481  0.84615385  0.92857143  0.85714286  0.73333333
  0.75862069  0.88888889  0.75        0.84615385]
0.830367970471<jupyter_text>Tuning Params<jupyter_code>from sklearn.model_selection import GridSearchCV
c_values = numpy.arange(0.1,10,0.1).tolist()
param_grid = dict(C=c_values)
print(param_grid)
lr = LogisticRegression(penalty="l1",C=0.8, random_state=37)
grid = GridSearchCV(lr, param_grid, cv=10, scoring='accuracy', n_jobs=-1)
columns = ['age', 'sex', 'cp', 'chol', 'thalach', 'exang', 'oldpeak', 'ca', 'thal']

grid.fit(features[columns], predictions)
grid.cv_results_
plot.plot(c_values, grid.cv_results_['mean_test_score']*100)
plot.xlabel("C Val")
plot.ylabel("Accuracy")
grid.best_score_
grid.best_params_
grid.best_estimator_<jupyter_output><empty_output><jupyter_text>Naive Bayes<jupyter_code>from sklearn.naive_bayes import GaussianNB
best = {"mean_score": 0}
for length in range(9, 14):
    for columns in itertools.combinations(features.columns.values, length):
        model = GaussianNB()
        scores = cross_val_score(model, features[list(columns)], predictions, cv=10, scoring='accuracy')
        result = {"columns": columns, "mean_score": scores.mean(), "scores": scores}
        if best["mean_score"] < result["mean_score"]:
            best = result
print(best)<jupyter_output>{'columns': ('sex', 'cp', 'trestbps', 'thalach', 'exang', 'oldpeak', 'slope', 'ca', 'thal'), 'mean_score': 0.85839080459770112, 'scores': array([ 0.83333333,  0.86666667,  0.93333333,  0.96666667,  0.8       ,
        0.83333333,  0.83333333,  0.89655172,  0.75862069,  0.86206897])}<jupyter_text>SVM<jupyter_code>from sklearn import svm
import itertools
from sklearn.cross_validation import cross_val_score

best = {"mean_score": 0}
for length in range(9, 14):
    for columns in itertools.combinations(features.columns.values, length):
        model = svm.SVC()
        scores = cross_val_score(model, features[list(columns)], predictions, cv=10, scoring='accuracy')
        result = {"columns": columns, "mean_score": scores.mean(), "scores": scores}
        if best["mean_score"] < result["mean_score"]:
            best = result
print(best)<jupyter_output>{'mean_score': 0.81816091954022985, 'scores': array([ 0.83333333,  0.86666667,  0.83333333,  0.96666667,  0.8       ,
        0.76666667,  0.66666667,  0.82758621,  0.75862069,  0.86206897]), 'columns': ('sex', 'cp', 'fbs', 'restecg', 'exang', 'oldpeak', 'slope', 'ca', 'thal')}<jupyter_text>Neural Network<jupyter_code>from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
import itertools
from sklearn.cross_validation import cross_val_score

#best = {"mean_score": 0}
#for length in range(9, 14):
#for columns in itertools.combinations(features.columns.values, length):
columns = ('sex', 'cp', 'trestbps', 'thalach', 'exang', 'oldpeak', 'slope', 'ca', 'thal')
model = MLPClassifier(solver='lbfgs', alpha=1e-6, hidden_layer_sizes=(13,20), random_state=1)
scores = cross_val_score(model, features[list(columns)], predictions, cv=10, scoring='accuracy')
result = {"columns": columns, "mean_score": scores.mean(), "scores": scores}
print(result)<jupyter_output>{'mean_score': 0.84839080459770122, 'scores': array([ 0.86666667,  0.83333333,  0.86666667,  0.96666667,  0.86666667,
        0.83333333,  0.73333333,  0.93103448,  0.72413793,  0.86206897]), 'columns': ('sex', 'cp', 'trestbps', 'thalach', 'exang', 'oldpeak', 'slope', 'ca', 'thal')}
{'fraction_non_alphanumeric': 0.15406624848684936, 'fraction_numerical': 0.07912402333003192, 'mean_word_length': 6.388617886178862, 'pattern_counts': {'":': 16, '<': 37, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 15, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '285060', 'license': 'MIT', 'n_tokens_mistral': 3783, 'n_tokens_neox': 3317, 'n_words': 666, 'path': 'notebooks/Heart Disease.ipynb', 'repo_name': 'nikhil-pandey/fyp-ml', 'size': '9087'}