{"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":"# 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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-22T16:21:25.302586Z","iopub.execute_input":"2022-07-22T16:21:25.303112Z","iopub.status.idle":"2022-07-22T16:21:25.313995Z","shell.execute_reply.started":"2022-07-22T16:21:25.303078Z","shell.execute_reply":"2022-07-22T16:21:25.312936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df= pd.read_csv('../input/digit-recognizer/train.csv')\ndf_test= pd.read_csv('../input/digit-recognizer/test.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:21:25.316255Z","iopub.execute_input":"2022-07-22T16:21:25.316900Z","iopub.status.idle":"2022-07-22T16:21:29.691175Z","shell.execute_reply.started":"2022-07-22T16:21:25.316853Z","shell.execute_reply":"2022-07-22T16:21:29.689951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we will be using t_destributed stochastic embedding technique for dimension reduction to just have 3 cols**","metadata":{}},{"cell_type":"code","source":"from sklearn import manifold\nX= df.drop('label',axis=1)\ny= df.label\ntsne= manifold.TSNE(n_components=3,random_state=42)\ntransformed_data= tsne.fit_transform(X)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:21:29.694164Z","iopub.execute_input":"2022-07-22T16:21:29.695027Z","iopub.status.idle":"2022-07-22T16:40:08.530046Z","shell.execute_reply.started":"2022-07-22T16:21:29.694976Z","shell.execute_reply":"2022-07-22T16:40:08.528502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we turn the 2d array to a dataframe","metadata":{}},{"cell_type":"code","source":"transformed_data= pd.DataFrame(transformed_data,columns=['1','2','3'])\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:40:08.532179Z","iopub.execute_input":"2022-07-22T16:40:08.532944Z","iopub.status.idle":"2022-07-22T16:40:08.539241Z","shell.execute_reply.started":"2022-07-22T16:40:08.532902Z","shell.execute_reply":"2022-07-22T16:40:08.538260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"now we calculate a single value to determine 70% as training and the remaining for validation","metadata":{}},{"cell_type":"code","source":"thresh =int( np.floor(.7*len(y)))\nthresh","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:43:54.751026Z","iopub.execute_input":"2022-07-22T16:43:54.752488Z","iopub.status.idle":"2022-07-22T16:43:54.761097Z","shell.execute_reply.started":"2022-07-22T16:43:54.752421Z","shell.execute_reply":"2022-07-22T16:43:54.759676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"building the model using knieghbors ","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n\n\nmodel= KNeighborsClassifier(n_neighbors=10)\nmodel.fit(transformed_data.iloc[:thresh,:],y[:thresh])\npred =model.predict(transformed_data.iloc[thresh:,:])\nmodel.score(transformed_data.iloc[thresh:,:],y[thresh:])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-22T16:43:56.940841Z","iopub.execute_input":"2022-07-22T16:43:56.942141Z","iopub.status.idle":"2022-07-22T16:43:57.928141Z","shell.execute_reply.started":"2022-07-22T16:43:56.942090Z","shell.execute_reply":"2022-07-22T16:43:57.927191Z"},"trusted":true},"execution_count":null,"outputs":[]}]}