<jupyter_start><jupyter_text>Lab 2b: Heart Disease / Scikit-LearnFollow the steps to load the heart disease database and to train & score various classifiers with it.The database is a modified version from the [UCI Machine Learning Repository](https://archive.ics.uci.edu/ml/datasets/heart+Disease):* Lines with missing values have been removed.* The predicted attribute in column 14 (58) - diagnosis of heart disease (angiographic disease status) has been simplified to two classes:  * 0... absence  * 1... presence  Your NamePlease enter your name in the next line (Markdown style cell): <NAME> (dh181812)  Exercise 1: Read about the DatasetCheck out the dataset description of [UCI](https://archive.ics.uci.edu/ml/datasets/heart+Disease). Our dataset is the Cleveland set and uses the smaller set of 14 attributes. Take a look at column 3 ("chest pain"). The values are either 1, 2, 3 or 4 in the dataset. Answer the following question in the next line as text (Markdown style cell): What types of chest pain do those four possible values map to?*Hint: you'll find the answer in the "Attribute information" section of the linked web page.* Value 1: typical angina, Value 2: atypical angina, Value 3: non-anginal pain, Value 4: asymptomatic   Exercise 2: ImportsUse the following block for all the imports you need for your notebook.This will include pandas, numpy, seaborn and all the parts of sklearn.Also configure matplotlib to draw graphics inline.<jupyter_code># Numpy
import numpy as np
# Pandas
import pandas as pd
# Seaborn
import seaborn as sns
# Matplotlib
%matplotlib inline
# Scikit learn modules
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn.neural_network import MLPClassifier<jupyter_output><empty_output><jupyter_text>Exercise 3: Load the CSVLoad the heart-disease-fhstp.csv file and print its head to check if importing worked.<jupyter_code># load csv file
df = pd.read_csv("heart-disease-fhstp.csv")
# print head of csv file
df.head()<jupyter_output><empty_output><jupyter_text>Exercise 4: DescribeUse the describe function of the Pandas DataFrame to find out the count, mean, standard deviation & more from the dataset for each column.<jupyter_code>df.describe()<jupyter_output><empty_output><jupyter_text>Exercise 5: Age HistogramPrint the histogram of the "age" column. Use 20 bins. This gives you a good understanding of the patients that were tested for heart diseases.<jupyter_code>df.age.hist(bins=20)<jupyter_output><empty_output><jupyter_text>Exercise 6: Violin PlotUse the Violin Plot of Seaborn to print the target class ("diameter narrowing") on the x axis and "max XR" on the y axis.<jupyter_code>sns.violinplot(x="diameter narrowing", y="max HR", data=df)<jupyter_output>C:\Users\Marcel\Anaconda3\lib\site-packages\scipy\stats\stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval<jupyter_text>Exercise 7: Pair PlotDraw a pair plot of the whole dataframe. Use the target class for the hue.Note that many of the values are mapped to different observations (-> categorical features), while others are numeric features.<jupyter_code>sns.pairplot(df, hue="diameter narrowing")<jupyter_output>C:\Users\Marcel\Anaconda3\lib\site-packages\statsmodels\nonparametric\kde.py:488: RuntimeWarning: invalid value encountered in true_divide
  binned = fast_linbin(X, a, b, gridsize) / (delta * nobs)
C:\Users\Marcel\Anaconda3\lib\site-packages\statsmodels\nonparametric\kdetools.py:34: RuntimeWarning: invalid value encountered in double_scalars
  FAC1 = 2*(np.pi*bw/RANGE)**2
C:\Users\Marcel\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py:83: RuntimeWarning: invalid value encountered in reduce
  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)<jupyter_text>Exercise 8: Prepare Data for ClassificationOur CSV contains all the data plus the target class values. Remove (pop) the target class into an extra variable ("y").Then split the dataframe into the 4 variables for training & test data. Use the random state 10.<jupyter_code># remove target class into variable y
y = df.pop("diameter narrowing").values
# splitting dataframe
X_train, X_test, y_train, y_test = train_test_split(df, y, random_state=10)<jupyter_output><empty_output><jupyter_text>Exercise 9: Decision TreeCreate a decision tree, fit it with the training data and score it with the test data.Use random state 10. Configure the classifier to use a min sample split of 20.<jupyter_code># create decision tree classifier
clfTree = DecisionTreeClassifier(random_state=10, min_samples_split=20)
# fit classifier with training data
clfTree.fit(X_train, y_train)
# score classifier with test data
clfTree.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Exercise 10: Random ForestTest a random forest classifier to see if it performs better than the decision tree. Use a max depth of 3, 20 estimators and a random state of 10.Again, fit it with the train data and test with the test data.<jupyter_code># create random forest classifier
clfForest = RandomForestClassifier(max_depth=3, n_estimators=20, random_state=10)
# fit classifier with training data
clfForest.fit(X_train, y_train)
# score classifier with test data
clfForest.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Exercise 11: PredictionUse the random forest classifier to predict the class of an unknown patient. Use the following measured features:[[23.0,0.0,3.0,120.0,233.0,1.0,2.0,173.0,1.0,0.6,2.0,1.0,6.0]]<jupyter_code># predict class of unknown patient
patient = clfForest.predict([[23.0,0.0,3.0,120.0,233.0,1.0,2.0,173.0,1.0,0.6,2.0,1.0,6.0]])
# print prediction
print(f"Prediction for patient is: {patient}")<jupyter_output>Prediction for patient is: [1]<jupyter_text>Exercise 12: ScalingMany classifiers like k-NN, Support Vector Machines and Neural Networks work best with scaled features. Therefore, we need to scale these to make the features more comparable. For example, k-NN computes the euclidian distance - as such, normalized / standardized feature values are important.Create the StandardScaler. Then use fit_transform() to analyze the training data and to scale it in one step. Afterwards, use transform() to scale the test data (this applies the same scaling that was learned from the training data).Make sure to save the scaled test + train data into new variables, e.g., X_train_scaled and X_test_scaled.<jupyter_code># create scaler
scaler = StandardScaler()
# analyze and scale training data
X_train_scaled = scaler.fit_transform(X_train)
# scale test data
X_test_scaled = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>Exercise 13: k-NN ClassificationCreate the KNeighborsClassifier. Next, fit the classifier on the *scaled* training data and score it with the *scaled* test data.<jupyter_code># create k-NN classifier
clfKnn = KNeighborsClassifier()
# fit classifier with scaled training data
clfKnn.fit(X_train_scaled, y_train)
# score classifier with scaled test data
clfKnn.score(X_test_scaled, y_test)<jupyter_output><empty_output><jupyter_text>Exercise 14: Support Vector MachineCreate a support vector classifier (SVC). Use "scale" as gamma and a random state of 10.Next, fit the classifier on the *scaled* training data and score it with the *scaled* test data.<jupyter_code># create support vector machine classifier
clfSvm = svm.SVC(gamma="scale", random_state=10)
# fit classifier with scaled training data
clfSvm.fit(X_train_scaled, y_train)
# score classifier with scaled test data
clfSvm.score(X_test_scaled, y_test)<jupyter_output><empty_output><jupyter_text>Exercise 15: Neural NetworkAs a final classifier, create a neural network (multi-layer perceptron). As before, specify the random_state of 10. Use 2000 maximum iterations when configuring the classifier. Again, fit and score the classifier on the *scaled* data.<jupyter_code># create neural network classifier
clfNn = MLPClassifier(random_state=10, max_iter=2000)
# fit classifier with scaled training data
clfNn.fit(X_train_scaled, y_train)
# score classifier with scaled test data
clfNn.score(X_test_scaled, y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07987925229304539, 'fraction_numerical': 0.020085916637640776, 'mean_word_length': 6.037581699346405, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 56, 'https://': 2, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '61721', 'license': 'MIT', 'n_tokens_mistral': 2768, 'n_tokens_neox': 2561, 'n_words': 968, 'path': '08scikit_learn/scikit-learn.ipynb', 'repo_name': 'marcelalmesberger/Python-and-Machine-Learning', 'size': '8625'}