<jupyter_start><jupyter_text>Setup<jupyter_code># Python ≥3.5 is required
import sys
assert sys.version_info >= (3, 5)

# Scikit-Learn ≥0.20 is required
import sklearn
assert sklearn.__version__ >= "0.20"

# Common imports
import numpy as np
import os

# To plot pretty figures
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc('axes', labelsize=14)
mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)

# Where to save the figures
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "project1"
IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID)
os.makedirs(IMAGES_PATH, exist_ok=True)

def save_fig(fig_id, tight_layout=True, fig_extension="png", resolution=300):
    path = os.path.join(IMAGES_PATH, fig_id + "." + fig_extension)
    print("Saving figure", fig_id)
    if tight_layout:
        plt.tight_layout()
    plt.savefig(path, format=fig_extension, dpi=resolution)<jupyter_output><empty_output><jupyter_text>Get data<jupyter_code>import os
import urllib.request

dataset_url = "https://archive.ics.uci.edu/ml/machine-learning-databases/heart-disease/"
dataset_fname = "processed.cleveland.data"
dataset_path = os.path.join("datasets", "heart")

if not os.path.isdir(dataset_path):
        os.makedirs(dataset_path)
                
urllib.request.urlretrieve(dataset_url+dataset_fname, os.path.join(dataset_path, dataset_fname))
import pandas as pd

cols = ['age', 'sex', 'cp', 'trestbps', 'chol', 'fbs', 'restecg', 'thalach', 'exang', 'oldpeak', 'slope', 'ca', 'thal', 'num']

heart_data = pd.read_csv(os.path.join(dataset_path, dataset_fname), names=cols, header=None)
heart_data.tail()
heart_data.info()
heart_data = heart_data.replace('?', np.nan) #replaces symbol indicating it is not a numbe
heart_data["ca"] = pd.to_numeric(heart_data["ca"])
heart_data["thal"] = pd.to_numeric(heart_data["thal"])
heart_data.describe()
heart_data['cp'].value_counts()<jupyter_output><empty_output><jupyter_text>Set aside a test set<jupyter_code>from sklearn.model_selection import train_test_split

train_set, test_set = train_test_split(heart_data, test_size=0.2, random_state=42) #what proportion of the original data is analyzedfra
train_set.info()
heart_data.info()
test_set.head()
test_set['sex'].value_counts()
train_set['sex'].value_counts()
heart_data['sex'].value_counts()
from sklearn.model_selection import StratifiedShuffleSplit

split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)
for train_index, test_index in split.split(heart_data, heart_data['sex']):
    strat_train_set = heart_data.loc[train_index]
    strat_test_set = heart_data.loc[test_index]
strat_train_set['sex'].value_counts()
strat_test_set['sex'].value_counts() / len(strat_test_set)
heart_data['sex'].value_counts() / len(heart_data)<jupyter_output><empty_output><jupyter_text>Explore the data<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
heart_data.hist(bins=50, figsize=(20,15))
save_fig("attribute_histogram_plots")
plt.show()
heart_data.plot(kind="scatter",x="age", y="num", alpha=0.1)
save_fig("better_visualization_plot")<jupyter_output><empty_output><jupyter_text>Preparing the Data<jupyter_code>train_set.info()
clean_set = train_set.dropna()
clean_set.head()
from sklearn.compose import ColumnTransformer
passthrough = ['exang', 'chol','thalach']
pipeline = ColumnTransformer([
    ('passthrough','passthrough',passthrough)
])

X = pipeline.fit_transform(clean_set)
y = clean_set['num'] > 0
y<jupyter_output><empty_output><jupyter_text>Train Models<jupyter_code>from sklearn.linear_model import SGDClassifier
sgd_clf = SGDClassifier(random_state=42)
np.count_nonzero(sgd_clf.predict(X)==y)
sgd_clf.predict(X)==y
from sklearn.metrics import confusion_matrix
confusion_matrix(y,sgd_clf.predict(X))
from sklearn.model_selection import cross_val_score

cross_val_score(sgd_clf, X, y, cv=4,scoring='accuracy')
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix

y_predict = cross_val_predict(sgd_clf, X, y, cv=4)
confusion_matrix(y,y_predict)
confusion_matrix(y,y_predict)/np.size(y)
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import OneHotEncoder

passthrough = ['exang','ca',]
numerical_att = ['thalach',]
pipeline = ColumnTransformer([
    ('std_scalar', StandardScaler(), numerical_att),
    ('one_hot', OneHotEncoder(),['cp']),
    ('passthrough','passthrough')
])<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rant_clf = RandomForestClassifier(n_estimators=10)

from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix

y_predict = cross_val_predict(rant_clf, X, y, cv=4)
confusion_matrix(y,y_predict)
confusion_matrix(y,y_predict)/np.size(y)<jupyter_output><empty_output><jupyter_text>Support Vector Machine<jupyter_code>from sklearn.svm import SVC
svm_clf = SVC(random_state=42)
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix

y_predict = cross_val_predict(rant_clf, X, y, cv=4)
confusion_matrix(y,y_predict)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12591170825335893, 'fraction_numerical': 0.009213051823416507, 'mean_word_length': 8.174295774647888, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 36, 'https://': 1, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '483505', 'license': 'Apache-2.0', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1884, 'n_words': 329, 'path': 'projectOne.ipynb', 'repo_name': 'astridgutierrez/handson-ml2', 'size': '5210'}