{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"a61ef9de-f818-da18-e47c-85f38d1122dd"},"source":"\n\nHello!\n\nA very quick attempt at the digits competition dataset. Before using more complicated algorithms (NN, ...), I wanted to see how good (or bad) a simple idea can be. My idea (I am sure other had it before) is that basically ones look like one, and so on. So I use the training set to produce a mean map of digit, with a associated variance map, and calculate a simple chi2, for each image in the test sample. This very simple script (unless I am wrong, it compresses the training sample into just two features), has a ~80% accuracy.\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"3c4d93ba-bbf3-60c4-43f6-d29117259725"},"outputs":[],"source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as nm # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b57c3c70-b4c8-432c-4e6f-1c280a264f7f"},"outputs":[],"source":"A=nm.loadtxt('../input/train.csv', skiprows=1, delimiter=',')\nB = nm.loadtxt('../input/test.csv', skiprows=1,  delimiter=',')\nprint('There are {} training examples'.format(A.shape[0]))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"96186d2a-9da4-e608-2022-1203ff51783b"},"outputs":[],"source":"X=A[:, 1:]/255.\ny=nm.int32(A[:,0])\n\nX_train=X[:40000,:]\ny_train=y[:40000]\n\nX_test=X[40000:,:]\ny_test=y[40000:]\n\n## Plot one digit for illustration\nid=244\n\nplt.figure(1)\nplt.clf()\nplt.imshow(X[id, :].reshape(28, 28), cmap=\"Greys\")\nplt.title('True value = {}'.format(y[id]))\n"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"61faac17-605a-d50e-0c5f-ed2211b9b9b1"},"outputs":[],"source":"means=[]\nvars=[]\nidv=[]\nfor i in range(10):\n    means.append(nm.mean(X[nm.where(y==i)[0], :], axis=0))\n    vars.append(nm.var(X[nm.where(y==i)[0], :], axis=0))\n    idv.append(nm.where(vars[i]!=0))\n\ndef testi(t,i):\n    return nm.sum((((t-means[i])**2)[idv[i]])/vars[i][idv[i]])\n\n\np=[nm.argmin([testi(X_test[j, :], i) for i in range(10)]) for j in range(X_test.shape[0])]\n\nprint(nm.mean(nm.array(p)==y_test) *100)\n"}],"metadata":{"_change_revision":0,"_is_fork":false,"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.0"}},"nbformat":4,"nbformat_minor":0}