{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","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 pandas as pd\nimport numpy as np\nfrom sklearn.cluster import KMeans\nfrom sklearn.model_selection import train_test_split\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\ndata=pd.read_csv(\"../input/train.csv\")\ndata.head()\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"825ca03b9499e3b76da101be643357fba4eba8a4"},"cell_type":"code","source":"image=data.iloc[:,1:]\nlabel=data.iloc[:,:1]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08fc8eaf74ff2f75d300886a0440dea45afe3a87"},"cell_type":"code","source":"kmeans= KMeans(n_clusters=10)\nkmeans.fit(image)\nla=kmeans.labels_","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b33c9eb6c0da4598718264936c3e53bd2786875a"},"cell_type":"code","source":"di={}\nfor j in range(10):\n    d={}\n    for i in range(len(label)):\n        if kmeans.labels_[i]==j:\n            d[label.label[i]]=d.get(label.label[i],0)+1\n    di[j]=next(iter(d))\ndi","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"23abae2c4300d50ffa0804612d90efe51c2894e2"},"cell_type":"code","source":"# checking the accuracy\nc=0\nfor i in range(len(label)):\n    if di[kmeans.labels_[i]]==label.label[i]:\n        c+=1\nprint(\"accuracy =\",c/len(label)*100,\"%\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54a099dbd425de2ba9935c66f0c403c867b0baca"},"cell_type":"code","source":"di={}\nfor j in range(10):\n    d={}\n    for i in range(len(label)):\n        if kmeans.labels_[i]==j:\n            d[label.label[i]]=d.get(label.label[i],0)+1\n    di[j]=next(iter(d))\ndi","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b03af7b2cc5b21739b595c950661120e68207a7b"},"cell_type":"markdown","source":"using knn to classify the images****"},{"metadata":{"trusted":true,"_uuid":"5988f1a41246b42bf229d1ddc78407f765ecdd12"},"cell_type":"code","source":"image=data.iloc[:,1:]\nlabel=data.iloc[:,:1]\nfrom sklearn.neighbors import KNeighborsClassifier as kn\nknn= kn(n_neighbors=10)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7e6f64b162f2ca0bb269597da4409ea9cad7f3cd"},"cell_type":"code","source":"image.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ec037e54585a67a98923a72c1d25eeca730afbb9"},"cell_type":"code","source":"image=data.iloc[:,1:]\nlabel=data.iloc[:,:1]\nfrom sklearn.neighbors import KNeighborsClassifier as kn\nknn= kn(n_neighbors=10)\nx_train,x_test,y_train,y_test = train_test_split(image,label,test_size = 0.2,random_state = 100) \nknn.fit(x_train,y_train)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf11c2f90fabff7d84108e93fa3d8e6548c6f67d"},"cell_type":"code","source":"predic=knn.predict(x_test)\n# metrics.accuracy_score(y_test,predic)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6a647d1ff8735c199c3acbce804377351b7bb0f3"},"cell_type":"code","source":"from sklearn import metrics\nmetrics.accuracy_score(y_test,predic)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b837c011af0908ff30ed57773b3a531b3db65fb5"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"51fabea49e861b0bdad599a372677729415cbd25"},"cell_type":"code","source":"df=pd.read_csv(\"../input/test.csv\")\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb22470c8b735c67c34ae62bfee455eb2662910a"},"cell_type":"code","source":"predic=knn.predict(df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"92e138d311e543608271b219570ef12c892dddb0"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e44e865dd23a6592d3b01abd830bcc48dedbe418"},"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.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}