Commit 4b09cf4f authored by Marvin Kastner's avatar Marvin Kastner
Browse files

Add Erkennung von Ziffern

parent bebe3dd9
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Erkennung von Ziffern\n",
"\n",
"Das Erkennen von Ziffern hat in dem Bereich Maschinelles Lernen Traidition.\n",
"Anbei werden die Ziffern klassifziert, die mit scikit-learn bereits mit ausgeliefert werden."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from sklearn import datasets\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn import metrics\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zunächst wird der Datensatz mit den Ziffern geladen."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'data': array([[ 0., 0., 5., ..., 0., 0., 0.],\n",
" [ 0., 0., 0., ..., 10., 0., 0.],\n",
" [ 0., 0., 0., ..., 16., 9., 0.],\n",
" ...,\n",
" [ 0., 0., 1., ..., 6., 0., 0.],\n",
" [ 0., 0., 2., ..., 12., 0., 0.],\n",
" [ 0., 0., 10., ..., 12., 1., 0.]]),\n",
" 'target': array([0, 1, 2, ..., 8, 9, 8]),\n",
" 'target_names': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),\n",
" 'images': array([[[ 0., 0., 5., ..., 1., 0., 0.],\n",
" [ 0., 0., 13., ..., 15., 5., 0.],\n",
" [ 0., 3., 15., ..., 11., 8., 0.],\n",
" ...,\n",
" [ 0., 4., 11., ..., 12., 7., 0.],\n",
" [ 0., 2., 14., ..., 12., 0., 0.],\n",
" [ 0., 0., 6., ..., 0., 0., 0.]],\n",
" \n",
" [[ 0., 0., 0., ..., 5., 0., 0.],\n",
" [ 0., 0., 0., ..., 9., 0., 0.],\n",
" [ 0., 0., 3., ..., 6., 0., 0.],\n",
" ...,\n",
" [ 0., 0., 1., ..., 6., 0., 0.],\n",
" [ 0., 0., 1., ..., 6., 0., 0.],\n",
" [ 0., 0., 0., ..., 10., 0., 0.]],\n",
" \n",
" [[ 0., 0., 0., ..., 12., 0., 0.],\n",
" [ 0., 0., 3., ..., 14., 0., 0.],\n",
" [ 0., 0., 8., ..., 16., 0., 0.],\n",
" ...,\n",
" [ 0., 9., 16., ..., 0., 0., 0.],\n",
" [ 0., 3., 13., ..., 11., 5., 0.],\n",
" [ 0., 0., 0., ..., 16., 9., 0.]],\n",
" \n",
" ...,\n",
" \n",
" [[ 0., 0., 1., ..., 1., 0., 0.],\n",
" [ 0., 0., 13., ..., 2., 1., 0.],\n",
" [ 0., 0., 16., ..., 16., 5., 0.],\n",
" ...,\n",
" [ 0., 0., 16., ..., 15., 0., 0.],\n",
" [ 0., 0., 15., ..., 16., 0., 0.],\n",
" [ 0., 0., 2., ..., 6., 0., 0.]],\n",
" \n",
" [[ 0., 0., 2., ..., 0., 0., 0.],\n",
" [ 0., 0., 14., ..., 15., 1., 0.],\n",
" [ 0., 4., 16., ..., 16., 7., 0.],\n",
" ...,\n",
" [ 0., 0., 0., ..., 16., 2., 0.],\n",
" [ 0., 0., 4., ..., 16., 2., 0.],\n",
" [ 0., 0., 5., ..., 12., 0., 0.]],\n",
" \n",
" [[ 0., 0., 10., ..., 1., 0., 0.],\n",
" [ 0., 2., 16., ..., 1., 0., 0.],\n",
" [ 0., 0., 15., ..., 15., 0., 0.],\n",
" ...,\n",
" [ 0., 4., 16., ..., 16., 6., 0.],\n",
" [ 0., 8., 16., ..., 16., 8., 0.],\n",
" [ 0., 1., 8., ..., 12., 1., 0.]]]),\n",
" 'DESCR': \"Optical Recognition of Handwritten Digits Data Set\\n===================================================\\n\\nNotes\\n-----\\nData Set Characteristics:\\n :Number of Instances: 5620\\n :Number of Attributes: 64\\n :Attribute Information: 8x8 image of integer pixels in the range 0..16.\\n :Missing Attribute Values: None\\n :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr)\\n :Date: July; 1998\\n\\nThis is a copy of the test set of the UCI ML hand-written digits datasets\\nhttp://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits\\n\\nThe data set contains images of hand-written digits: 10 classes where\\neach class refers to a digit.\\n\\nPreprocessing programs made available by NIST were used to extract\\nnormalized bitmaps of handwritten digits from a preprinted form. From a\\ntotal of 43 people, 30 contributed to the training set and different 13\\nto the test set. 32x32 bitmaps are divided into nonoverlapping blocks of\\n4x4 and the number of on pixels are counted in each block. This generates\\nan input matrix of 8x8 where each element is an integer in the range\\n0..16. This reduces dimensionality and gives invariance to small\\ndistortions.\\n\\nFor info on NIST preprocessing routines, see M. D. Garris, J. L. Blue, G.\\nT. Candela, D. L. Dimmick, J. Geist, P. J. Grother, S. A. Janet, and C.\\nL. Wilson, NIST Form-Based Handprint Recognition System, NISTIR 5469,\\n1994.\\n\\nReferences\\n----------\\n - C. Kaynak (1995) Methods of Combining Multiple Classifiers and Their\\n Applications to Handwritten Digit Recognition, MSc Thesis, Institute of\\n Graduate Studies in Science and Engineering, Bogazici University.\\n - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika.\\n - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin.\\n Linear dimensionalityreduction using relevance weighted LDA. School of\\n Electrical and Electronic Engineering Nanyang Technological University.\\n 2005.\\n - Claudio Gentile. A New Approximate Maximal Margin Classification\\n Algorithm. NIPS. 2000.\\n\"}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"digits = datasets.load_digits()\n",
"digits"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Darstellung einer Ziffer\n",
"\n",
"Nun wird exemplarisch die 22te Ziffer des Datensatzes dargestellt."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0., 0., 8., 16., 5., 0., 0., 0.],\n",
" [ 0., 1., 13., 11., 16., 0., 0., 0.],\n",
" [ 0., 0., 10., 0., 13., 3., 0., 0.],\n",
" [ 0., 0., 3., 1., 16., 1., 0., 0.],\n",
" [ 0., 0., 0., 9., 12., 0., 0., 0.],\n",
" [ 0., 0., 3., 15., 5., 0., 0., 0.],\n",
" [ 0., 0., 14., 15., 8., 8., 3., 0.],\n",
" [ 0., 0., 7., 12., 12., 12., 13., 1.]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"digits.images[22]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Die Pixel werden anscheinend als Fließkommazahlen zwischen 0 und 16 dargestellt, wobei die Nachkommastellen nicht genutzt werden.\n",
"Eine Ziffer wird außerdem durch eine $8 \\times 8$-Matrix repräsentiert."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAACtBJREFUeJzt3d2LXeUZhvH77kRp/WJCa0vMhEZBAlJoIhKQgKSxLbGKmYMeJKA4UogniqEF0Z5I/wGZHhQhxFjBVGnjJ2K1ggYrtNYkjq3JxDINUzKNNkqNRgsNo08PZqek6ZS9JvtdH/Pk+kFwPjZ5n024XGv2rL1eR4QA5PSFtgcAUB8CBxIjcCAxAgcSI3AgMQIHEiNwIDECBxIjcCCxJXX8pbZTXh63bNmyRte77LLLGltramqqsbU++uijxtbKLCLc7zG1BJ7VHXfc0eh6999/f2NrjY6ONrbWM88809ha5zpO0YHECBxIjMCBxAgcSIzAgcQIHEiMwIHECBxIrFLgtjfafsf2lO176x4KQBl9A7c9JOlnkm6QdJWkLbavqnswAIOrcgRfK2kqIg5HxElJj0vaVO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CYEDiRG4EBiBA4kRuBAYgQOJEbgQGIEDiT2bwXrnem842oEAAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.imshow(digits.images[22], cmap=\"gray\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Es handelt sich scheinbar um eine 2, wobei 0 als schwarz und 16 als weiß dargestellt wird.\n",
"Dies können wir über das Array `digits.target` überprüfen."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"digits.target[22]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dies bestätigt, die oben dargestellte Ziffer eine 2 ist.\n",
"\n",
"Eine alternative, uns eher geläufige Darstellung ist schwarz auf weiß.\n",
"Dafür können wir die Intensitätswerte einfach \"umdrehen\"."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[16., 16., 8., 0., 11., 16., 16., 16.],\n",
" [16., 15., 3., 5., 0., 16., 16., 16.],\n",
" [16., 16., 6., 16., 3., 13., 16., 16.],\n",
" [16., 16., 13., 15., 0., 15., 16., 16.],\n",
" [16., 16., 16., 7., 4., 16., 16., 16.],\n",
" [16., 16., 13., 1., 11., 16., 16., 16.],\n",
" [16., 16., 2., 1., 8., 8., 13., 16.],\n",
" [16., 16., 9., 4., 4., 4., 3., 15.]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inverted = 16 - digits.images[22]\n",
"inverted"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/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Umw/Ken3EbGtd6PRiyPiw7bnOl9dOIKvlXQwIg5FxClJz0ja2PJMfYuIYxGxr/f2x5KmJK1od6oybA9KulXStrZnKcn25ZJulPS4JEXEqcUct9SNwFdIOnLO+9NKEsJZtockrZH0VruTFPOIpPskfdb2IIVdLWlG0vbetx/bbF/S9lD96ELgnuNjaV7at32ppGcljUXER23P0y/bt0k6HhF7256lBhdIul7SoxGxRtKnkhb1a0JdCHxa0spz3h+UdLSlWYqyfaFm494ZEVnuSLtO0u22D2v226n1tp9qd6RipiVNR8TZM61dmg1+0epC4G9Lutb2Vb0XNTZJeqHlmfpm25r9Xm4qIh5ue55SIuKBiBiMiCHN/lu9FhE/anmsIiLiA0lHbK/qfehmSYv6RdFKt02uU0Sctn2XpFckDUh6IiL2tzxWCesk3SHpL7Yneh/7eUS81OJMmN/dknb2DjaHJN3Z8jx9af3HZADq04VTdAA1IXAgMQIHEiNwIDECBxIjcCAxAgcSI3Agsf8Cmi2uevnhmE8AAAAASUVORK5CYII=\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.imshow(inverted, cmap=\"gray\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dieser Schritt ist aber für den Menschen eher interessant und für weniger für das Maschinelle Lernen.\n",
"\n",
"Eine Möglichkeit, Ziffern zu analysieren, ist, einfach jeden Pixel wie ein eigenes Attribut zu behandeln.\n",
"Beim Aufruf von `flatten` werden die Reihen der Matrix aneinandergehängt, vgl. die [numpy-Dokumentation](https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.ndarray.flatten.html)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 0., 8., 16., 5., 0., 0., 0., 0., 1., 13., 11., 16.,\n",
" 0., 0., 0., 0., 0., 10., 0., 13., 3., 0., 0., 0., 0.,\n",
" 3., 1., 16., 1., 0., 0., 0., 0., 0., 9., 12., 0., 0.,\n",
" 0., 0., 0., 3., 15., 5., 0., 0., 0., 0., 0., 14., 15.,\n",
" 8., 8., 3., 0., 0., 0., 7., 12., 12., 12., 13., 1.])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"flattened = digits.images[22].flatten()\n",
"flattened"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hier sind nun einfach alle Pixel aneinander gehängt worden.\n",
"So nimmt ein Bild nur eine Zeile ein."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAs4AAAA5CAYAAAAm5K2kAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAACTxJREFUeJzt3X/Mneccx/H3R+vH4le7X3RaaiG1JqNYhEykNmND1iVMCNKKRSR+LSEYiY1EjD+YP0QiG13Ej0mxNcuCsTUkYrSUselW1tBsWqwdk2nT+vrj3OXZ01O92vP0nPvo+5U8Ofd9n2+v62q+Pff97f1c575SVUiSJEn63x4x6QFIkiRJ08DCWZIkSWpg4SxJkiQ1sHCWJEmSGlg4S5IkSQ0snCVJkqQGIxXOSU5McnOSu7vXhYeI259kc/ezfpQ+JUmSpEnIKM9xTvIp4P6qujLJB4GFVfWBIXEPVtXjRhinJEmSNFGjFs5bgJVVdV+SRcCGqlo2JM7CWZIkSVNt1DnOT6qq+wC611MPEfeYJBuT/CTJRSP2KUmSJI3d/MMFJPk+8OQhb334CPp5alXdm+R04JYkt1fV74b09Tbgbd3u84+g/SaLFi1qijvttNOa29y6dWtT3AMPPNDc5iTNmzevOXbZsoN+uTDUnj17mttcsGBBU9ymTZua25wGS5cubY7dtm1bU9zy5cub27znnnua4h566KHmNifphBNOaI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vd3W7a9jUEhPZY6mpXA+7LLe6o31wOpuezVwwwTHclzr5mJeA9xZVZ+e8ZY56okkpyRZ0G2fALyMwVz0W4HXdmHmaEKq6rKqWlxVSxlcd26pqjdifnojyWOTPP7ANvBy4Nd4nuuNqvoT8Mcky7pD5wJ3MKU5mpoFUJK8ksH/9A8s6/3xCQ/puJfka8BK4GRgB3A5cD3wDeCpwB+Ai6tq9hcINQZJXgz8CLid/87P/BCDec7mqAeSPJvBl2LmMbiR8Y2q+liS0xnc4TwR+AXwpqraM7mRKslK4H1V9Wrz0x9dLr7d7c4HvlpVH09yEp7neiPJCgZfsH0U8HvgLXTnPKYsR1NTOEuSJEmTNC1TNSRJkqSJsnCWJEmSGlg4S5IkSQ0snCVJkqQGFs6SJElSAwtnSZIkqYGFsyRJktTAwlmSJElq8G+r3FBSBm/pjgAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 864x28.8 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig, ax = plt.subplots(figsize=(12, .4))\n",
"ax.imshow(np.expand_dims(flattened, axis=0), cmap=\"gray\", aspect=\"auto\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Vorverarbeitung der Bilder"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Die Methode `flatten` erstellt aus einer 2d-Matrix einen 1d-Vektor.\n",
"Eine grafische Repräsentation dieses Formats ist oben zu sehen.\n",
"Dies ist notwendig, weil die bislang vorgestellten Klassifizierungs-Algorithmen nur einfache Attribute verstehen - die räumliche Nähe der Pixel kann von SVMs, Feedforward-NNs oder RandomForests so, wie sie bislang definiert worden sind, nicht ausgewertet werden.\n",
"Bei diesem Vorverarbeitungs-Schritt geht die Information über die räumliche Nähe im 2d-Bild verloren."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"images = [image.flatten() for image in digits.images]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nun liegt die Eingabe so vor, dass damit wie bislang auch verfahren werden kann.\n",
"Die Kategorie ist in `digits.target` vermerkt.\n",
"Also `digits.images[22]` ist das Bild und in `digits.target[22]` steht dann, dass es sich bei dem Bild um eine Zwei handelt."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The accuracy reached 92.5925925925926 %\n"
]
}
],
"source": [
"X_train, X_test, y_train, y_test = train_test_split(images, digits.target, test_size=0.33, random_state=42)\n",
"\n",
"clf = RandomForestClassifier(\n",
" n_estimators=10,\n",
" max_depth=7,\n",
" n_jobs=-1,\n",
" max_features=\"auto\",\n",
" class_weight=None\n",
")\n",
"clf.fit(X_train, y_train)\n",
"accuracy = clf.score(X_test, y_test)\n",
"print(\"The accuracy reached {acc} %\".format(acc=accuracy * 100))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAWUAAAFJCAYAAABD6zz2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAHgJJREFUeJzt3XuYXFWd7vHv290kARIwkqiHJEBUQPPwiGjIIHhhBDlBGWDm8RIUFWXM8QKioAwIB5UZz3gZL6jRmQjIERgQEedkNBovo44gYEIAMQQwhoG0iUMHIiK33H7nj70bKpWu6t2XvWtV1fvh2c9TtWvXWqs6zdur1l57bUUEZmaWhp5WN8DMzJ7mUDYzS4hD2cwsIQ5lM7OEOJTNzBLiUDYzS4hD2UZF0q6S/l3Sw5K+NYZy3iLph+PZtlaQ9H1Jb291O6z9OZQ7nKQ3S1oh6c+SNuTh8fJxKPr1wLOBvSLiDaMtJCKujIhjxqE9O5B0pKSQdF3d/oPz/T8rWM7HJF0x3HERcW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Pj4h7JC0EvidpI3A9cNAQRZwBLJZ0KrANeE9E3CjphnzK2ffzceUXAjfmPfU/AydHxEpJ3wRuA+4jG2IZzv8mu4PLfWRj5LXhfzfwc+DZwLsj4glJF5ONNa/Ml68cAE4s9tMxGxuvEmdmlhAPX5iZJcShbGaWEIeymVlCHMpmZglxKJuZJcShbGaWEIeymVlCHMpmZgn5/3yqhmu1FxGHAAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x432 with 2 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"predicted = clf.predict(X_test)\n",
"confusion_matrix = metrics.confusion_matrix(\n",
" y_test,\n",
" predicted\n",
")\n",
"normalized_confusion_matrix = confusion_matrix.astype('float') / confusion_matrix.sum(axis=1)[:,]\n",
" \n",
"fig, ax = plt.subplots(figsize=(6, 6))\n",
"img = ax.imshow(normalized_confusion_matrix, interpolation='nearest', cmap=plt.cm.Blues)\n",
"plt.title(\"Confusion Matrix\")\n",
"plt.colorbar(img, shrink=0.5)\n",
"\n",
"plt.ylabel('True label')\n",
"plt.xlabel('Predicted label')\n",
"plt.show()"
]
}
],
"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.6.5"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment