{"cells":[{"metadata":{"_cell_guid":"00c3d68f-1afc-49f2-a9cd-24cfaf9220c2","_uuid":"5a09dfc52fc6631134aa20e88aaddf06e4c0bece","trusted":true,"collapsed":true},"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt, matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import svm\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nimport os\n%matplotlib inline","execution_count":21,"outputs":[]},{"metadata":{"_cell_guid":"5f36a0eb-e694-407d-ad69-71bad38f5886","_uuid":"c4a86d7d9f34808ebe6e4b796c4e4a60212f0e3f","collapsed":true,"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')","execution_count":2,"outputs":[]},{"metadata":{"_cell_guid":"13051324-a602-4833-9fa8-9fb2b7974393","_uuid":"be1a72917f411e4455c0e9a3a8ec220fb4176bf5","trusted":true},"cell_type":"code","source":"train.shape","execution_count":3,"outputs":[]},{"metadata":{"_cell_guid":"acae8457-daea-40ed-b31d-10decf5bf622","_uuid":"0586e1209d69945b32e0f0bc1411b5ecc2e50602","collapsed":true,"trusted":true},"cell_type":"code","source":"images = train.iloc[:,1:]\nlabels = train.iloc[:,:1]","execution_count":4,"outputs":[]},{"metadata":{"_cell_guid":"bcceaffa-4575-4e8c-948d-d7da08d03d95","_uuid":"31cd85da3973b5c9bf0c23d75a04db6a0324b063","collapsed":true,"trusted":true},"cell_type":"code","source":"train_X,val_X, train_y, val_y = train_test_split(images, labels, test_size=0.8, stratify = labels)","execution_count":5,"outputs":[]},{"metadata":{"_cell_guid":"5eee7a5b-981c-4229-bde8-fc1c1dff4118","_uuid":"d53c5df8d38bb30b1e5554ba019af49fda2cc882","trusted":true},"cell_type":"code","source":"print(train_X.shape, val_X.shape, train_y.shape, val_y.shape)","execution_count":6,"outputs":[]},{"metadata":{"_cell_guid":"b4fd6289-efab-4c7f-85f1-137924d8509a","_uuid":"4a2c516dfe6cf61376888a7513129984f4c6c4ba","trusted":true},"cell_type":"code","source":"i=1\nimg = train_X.iloc[i]\nimg = img.values.reshape(28,28)\nplt.imshow(img,cmap='Greys')","execution_count":11,"outputs":[]},{"metadata":{"_cell_guid":"3311e502-732e-4f4e-ba72-b789c0a135a1","_uuid":"efbd73e7cdb53ad4032ec990191be96c9f8cb4cd","trusted":true},"cell_type":"code","source":"lr = LogisticRegression(n_jobs= 8 )\nlr.fit(train_X, train_y)","execution_count":12,"outputs":[]},{"metadata":{"_cell_guid":"3a62a443-c490-488f-bfaa-50601ab63656","_uuid":"acadccc0fbd0f46bdc4e80aa416d6d0f29780070","collapsed":true,"trusted":true},"cell_type":"code","source":"val_predict = lr.predict(val_X)","execution_count":13,"outputs":[]},{"metadata":{"_cell_guid":"74675b20-757d-4716-83d3-07aa45190f83","_uuid":"da9d9bd22b71bb79b2047cdfa9cee653a86df756","trusted":true},"cell_type":"code","source":"(((val_predict == val_y['label'].values).sum()/val_predict.shape) * 100) [0] #84.01","execution_count":14,"outputs":[]},{"metadata":{"_cell_guid":"8d9e2308-9ea7-4137-a584-71b937032f97","_uuid":"4aec43687c14fd98aafbe4959dddbf3ffdb2d02c","collapsed":true,"trusted":true},"cell_type":"code","source":"train_X = train_X.clip(lower=0,upper=1)\nval_X = val_X.clip(lower=0,upper=1)\ntest= test.clip(lower=0,upper=1)","execution_count":15,"outputs":[]},{"metadata":{"_cell_guid":"3866ca1a-6032-4709-834f-3e0a9beef2fa","_uuid":"70c595c55345a14b85bc6ab989993c285d670fb5","trusted":true},"cell_type":"code","source":"i=55\nimg = train_X.iloc[i]\nimg = img.values.reshape(28,28)\nplt.imshow(img,cmap='Greys')","execution_count":17,"outputs":[]},{"metadata":{"_cell_guid":"0526eaca-6d78-4994-abe9-2eead12e6cae","_uuid":"3be577bb34d543070ca00c60fd83adab896e6a19","trusted":true},"cell_type":"code","source":"lr = LogisticRegression()\nlr.fit(train_X, train_y)","execution_count":18,"outputs":[]},{"metadata":{"_cell_guid":"7daf116d-f7d3-4730-89b3-e988dfe7945c","_uuid":"62bad302d0d5de01ca1d4ece4108be82330b8416","trusted":true},"cell_type":"code","source":"val_predict = lr.predict(val_X)\n(((val_predict == val_y['label'].values).sum()/val_predict.shape) * 100) [0] #88.78","execution_count":19,"outputs":[]},{"metadata":{"_uuid":"e873134555054fd26377a448b778a13608bfb345"},"cell_type":"markdown","source":"Let's try different neighbor combinaion with knn."},{"metadata":{"trusted":true,"_uuid":"e0fcecf5be900f185cd5a0ae46aa66464588261f"},"cell_type":"code","source":"knnReg = KNeighborsClassifier(n_neighbors=10)\nknnReg.fit(train_X, train_y)\nval_predict_knn = knnReg.predict(val_X)\n(((val_predict_knn == val_y['label'].values).sum()/val_predict_knn.shape) * 100) [0] #88.78","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"df454eb4-a8d0-48a3-be67-4d10aa58ea04","_uuid":"1493f2174d8d2b2ad41e24cd146d2416e3327a52","collapsed":true,"trusted":true},"cell_type":"code","source":"test_predict = knnReg.predict(test)\nsubmission = pd.DataFrame(test_predict).reset_index()\nsubmission.columns=['ImageId','Label']\nsubmission.ImageId =  submission.ImageId + 1\nsubmission.to_csv(\"knn_submission.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"643de7b6-4fe9-4f3e-a27c-0b5a0a742ace","_uuid":"69ca979480431407987c96efe6dc8484f65f2811","collapsed":true,"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}