2016-03-15 12:55:14 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](files/images/EscUpmPolit_p.gif \"UPM\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Course Notes for Learning Intelligent Systems"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2019-02-28 10:32:00 +00:00
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © Carlos A. Iglesias"
2016-03-15 12:55:14 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## [Introduction to Machine Learning](2_0_0_Intro_ML.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Table of Contents\n",
"* [kNN Model](#kNN-Model)\n",
"* [Load data and preprocessing](#Load-data-and-preprocessing)\n",
"* [Train classifier](#Train-classifier)\n",
"* [Evaluating the algorithm](#Evaluating-the-algorithm)\n",
" * [Precision, recall and f-score](#Precision,-recall-and-f-score)\n",
"\t* [Confusion matrix](#Confusion-matrix)\n",
"\t* [K-Fold validation](#K-Fold-validation)\n",
"* [Tuning the algorithm](#Tuning-the-algorithm)\n",
"* [References](#References)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# kNN Model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2016-03-28 10:26:20 +00:00
"The goal of this notebook is to learn how to train a model, make predictions with that model and evaluate these predictions.\n",
2016-03-15 12:55:14 +00:00
"\n",
"The notebook uses the [kNN (k nearest neighbors) algorithm](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2016-03-28 10:26:20 +00:00
"## Loading data and preprocessing\n",
2016-03-15 12:55:14 +00:00
"\n",
"The first step is loading and preprocessing the data as explained in the previous notebooks."
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 1,
2018-02-23 14:48:59 +00:00
"metadata": {},
2016-03-15 12:55:14 +00:00
"outputs": [],
"source": [
"# library for displaying plots\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# display plots in the notebook \n",
2016-03-28 10:26:20 +00:00
"%matplotlib inline"
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 2,
2018-02-23 14:48:59 +00:00
"metadata": {},
2016-03-28 10:26:20 +00:00
"outputs": [],
"source": [
2016-03-15 12:55:14 +00:00
"## First, we repeat the load and preprocessing steps\n",
"\n",
"# Load data\n",
"from sklearn import datasets\n",
"iris = datasets.load_iris()\n",
"\n",
"# Training and test spliting\n",
2018-02-23 14:48:59 +00:00
"from sklearn.model_selection import train_test_split\n",
2016-03-15 12:55:14 +00:00
"\n",
"x_iris, y_iris = iris.data, iris.target\n",
"\n",
"# Test set will be the 25% taken randomly\n",
"x_train, x_test, y_train, y_test = train_test_split(x_iris, y_iris, test_size=0.25, random_state=33)\n",
"\n",
"# Preprocess: normalize\n",
"from sklearn import preprocessing\n",
"scaler = preprocessing.StandardScaler().fit(x_train)\n",
"x_train = scaler.transform(x_train)\n",
"x_test = scaler.transform(x_test)"
]
},
{
"cell_type": "markdown",
2019-02-28 14:25:19 +00:00
"metadata": {},
2016-03-15 12:55:14 +00:00
"source": [
"## Train classifier"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The usual steps for creating a classifier are:\n",
"1. Create classifier object\n",
"2. Call *fit* to train the classifier\n",
"3. Call *predict* to obtain predictions\n",
"\n",
"Once the model is created, the most relevant methods are:\n",
"* model.fit(x_train, y_train): train the model\n",
"* model.predict(x): predict\n",
"* model.score(x, y): evaluate the prediction"
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
" metric_params=None, n_jobs=None, n_neighbors=15, p=2,\n",
" weights='uniform')"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
2016-03-15 12:55:14 +00:00
"source": [
"from sklearn.neighbors import KNeighborsClassifier\n",
"import numpy as np\n",
"\n",
"# Create kNN model\n",
"model = KNeighborsClassifier(n_neighbors=15)\n",
"\n",
"# Train the model using the training sets\n",
"model.fit(x_train, y_train) "
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Prediction [1 0 1 1 1 0 0 1 0 2 0 0 1 2 0 1 2 2 1 1 0 0 1 0 0 2 1 1 2 2 2 2 0 0 1 1 0\n",
" 1 2 1 2 0 2 0 1 0 2 1 0 2 2 0 0 2 0 0 0 2 2 0 1 0 1 0 1 1 1 1 1 0 1 0 1 2\n",
" 0 0 0 0 2 2 0 1 1 2 1 0 0 2 1 1 0 1 1 0 2 1 2 1 2 0 2 0 0 0 2 1 2 1 2 1 2\n",
" 0]\n",
"Expected [1 0 1 1 1 0 0 1 0 2 0 0 1 2 0 1 2 2 1 1 0 0 2 0 0 2 1 1 2 2 2 2 0 0 1 1 0\n",
" 1 2 1 2 0 2 0 1 0 2 1 0 2 2 0 0 2 0 0 0 2 2 0 1 0 1 0 1 1 1 1 1 0 1 0 1 2\n",
" 0 0 0 0 2 2 0 1 1 2 1 0 0 1 1 1 0 1 1 0 2 2 2 1 2 0 1 0 0 0 2 1 2 1 2 1 2\n",
" 0]\n"
]
}
],
2016-03-15 12:55:14 +00:00
"source": [
"print(\"Prediction \", model.predict(x_train))\n",
"print(\"Expected \", y_train)"
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accuracy in training 0.9642857142857143\n"
]
}
],
2016-03-15 12:55:14 +00:00
"source": [
"# Evaluate Accuracy in training\n",
"\n",
"from sklearn import metrics\n",
"y_train_pred = model.predict(x_train)\n",
"print(\"Accuracy in training\", metrics.accuracy_score(y_train, y_train_pred))"
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accuracy in testing 0.9210526315789473\n"
]
}
],
2016-03-15 12:55:14 +00:00
"source": [
"# Now we evaluate error in testing\n",
"y_test_pred = model.predict(x_test)\n",
"print(\"Accuracy in testing \", metrics.accuracy_score(y_test, y_test_pred))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we are going to visualize the Nearest Neighbors classification. It will plot the decision boundaries for each class.\n",
"\n",
"We are going to import a function defined in the file [util_knn.py](files/util_knn.py) using the *magic command* **%run**."
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAEICAYAAACktLTqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJztnXecVNX5h593ylaq9A6CWFBURLHGXiMaTcAeW0SMUROjJppojMaSmFiiRoMaY0t+kUQTCxhRI/aCiohYQHrvLLDLtnl/f5y77JQ7uzO7U7a8D5/9MHPumfe8994z3zn3PU1UFcMwDKNtEci3A4ZhGEbmMXE3DMNog5i4G4ZhtEFM3A3DMNogJu6GYRhtEBN3wzCMNoiJu4eI3CgiT7Z1P0TkcxE5zHstIvKoiGwQkQ9E5BAR+SoLZQ4UkS0iEsy0bc/+30XkO97r80TkrWyU01oQkQdF5PoU8/5VRH6TbZ/yQfx1EJFLRGSVVxe75diXZ0TkuFyW2WrFXUSeFJEVIlImIl+LyA9S+MyZIjLDu7krRGSqiBycC39bCqo6QlVf994eDBwN9FfV/VT1TVXdublliMhCETkqqszFqtpBVWuba9unrJHAnsB/Mm27gTIPF5H/icgmEVnoc3yhiFR49WyLiLycK98AVHWiqt6cCVs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GCMVrOVuGIbRBjFxNwzDaIOYuBtGK8JCMkaqmLgbhmG0QUzcDcMw2iApibuILBSRz0RkpojM8DkuIvJHEZknIrNEZFTmXTUMwzBSJZ2hkIer6tokx44HdvL+xgAPeP8bhpEhLN5upEO
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAEICAYAAACktLTqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJztnXecVNX5h593Z7axVCnSiyCoKCoQwRYVwRbRaAJq1NgiokaNNWISNXZjj5oY1Bhb/EWMGgsaUSOCHRVQQQXpvbPALlvf3x/nLszO3Jmd2Z2yO/s++5nPzpx75pz3nnvud859TxNVxTAMw8gucjJtgGEYhpF8TNwNwzCyEBN3wzCMLMTE3TAMIwsxcTcMw8hCTNwNwzCykGYv7iJyo4g8k+12iMg3InK4915E5AkR2Sgin4rIoSLyXQry7CkiW0UkkOy0vfSfE5Gfeu/PFpHpqcinqSAij4jIH+KM+w8RuSXVNiWLUHtTVV/TgYhcKiJ3pCOvJifuIvKMiKwUkWIR+V5EfhXHd34hIjM8oVkpIm+IyCHpsLexoKoDVfU97+MhwCigu6oeoKrTVHVAQ/MQkUUiMjIkzyWq2lJVqxqatk9eg4B9gf8kO+0YeR4hIv8Tkc0issjn+CIRKfXq2VYReStdtgGo6nhVvTkZaYmIiki/ZKQVZ369/crUj3jrayNquL1X07ACJgJniEinVOfb5MQduB3oraqtgROAW0RkSLTIInIFcD9wG7Ar0BP4C3BiGmxtrPQCFqnqtkwb0gAuAJ7V9M7C2wb8Hbg6RpzR3g9aS1U9Kk12GU0EVd0OvAH8MtV5NTlxV9VvVLWs5qP36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vIj08ypjTbjf8L/XgM4i8hsRyReRViIyLAN2xOJT3M11h4gUiUiBiBwcYtdWYJOIdCNy1MhqnI84AlVdCnwI3O6lOQg4D+cOqA+T8XEJqepzuI60t8XrIPSJ82xII8Dv5euWEZEcESnAuQzEO48871hPETnYuz4FInI1rrX3gXf8cBGJ9eM3FTgCKFTVZcA0nN+8PfClF+c1oL+InOnV41wR+ZGI7OmT3vPAOSKyp4i0wN0HiVDrWiZSt9QNu41WtuPjyPsjnO/7UhEJisjJOHdhBCIyQERGeMK9HSgNsWs10Ft2dvzm4Vw4a4FKETkWiGtEk+cW+ztwr7iBAQEROdDL9xlgtIgc7YUXeNe7e5TkDsONmEkpTUrccb/EFwLLcJ1MdwO/UdWoY51V9V7gCuD3uIu6FPg1rsUbzlN4nVnAHODjsONnAos8l8R4XMsVYHfgbZzwfYTrNHrPx5YtuM6W0bhH4nm4GzqtdsRCnb97NM7FsQRX1qd4h/+I6wzaDLyO6+8I5Xbg996j6VU+yZ+GewpZAbwE3OD5J+vDROD0sNZdzTk8iesUe1dEetczfT9+jBOPybiWZCnOZwvuh++vuHq5HCfMx6rqeu94D9w18UVVv8ddt2ne52LcqJwPvGtSU3+OAk7FleEqXCs13ye9N4A/43z/80PyjtY/Fc6NwJPetRxLEupWvHgutZNxneQbcfUvvK7VkA/cgWtZr8I9zdaMQJvk/V8vIl945Xcp7odvI+4J+ZUETLsK+Arn/9+AK/scr+FyopdvjcZcjY++eo2D44AnE8i3XtSMpDCMJoeI/BN4XlX9fqgbFSLyGDBJVf+bofz3BL4G8v36U4z0ICKXAD1U9ZqU52XibhjZiYichHvCKsK1FKtVNd7+J6OJ09TcMoZhxM8FODfBDzg/tF/Hq5GlWMvdMAwjC7GWu2EYRhYS1+SeVNChdWvt3bFjprI3jKxhI+0ybYKRRhYs+HydqtYpnhkT994dOzLjjrQsa2wYWc0kxmTaBCO
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
2016-03-15 12:55:14 +00:00
"source": [
"%run util_knn.py\n",
2019-02-28 14:25:19 +00:00
"\n",
2016-03-15 12:55:14 +00:00
"plot_classification_iris()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Evaluating the algorithm"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Precision, recall and f-score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For evaluating classification algorithms, we usually calculate three metrics: precision, recall and F1-score\n",
"\n",
"* **Precision**: This computes the proportion of instances predicted as positives that were correctly evaluated (it measures how right our classifier is when it says that an instance is positive).\n",
"* **Recall**: This counts the proportion of positive instances that were correctly evaluated (measuring how right our classifier is when faced with a positive instance).\n",
"* **F1-score**: This is the harmonic mean of precision and recall, and tries to combine both in a single number."
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" precision recall f1-score support\n",
"\n",
" setosa 1.00 1.00 1.00 8\n",
" versicolor 0.79 1.00 0.88 11\n",
" virginica 1.00 0.84 0.91 19\n",
"\n",
" micro avg 0.92 0.92 0.92 38\n",
" macro avg 0.93 0.95 0.93 38\n",
"weighted avg 0.94 0.92 0.92 38\n",
"\n"
]
}
],
2016-03-15 12:55:14 +00:00
"source": [
2016-03-28 10:26:20 +00:00
"print(metrics.classification_report(y_test, y_test_pred, target_names=iris.target_names))"
2016-03-15 12:55:14 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Confusion matrix"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another useful metric is the confusion matrix"
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 8 0 0]\n",
" [ 0 11 0]\n",
" [ 0 3 16]]\n"
]
}
],
2016-03-15 12:55:14 +00:00
"source": [
"print(metrics.confusion_matrix(y_test, y_test_pred))"
]
},
{
"cell_type": "markdown",
2018-02-23 14:48:59 +00:00
"metadata": {},
2016-03-15 12:55:14 +00:00
"source": [
"We see we classify well all the 'setosa' and 'versicolor' samples. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### K-Fold validation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to avoid bias in the training and testing dataset partition, it is recommended to use **k-fold validation**."
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.93333333 0.8 1. 0.93333333 0.93333333 0.93333333\n",
" 1. 1. 0.86666667 0.93333333]\n"
]
}
],
2016-03-15 12:55:14 +00:00
"source": [
2018-02-23 14:48:59 +00:00
"from sklearn.model_selection import cross_val_score, KFold\n",
2016-03-15 12:55:14 +00:00
"from sklearn.pipeline import Pipeline\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"# create a composite estimator made by a pipeline of preprocessing and the KNN model\n",
"model = Pipeline([\n",
" ('scaler', StandardScaler()),\n",
" ('kNN', KNeighborsClassifier())\n",
"])\n",
"\n",
"# create a k-fold cross validation iterator of k=10 folds\n",
2018-02-23 14:48:59 +00:00
"cv = KFold(10, shuffle=True, random_state=33)\n",
2016-03-15 12:55:14 +00:00
"\n",
"# by default the score used is the one returned by score method of the estimator (accuracy)\n",
"scores = cross_val_score(model, x_iris, y_iris, cv=cv)\n",
"print(scores)"
]
},
{
"cell_type": "markdown",
2019-02-28 14:25:19 +00:00
"metadata": {},
2016-03-15 12:55:14 +00:00
"source": [
"We get an array of k scores. We can calculate the mean and the standard error to obtain a final figure"
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mean score: 0.933 (+/- 0.020)\n"
]
}
],
2016-03-15 12:55:14 +00:00
"source": [
"from scipy.stats import sem\n",
"def mean_score(scores):\n",
" return (\"Mean score: {0:.3f} (+/- {1:.3f})\").format(np.mean(scores), sem(scores))\n",
"print(mean_score(scores))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So, we get an average accuracy of 0.940."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tuning the algorithm"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are going to tune the algorithm, and calculate which is the best value for the k parameter."
]
},
{
"cell_type": "code",
2019-03-06 16:44:30 +00:00
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Text(0, 0.5, 'Accuracy')"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYsAAAEKCAYAAADjDHn2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJztvXuYY/lZ3/l5dasqSdXdkrqqZ6a73T3TVY49LMY2jSHmYi8EMzasHdvZ2E4WzCXrJYl3E3icB3shDs8Ex5g4WSD2snESLzYQfJldEsMOGDNrAw+LYXqwxzAeZkrdnkv3jLuqS9XdJdVFt9/+cc5PpVbrcnR0jqTSeT/P00+rpCPpd3Sk3/d3ed/vK8YYFEVRFKUfsUk3QFEURZl+VCwURVGUgahYKIqiKANRsVAURVEGomKhKIqiDETFQlEURRlIqGIhIveJyBMiUhSRd3d5/IyIPCQiXxGRL4jIqbbHGiLyZfffZ8Jsp6IoitIfCSvPQkTiwJPA9wKXgYeBtxljvtp2zKeB3zHGfExEvhv4EWPMD7qPlY0x2VAapyiKogxFmDOLVwBFY8wlY0wV+ATwho5j7gUecm9/vsvjiqIoyhSQCPG1TwLPtv19GfjWjmMeBd4M/BLwRmBRRArGmE1gXkQuAHXg540x/6XzDU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yKQP+ZRTScd5XhULHoQi6d1JHVjGJFoNeeBbg25TM6WLDn1W/PIp9xanoYM7uWJ8rwqFh0Ia+e/jNLa2bRZxmq4IrFLHaWdsYwaM9mv95kp9oYV7OUQ4CKRRcKMzyyjDo2f2JQZ1ltNKnMYGdZai1
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
2016-03-15 12:55:14 +00:00
"source": [
"k_range = range(1, 21)\n",
"accuracy = []\n",
"for k in k_range:\n",
" m = KNeighborsClassifier(k)\n",
" m.fit(x_train, y_train)\n",
" y_test_pred = m.predict(x_test)\n",
" accuracy.append(metrics.accuracy_score(y_test, y_test_pred))\n",
"plt.plot(k_range, accuracy)\n",
"plt.xlabel('k value')\n",
"plt.ylabel('Accuracy')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The result is very dependent of the input data. Execute again the train_test_split and test again how the result changes with k."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* [KNeighborsClassifier API scikit-learn](http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html)\n",
"* [Learning scikit-learn: Machine Learning in Python](http://proquest.safaribooksonline.com/book/programming/python/9781783281930/1dot-machine-learning-a-gentle-introduction/ch01s02_html), Raúl Garreta; Guillermo Moncecchi, Packt Publishing, 2013.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Licence\n",
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n",
2019-02-28 10:32:00 +00:00
"© Carlos A. Iglesias, Universidad Politécnica de Madrid."
2016-03-15 12:55:14 +00:00
]
}
],
"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",
2019-03-06 16:44:30 +00:00
"version": "3.7.1"
2019-02-28 10:32:00 +00:00
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
2016-03-15 12:55:14 +00:00
}
},
"nbformat": 4,
2018-02-23 14:48:59 +00:00
"nbformat_minor": 1
2016-03-15 12:55:14 +00:00
}