{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#importing libraries\nimport matplotlib.pyplot as plt\nfrom sklearn import datasets\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.neighbors import KNeighborsClassifier","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-02T07:50:35.659920Z","iopub.execute_input":"2021-08-02T07:50:35.660324Z","iopub.status.idle":"2021-08-02T07:50:36.870960Z","shell.execute_reply.started":"2021-08-02T07:50:35.660286Z","shell.execute_reply":"2021-08-02T07:50:36.869946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load the data\ndigits=datasets.load_digits()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:50:36.872690Z","iopub.execute_input":"2021-08-02T07:50:36.873115Z","iopub.status.idle":"2021-08-02T07:50:36.987753Z","shell.execute_reply.started":"2021-08-02T07:50:36.873072Z","shell.execute_reply":"2021-08-02T07:50:36.986817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Data allocation\nlen(digits.data)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:50:46.140985Z","iopub.execute_input":"2021-08-02T07:50:46.141447Z","iopub.status.idle":"2021-08-02T07:50:46.149604Z","shell.execute_reply.started":"2021-08-02T07:50:46.141414Z","shell.execute_reply":"2021-08-02T07:50:46.148454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Data allocation\nlen(digits.target)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:50:59.842818Z","iopub.execute_input":"2021-08-02T07:50:59.843160Z","iopub.status.idle":"2021-08-02T07:50:59.848594Z","shell.execute_reply.started":"2021-08-02T07:50:59.843131Z","shell.execute_reply":"2021-08-02T07:50:59.847825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Datavisualization\nplt.imshow(digits.images[5],cmap=plt.cm.gray_r,interpolation='nearest')","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:51:09.051513Z","iopub.execute_input":"2021-08-02T07:51:09.051993Z","iopub.status.idle":"2021-08-02T07:51:09.232878Z","shell.execute_reply.started":"2021-08-02T07:51:09.051962Z","shell.execute_reply":"2021-08-02T07:51:09.231777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#view\ndigits.target[5]","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:51:23.301917Z","iopub.execute_input":"2021-08-02T07:51:23.302303Z","iopub.status.idle":"2021-08-02T07:51:23.308715Z","shell.execute_reply.started":"2021-08-02T07:51:23.302243Z","shell.execute_reply":"2021-08-02T07:51:23.307601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Features allocation\nSamplefeatures=digits.data\nlabels=digits.target","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:51:32.370766Z","iopub.execute_input":"2021-08-02T07:51:32.371244Z","iopub.status.idle":"2021-08-02T07:51:32.375573Z","shell.execute_reply.started":"2021-08-02T07:51:32.371213Z","shell.execute_reply":"2021-08-02T07:51:32.374502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split into training and test set\ntrainimg,testimg,trainlab,testlab=train_test_split(Samplefeatures,labels,test_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:51:42.466918Z","iopub.execute_input":"2021-08-02T07:51:42.467307Z","iopub.status.idle":"2021-08-02T07:51:42.473171Z","shell.execute_reply.started":"2021-08-02T07:51:42.467275Z","shell.execute_reply":"2021-08-02T07:51:42.471992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(trainimg)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:52:08.858831Z","iopub.execute_input":"2021-08-02T07:52:08.859340Z","iopub.status.idle":"2021-08-02T07:52:08.865346Z","shell.execute_reply.started":"2021-08-02T07:52:08.859296Z","shell.execute_reply":"2021-08-02T07:52:08.864403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(testimg)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:52:04.456837Z","iopub.execute_input":"2021-08-02T07:52:04.457209Z","iopub.status.idle":"2021-08-02T07:52:04.463689Z","shell.execute_reply.started":"2021-08-02T07:52:04.457170Z","shell.execute_reply":"2021-08-02T07:52:04.462510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fit the algorithm\nKnn=KNeighborsClassifier(n_neighbors=7)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:52:16.311004Z","iopub.execute_input":"2021-08-02T07:52:16.311407Z","iopub.status.idle":"2021-08-02T07:52:16.315194Z","shell.execute_reply.started":"2021-08-02T07:52:16.311351Z","shell.execute_reply":"2021-08-02T07:52:16.314478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Knn.fit(trainimg,trainlab)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:52:26.864853Z","iopub.execute_input":"2021-08-02T07:52:26.865379Z","iopub.status.idle":"2021-08-02T07:52:26.884987Z","shell.execute_reply.started":"2021-08-02T07:52:26.865343Z","shell.execute_reply":"2021-08-02T07:52:26.884124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Knn.predict(testimg[[359]])","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:52:35.209106Z","iopub.execute_input":"2021-08-02T07:52:35.209454Z","iopub.status.idle":"2021-08-02T07:52:35.216689Z","shell.execute_reply.started":"2021-08-02T07:52:35.209423Z","shell.execute_reply":"2021-08-02T07:52:35.215808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Knn.score(testimg,testlab))","metadata":{"execution":{"iopub.status.busy":"2021-08-02T07:52:47.443117Z","iopub.execute_input":"2021-08-02T07:52:47.443501Z","iopub.status.idle":"2021-08-02T07:52:47.523696Z","shell.execute_reply.started":"2021-08-02T07:52:47.443472Z","shell.execute_reply":"2021-08-02T07:52:47.522627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}