{"cells":[{"metadata":{"trusted":true,"_uuid":"27966837292c5c27914766bda72e81d8db1b547e"},"cell_type":"code","source":"import os\nos.listdir(\"../input\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c312557806b41f802c54f2a10b90d0cc2c18a26a"},"cell_type":"code","source":"# import library\nimport pandas as pd\nimport matplotlib.pyplot as plt, matplotlib.image as implt\nfrom sklearn.model_selection import train_test_split, GridSearchCV\nfrom sklearn import svm\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5be821162990a26d1a29ec0f427edf074520b5ea"},"cell_type":"code","source":"# import data and split label and image\nlabeled_image = pd.read_csv(\"../input/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d327f6bd721424f2e18c4eaa5c660d29e3d917c"},"cell_type":"code","source":"label = labeled_image.iloc[0:5000,:1]\nimage = labeled_image.iloc[0:5000,1:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1d8b1f64c452cc3d63bc4e844896d815176ece7c"},"cell_type":"code","source":"# split data into 2 subdata like training dataset and testing dataset\nlabel_train, label_test, image_train, image_test = train_test_split(label, image, test_size=0.2, random_state=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"67415e4663e1ede770ee7d0b6ca6e7b7da3b2359"},"cell_type":"code","source":"# we use support vector machine to classifier\n# Note: return `label_train` to 1-D array by ravel() function\nclf = svm.SVC()\nclf.fit(image_train,label_train.values.ravel())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"26ce20d40ece1e8a7064994cec63b59223a6aed9"},"cell_type":"code","source":"# defaults parameters of estimator \"SVC\"\nclf.get_params()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c28e85cf9a971cc2e2cc378384cf0de75aaf26b1"},"cell_type":"code","source":"pred = clf.predict(image_test)\ntest = label_test.values.ravel()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"20cac9f150d6fbec22deeec7da5472141d5f75b7"},"cell_type":"code","source":"print(\"prediction:\", pred[:10])\nprint(\"real label:\", test[:10])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef021b26e5989e35b7ee509cef68bdf97db17c18"},"cell_type":"code","source":"print(\"mean accuracy: \", clf.score(image_test, label_test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17885bf4ec29ab9d4237dc3abf11341cbf8a0b2f"},"cell_type":"code","source":"# show 9 image:\nfor i in range(9):\n    plt.subplot(191 + i)\n    img = image_test.iloc[i].values.reshape((28,28))\n    plt.imshow(img, cmap='gray')\n    plt.title(label_test.iloc[i,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"09d7570ef94a03000a2315c441ef05b4aad08616"},"cell_type":"code","source":"def show_detail_digit(data_test, results_of_model, number_of_digit):\n    index_of_digit = [results_of_model == number_of_digit][0]\n    f, axes = plt.subplots(9,9, sharex=True, sharey=True, figsize=(16,8))\n    for r in range(9):\n        for c in range(9):\n            img = data_test.iloc[index_of_digit].iloc[r*9+c].values.reshape((28,28))\n            axes[r,c].imshow(img, cmap='binary')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aefb99fad982d69513e3041eda84aa3f892230cd"},"cell_type":"code","source":"# show image black white\nimage_train.iloc[image_train>0] = 1\nimage_test.iloc[image_test>0] = 1\n\n#show_detail_digit(image_train,label_train.values.ravel(), 3)\n\nfor i in range(9):\n    plt.subplot(191 + i)\n    img = image_train.iloc[i].values.reshape((28,28))\n    plt.imshow(img, cmap='binary')\n    plt.title(label_train.iloc[i,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cd71302fc891b7c4e25144719e72c5a4bb822aa6"},"cell_type":"code","source":"# We repeat the classification like above to see how's the performance going:\n#clf = svm.SVC()\nclf.fit(image_train,label_train.values.ravel())\n\npred = clf.predict(image_test)\ntest = label_test.values.ravel()\n\nprint(\"prediction:\", pred[:10])\nprint(\"real label:\", test[:10])\n\nprint(\"mean accuracy: \", clf.score(image_test, label_test))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dc35d722e4580e1909cf2a44f33224a01029cfe2"},"cell_type":"code","source":"# the image that was be failed to predict\n\nfor i in range(9):\n    plt.subplot(191 + i)\n    img = image_test.iloc[i].values.reshape((28,28))\n    plt.imshow(img, cmap='binary')\n    plt.title(pred[i], color='red')\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c0a885c463319cb982d89b05dc03520ea64c632e"},"cell_type":"markdown","source":"## Improve model by using hyperparameter, in this case, we use GridSearchCV to exhausively find the combination of hyperparameter that have best model's performance"},{"metadata":{"trusted":false,"_uuid":"e8a5f57bb1ec01c013a376d241da7568331b5984"},"cell_type":"code","source":"estimator = svm.SVC(kernel='rbf')\nprint(\"defaults parameters: \", estimator.get_params())","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"1deba0b3be6e9b78f781fb0452e6d64425d83914"},"cell_type":"code","source":"label_train_1D_array = label_train.values.ravel()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"bd90bd424292f01439d0b24a9db903ea4a4d277c"},"cell_type":"code","source":"param_grid = {\n    'C':[1, 5 , 7, 10],\n    'gamma': [0.001, 0.01, 0,1]\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"19f2bb5ecf9c2d1cd2733afb58e7d8f15bf6e980"},"cell_type":"code","source":"svc_params_selection = GridSearchCV(estimator, param_grid)\nsvc_params_selection.fit(image_train,label_train_1D_array)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"5b77e5a0a50115861b900917f9c0e7355e140828"},"cell_type":"code","source":"print(svc_params_selection.best_params_)\nprint(svc_params_selection.best_score_)\nprint(svc_params_selection.best_estimator_)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"1daa5b89b9e12fe7f19d06cd9af24a8de78567fb"},"cell_type":"code","source":"pred = svc_params_selection.predict(image_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"d4bd915e6ae8c8cdc88b86748f4fce047b6fd7fb"},"cell_type":"code","source":"print(\"prediction:\", pred[:10])\nprint(\"real label:\", test[:10])\n\nprint(\"mean accuracy: \", svc_params_selection.score(image_test, label_test))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"037a29d91393d146a89acff1562efac9a4f40018"},"cell_type":"markdown","source":"## Run model on the test dataset"},{"metadata":{"trusted":false,"_uuid":"53b81d26bf205719145720d43637fc2e1db61079"},"cell_type":"code","source":"test_data=pd.read_csv('../input/test.csv')\ntest_data[test_data>0]=1\nresults=svc_params_selection.predict(test_data[0:5000])","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"6a04e19e82ccefd715c97fe196e5694b76ad6119"},"cell_type":"code","source":"df = pd.DataFrame(results)\ndf.index.name='ImageId'\ndf.index+=1\ndf.columns=['Label']\ndf.to_csv('sample_submission.csv', header=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"b5155c99da1e0e09f008a9c44573aa7e133c078e"},"cell_type":"code","source":"# show 10 image results:\nf, axes = plt.subplots(9,9, sharex=True, sharey=True, figsize=(16,16))\nfor i in range(9):\n    for j in range(9):\n        img = test_data.iloc[i*9+j].values.reshape((28,28))\n        axes[i,j].imshow(img, cmap='binary')\n        axes[i,j].set_title(results[i*9+j], color='red')\n        \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"108416037f8957413bb4c2cc502e7a9798f2e99b"},"cell_type":"code","source":"# show all of digit number:\nshow_detail_digit(test_data, results, 9)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}