Browse Source

added missing notebooks

master
Klaus Reygers 1 year ago
parent
commit
44673d97c6
  1. 409
      notebooks/04_decision_trees_ex_1_compare_tree_classifiers.ipynb
  2. 484
      notebooks/04_decision_trees_ex_1_sol_compare_tree_classifiers.ipynb
  3. 264
      notebooks/04_decision_trees_ex_2_magic_xgboost_and_random_forest.ipynb
  4. 1775
      notebooks/04_decision_trees_ex_2_sol_magic_xgboost_and_random_forest.ipynb
  5. 159
      notebooks/05_neural_networks_ex_1_hyperparameter_optimization.ipynb
  6. 573
      notebooks/05_neural_networks_ex_1_sol_hyperparameter_optimization.ipynb
  7. 249
      notebooks/05_neural_networks_ex_2_mnist_keras_train.ipynb
  8. 182
      notebooks/05_neural_networks_ex_2_sol_mnist_keras_apply.ipynb
  9. 467
      notebooks/05_neural_networks_ex_2_sol_mnist_keras_train.ipynb

409
notebooks/04_decision_trees_ex_1_compare_tree_classifiers.ipynb

@ -0,0 +1,409 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Logistic regression with scikit-learn: heart disease data set"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Read data "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>age</th>\n",
" <th>sex</th>\n",
" <th>cp</th>\n",
" <th>trestbps</th>\n",
" <th>chol</th>\n",
" <th>fbs</th>\n",
" <th>restecg</th>\n",
" <th>thalach</th>\n",
" <th>exang</th>\n",
" <th>oldpeak</th>\n",
" <th>slope</th>\n",
" <th>ca</th>\n",
" <th>thal</th>\n",
" <th>target</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>63</td>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>145</td>\n",
" <td>233</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>150</td>\n",
" <td>0</td>\n",
" <td>2.3</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>37</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>130</td>\n",
" <td>250</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>187</td>\n",
" <td>0</td>\n",
" <td>3.5</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>41</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>130</td>\n",
" <td>204</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>172</td>\n",
" <td>0</td>\n",
" <td>1.4</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>56</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>120</td>\n",
" <td>236</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>178</td>\n",
" <td>0</td>\n",
" <td>0.8</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>57</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>120</td>\n",
" <td>354</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>163</td>\n",
" <td>1</td>\n",
" <td>0.6</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>298</th>\n",
" <td>57</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>140</td>\n",
" <td>241</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>123</td>\n",
" <td>1</td>\n",
" <td>0.2</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>299</th>\n",
" <td>45</td>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>110</td>\n",
" <td>264</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>132</td>\n",
" <td>0</td>\n",
" <td>1.2</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>300</th>\n",
" <td>68</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>144</td>\n",
" <td>193</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>141</td>\n",
" <td>0</td>\n",
" <td>3.4</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>301</th>\n",
" <td>57</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>130</td>\n",
" <td>131</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>115</td>\n",
" <td>1</td>\n",
" <td>1.2</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>302</th>\n",
" <td>57</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>130</td>\n",
" <td>236</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>174</td>\n",
" <td>0</td>\n",
" <td>0.0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>303 rows Ă— 14 columns</p>\n",
"</div>"
],
"text/plain": [
" age sex cp trestbps chol fbs restecg thalach exang oldpeak \\\n",
"0 63 1 3 145 233 1 0 150 0 2.3 \n",
"1 37 1 2 130 250 0 1 187 0 3.5 \n",
"2 41 0 1 130 204 0 0 172 0 1.4 \n",
"3 56 1 1 120 236 0 1 178 0 0.8 \n",
"4 57 0 0 120 354 0 1 163 1 0.6 \n",
".. ... ... .. ... ... ... ... ... ... ... \n",
"298 57 0 0 140 241 0 1 123 1 0.2 \n",
"299 45 1 3 110 264 0 1 132 0 1.2 \n",
"300 68 1 0 144 193 1 1 141 0 3.4 \n",
"301 57 1 0 130 131 0 1 115 1 1.2 \n",
"302 57 0 1 130 236 0 0 174 0 0.0 \n",
"\n",
" slope ca thal target \n",
"0 0 0 1 1 \n",
"1 0 0 2 1 \n",
"2 2 0 2 1 \n",
"3 2 0 2 1 \n",
"4 2 0 2 1 \n",
".. ... .. ... ... \n",
"298 1 0 3 0 \n",
"299 1 0 3 0 \n",
"300 1 2 3 0 \n",
"301 1 1 3 0 \n",
"302 1 1 2 0 \n",
"\n",
"[303 rows x 14 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"filename = \"https://www.physi.uni-heidelberg.de/~reygers/lectures/2021/ml/data/heart.csv\"\n",
"df = pd.read_csv(filename)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"y = df['target'].values\n",
"X = df[[col for col in df.columns if col!=\"target\"]]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, shuffle=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fit the model"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.ensemble import AdaBoostClassifier\n",
"from sklearn.ensemble import GradientBoostingClassifier\n",
"\n",
"lr = LogisticRegression(penalty='none', fit_intercept=True, max_iter=5000, tol=1E-5)\n",
"rf = RandomForestClassifier(max_depth=3)\n",
"ab = AdaBoostClassifier()\n",
"gb = GradientBoostingClassifier()\n",
"\n",
"classifiers = [lr, rf, ab, gb]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LogisticRegression\n",
"RandomForestClassifier\n",
"AdaBoostClassifier\n",
"GradientBoostingClassifier\n"
]
}
],
"source": [
"for clf in classifiers:\n",
" print(clf.__class__.__name__)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Train models and compare ROC curves"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"### Your code here ###"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

484
notebooks/04_decision_trees_ex_1_sol_compare_tree_classifiers.ipynb
File diff suppressed because one or more lines are too long
View File

264
notebooks/04_decision_trees_ex_2_magic_xgboost_and_random_forest.ipynb
File diff suppressed because one or more lines are too long
View File

1775
notebooks/04_decision_trees_ex_2_sol_magic_xgboost_and_random_forest.ipynb
File diff suppressed because one or more lines are too long
View File

159
notebooks/05_neural_networks_ex_1_hyperparameter_optimization.ipynb

@ -0,0 +1,159 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Hyperparameter optimization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Superconductivty Data Set: Predict the critical temperature based on 81 material features."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.metrics import mean_squared_error"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"filename = \"https://www.physi.uni-heidelberg.de/~reygers/lectures/2021/ml/data/train_critical_temp.csv\"\n",
"df = pd.read_csv(filename, engine='python')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = df['critical_temp'].values\n",
"X = df[[col for col in df.columns if col!=\"critical_temp\"]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, shuffle=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.neural_network import MLPRegressor\n",
"import time\n",
"\n",
"mlpr = MLPRegressor(hidden_layer_sizes=(50,50), activation='relu', random_state=1, max_iter=5000)\n",
"\n",
"start_time = time.time()\n",
"mlpr.fit(X_train, y_train)\n",
"run_time = time.time() - start_time\n",
"\n",
"print(run_time)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y_pred = mlpr.predict(X_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.scatter(y_test, y_pred, s=2)\n",
"plt.xlabel(\"true critical temperature (K)\", fontsize=14)\n",
"plt.ylabel(\"predicted critical temperature (K)\", fontsize=14)\n",
"plt.savefig(\"critical_temperature.pdf\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rms = np.sqrt(mean_squared_error(y_test, y_pred))\n",
"print(f\"root mean square error {rms:.2f}\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Now try to optimize the hyperparameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import GridSearchCV\n",
"\n",
"### Your code here\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

573
notebooks/05_neural_networks_ex_1_sol_hyperparameter_optimization.ipynb
File diff suppressed because one or more lines are too long
View File

249
notebooks/05_neural_networks_ex_2_mnist_keras_train.ipynb

@ -0,0 +1,249 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise: Training a digit-classification neural network on the MNIST dataset using Keras\n",
"\n",
"This example is from [Stefan Wunsch (CERN IML TensoFlow and Keras workshop)](https://github.com/stwunsch/iml_tensorflow_keras_workshop). See also the example on the [Keras website](https://keras.io/examples/vision/mnist_convnet/).\n",
"\n",
"The MNIST dataset is one of the most popular benchmark-datasets in modern machine learning. The dataset consists of 70000 images of handwritten digits and associated labels, which can be used to train neural network performing image classification.\n",
"\n",
"The following program presents the basic workflow of Keras showing the most import details of the API."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"np.random.seed(1234)\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download the dataset\n",
"\n",
"The code below downloads the dataset and performs a scaling of the pixel-values of the images. Because the images are encoded with 8-bit unsigned int values, we scale these values to floating-point values in the range `[0, 1)` so that the inputs match the activation of the neurons better."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.datasets import mnist\n",
"from tensorflow.keras.utils import to_categorical\n",
"\n",
"# Download dataset\n",
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
"\n",
"# The data is loaded as flat array with 784 entries (28x28),\n",
"# we need to reshape it into an array with shape:\n",
"# (num_images, pixels_row, pixels_column, color channels)\n",
"x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)\n",
"x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)\n",
"\n",
"# Convert digits to one-hot vectors, e.g.,\n",
"# 2 -> [0 0 1 0 0 0 0 0 0 0]\n",
"# 0 -> [1 0 0 0 0 0 0 0 0 0]\n",
"# 9 -> [0 0 0 0 0 0 0 0 0 1]\n",
"y_train = to_categorical(y_train, 10)\n",
"y_test = to_categorical(y_test, 10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Addtionally, we store some example images to disk to show later on the inference part of the Keras API."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import png\n",
"\n",
"num_examples = 6\n",
"# offset = 100\n",
"offset = 200\n",
"\n",
"plt.figure(figsize=(num_examples*2, 2))\n",
"for i in range(num_examples):\n",
" plt.subplot(1, num_examples, i+1)\n",
" plt.axis('off')\n",
" # example = np.squeeze(np.array(x_test[offset+i]*255).astype(\"uint8\"))\n",
" example = np.squeeze(np.array(x_test[offset+i]).astype(\"uint8\"))\n",
" plt.imshow(example, cmap=\"gray\")\n",
" w = png.Writer(28, 28, greyscale=True)\n",
" w.write(open(\"mnist_example_{}.png\".format(i+1), 'wb'), example)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Model / data parameters\n",
"num_classes = 10\n",
"input_shape = (28, 28, 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the model\n",
"\n",
"The model definition in Keras can be done using the `Sequential` or the functional API. Shown here is the `Sequential` API allowing to stack neural network layers on top of each other, which is feasible for most neural network models. In contrast, the functional API would allow to have multiple inputs and outputs for a maximum of flexibility to build your custom model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense, Flatten, MaxPooling2D, Conv2D, Input, Dropout\n",
"\n",
"# conv layer with 8 3x3 filters\n",
"model = Sequential(\n",
" [\n",
" Input(shape=input_shape),\n",
" Conv2D(8, kernel_size=(3, 3), activation=\"relu\"),\n",
" MaxPooling2D(pool_size=(2, 2)),\n",
" Flatten(),\n",
" Dense(16, activation=\"relu\"),\n",
" Dense(num_classes, activation=\"softmax\"),\n",
" ]\n",
")\n",
"\n",
"model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compile the model\n",
"\n",
"Using Keras, you have to `compile` a model, which means adding the loss function, the optimizer algorithm and validation metrics to your training setup."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.compile(loss=\"categorical_crossentropy\",\n",
" optimizer=\"adam\",\n",
" metrics=[\"accuracy\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Train the model\n",
"\n",
"The cell below shows the training procedure of Keras using the `model.fit(...)` method. Besides typical options such as `batch_size` and `epochs`, which control the number of gradient steps of your training, Keras allows to use callbacks during training.\n",
"\n",
"Callbacks are methods, which are called during training to perform tasks such as saving checkpoints of the model (`ModelCheckpoint`) or stop the training early if a convergence criteria is met (`EarlyStopping`)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\n",
"\n",
"checkpoint = ModelCheckpoint(\n",
" filepath=\"mnist_keras_model.h5\",\n",
" save_best_only=True,\n",
" verbose=1)\n",
"early_stopping = EarlyStopping(patience=2)\n",
"\n",
"history = model.fit(x_train, y_train, # Training data\n",
" batch_size=200, # Batch size\n",
" epochs=50, # Maximum number of training epochs\n",
" validation_split=0.5, # Use 50% of the train dataset for validation\n",
" callbacks=[checkpoint, early_stopping]) # Register callbacks"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### a) Plot training and validation loss as well as training and validation accurace as a function of the number of epochs "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# hint: use\n",
"# history.history[\"loss\"]\n",
"# history.history[\"val_loss\"]\n",
"# history.history[\"accuracy\"]\n",
"# history.history[\"val_accuracy\"]\n",
"\n",
"### Your code here ###"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### b) Determine the accuracy of the fully trained model\n",
"The prediction of unseen data is performed using the `model.predict(inputs)` call. Below, a basic test of the model is done by calculating the accuracy on the test dataset."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Your code here ###"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

182
notebooks/05_neural_networks_ex_2_sol_mnist_keras_apply.ipynb
File diff suppressed because one or more lines are too long
View File

467
notebooks/05_neural_networks_ex_2_sol_mnist_keras_train.ipynb
File diff suppressed because one or more lines are too long
View File

Loading…
Cancel
Save