{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#train my classifier for digits\n", "import os\n", "from skimage.io import imread\n", "from skimage.color import rgb2grey\n", "\n", "digits_images = []\n", "targets = []\n", "for digit in range(9):\n", " training_directory = 'resources/digits/'+str(digit)+'/'\n", " for filename in os.listdir(training_directory):\n", " if (filename.endswith('.png')):\n", " training_digit = imread(training_directory+filename)\n", " training_digit = rgb2grey(training_digit)#color to gray\n", " digits_images.append(training_digit.reshape(128*128))\n", " targets.append(digit)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(16384,)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "digits_images[0].shape" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "X_train, X_test, Y_train, Y_test = train_test_split(digits_images, targets, test_size=0.1, random_state=0)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n", " metric_params=None, n_jobs=1, n_neighbors=9, p=2,\n", " weights='uniform')" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.neighbors import KNeighborsClassifier\n", "import numpy as np\n", "\n", "knn = KNeighborsClassifier(n_neighbors=len(np.unique(Y_train)))\n", "knn.fit(X_train, Y_train)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9498834498834499\n" ] } ], "source": [ "score = knn.score(X_test, Y_test)\n", "print score" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([8])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "knn.predict([X_test[0]])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/wolf/anaconda2/lib/python2.7/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.\n", " from numpy.core.umath_tests import inner1d\n" ] }, { "data": { "text/plain": [ "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n", " max_depth=30, max_features='auto', max_leaf_nodes=None,\n", " min_impurity_decrease=0.0, min_impurity_split=None,\n", " min_samples_leaf=1, min_samples_split=2,\n", " min_weight_fraction_leaf=0.0, n_estimators=9, n_jobs=1,\n", " oob_score=False, random_state=None, verbose=0,\n", " warm_start=False)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.ensemble import RandomForestClassifier\n", "\n", "rc = RandomForestClassifier(n_estimators=9, max_depth=30)\n", "rc.fit(X_train, Y_train)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9662004662004662\n" ] } ], "source": [ "score = rc.score(X_test, Y_test)\n", "print score" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rc.predict([X_test[2]])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import matplotlib.pyplot as plt\n", "\n", "plt.imshow(X_test[2].reshape(128,128), cmap=\"gray\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15" } }, "nbformat": 4, "nbformat_minor": 2 }