{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:20:21.052257Z","iopub.execute_input":"2025-11-01T18:20:21.052660Z","iopub.status.idle":"2025-11-01T18:20:21.061609Z","shell.execute_reply.started":"2025-11-01T18:20:21.052630Z","shell.execute_reply":"2025-11-01T18:20:21.060434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:20:21.421869Z","iopub.execute_input":"2025-11-01T18:20:21.422204Z","iopub.status.idle":"2025-11-01T18:20:28.161698Z","shell.execute_reply.started":"2025-11-01T18:20:21.422183Z","shell.execute_reply":"2025-11-01T18:20:28.160603Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:20:28.163630Z","iopub.execute_input":"2025-11-01T18:20:28.164049Z","iopub.status.idle":"2025-11-01T18:20:28.204137Z","shell.execute_reply.started":"2025-11-01T18:20:28.164014Z","shell.execute_reply":"2025-11-01T18:20:28.203175Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(df.iloc[18511,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:20:28.205137Z","iopub.execute_input":"2025-11-01T18:20:28.205415Z","iopub.status.idle":"2025-11-01T18:20:28.553821Z","shell.execute_reply.started":"2025-11-01T18:20:28.205392Z","shell.execute_reply":"2025-11-01T18:20:28.552841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:20:28.555590Z","iopub.execute_input":"2025-11-01T18:20:28.555905Z","iopub.status.idle":"2025-11-01T18:20:28.561416Z","shell.execute_reply.started":"2025-11-01T18:20:28.555882Z","shell.execute_reply":"2025-11-01T18:20:28.560472Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test = train_test_split(X,y,random_state = 42, test_size = .2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:20:28.562389Z","iopub.execute_input":"2025-11-01T18:20:28.562687Z","iopub.status.idle":"2025-11-01T18:20:28.928887Z","shell.execute_reply.started":"2025-11-01T18:20:28.562657Z","shell.execute_reply":"2025-11-01T18:20:28.927833Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:20:49.221778Z","iopub.execute_input":"2025-11-01T18:20:49.222136Z","iopub.status.idle":"2025-11-01T18:20:49.228618Z","shell.execute_reply.started":"2025-11-01T18:20:49.222109Z","shell.execute_reply":"2025-11-01T18:20:49.227629Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:20:49.230493Z","iopub.execute_input":"2025-11-01T18:20:49.230873Z","iopub.status.idle":"2025-11-01T18:20:49.300440Z","shell.execute_reply.started":"2025-11-01T18:20:49.230849Z","shell.execute_reply":"2025-11-01T18:20:49.299390Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:20:56.936665Z","iopub.execute_input":"2025-11-01T18:20:56.937902Z","iopub.status.idle":"2025-11-01T18:20:56.942328Z","shell.execute_reply.started":"2025-11-01T18:20:56.937861Z","shell.execute_reply":"2025-11-01T18:20:56.941455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:20:56.943989Z","iopub.execute_input":"2025-11-01T18:20:56.944336Z","iopub.status.idle":"2025-11-01T18:20:57.168416Z","shell.execute_reply.started":"2025-11-01T18:20:56.944310Z","shell.execute_reply":"2025-11-01T18:20:57.167370Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:20:57.169331Z","iopub.execute_input":"2025-11-01T18:20:57.169635Z","iopub.status.idle":"2025-11-01T18:21:10.942704Z","shell.execute_reply.started":"2025-11-01T18:20:57.169611Z","shell.execute_reply":"2025-11-01T18:21:10.941834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:21:10.944353Z","iopub.execute_input":"2025-11-01T18:21:10.944714Z","iopub.status.idle":"2025-11-01T18:21:10.953160Z","shell.execute_reply.started":"2025-11-01T18:21:10.944679Z","shell.execute_reply":"2025-11-01T18:21:10.952285Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:21:10.954197Z","iopub.execute_input":"2025-11-01T18:21:10.954489Z","iopub.status.idle":"2025-11-01T18:21:10.966698Z","shell.execute_reply.started":"2025-11-01T18:21:10.954466Z","shell.execute_reply":"2025-11-01T18:21:10.965510Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:21:10.967772Z","iopub.execute_input":"2025-11-01T18:21:10.968056Z","iopub.status.idle":"2025-11-01T18:21:10.983790Z","shell.execute_reply.started":"2025-11-01T18:21:10.968033Z","shell.execute_reply":"2025-11-01T18:21:10.982786Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:21:10.984782Z","iopub.execute_input":"2025-11-01T18:21:10.985052Z","iopub.status.idle":"2025-11-01T18:21:11.759669Z","shell.execute_reply.started":"2025-11-01T18:21:10.985031Z","shell.execute_reply":"2025-11-01T18:21:11.758847Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components = 100)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:21:11.762119Z","iopub.execute_input":"2025-11-01T18:21:11.762523Z","iopub.status.idle":"2025-11-01T18:21:14.856984Z","shell.execute_reply.started":"2025-11-01T18:21:11.762495Z","shell.execute_reply":"2025-11-01T18:21:14.855878Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:21:14.858132Z","iopub.execute_input":"2025-11-01T18:21:14.858474Z","iopub.status.idle":"2025-11-01T18:21:14.864762Z","shell.execute_reply.started":"2025-11-01T18:21:14.858431Z","shell.execute_reply":"2025-11-01T18:21:14.863987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn = KNeighborsClassifier()\nknn.fit(X_train_trf,y_train)\ny_pred = knn.predict(X_test_trf)\naccuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:21:14.865624Z","iopub.execute_input":"2025-11-01T18:21:14.865916Z","iopub.status.idle":"2025-11-01T18:21:16.551427Z","shell.execute_reply.started":"2025-11-01T18:21:14.865889Z","shell.execute_reply":"2025-11-01T18:21:16.550482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\n\n# Ensure y_train is string type for coloring\ny_train_trf = y_train.astype(str)\n\n# Scatter plot using first two features\nfig = px.scatter(\n    x = X_train_trf[:, 0],\n    y = X_train_trf[:, 1],\n    color = y_train_trf,\n    color_discrete_sequence = px.colors.qualitative.G10\n)\n\nfig.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:23:06.385574Z","iopub.execute_input":"2025-11-01T18:23:06.385966Z","iopub.status.idle":"2025-11-01T18:23:10.735518Z","shell.execute_reply.started":"2025-11-01T18:23:06.385936Z","shell.execute_reply":"2025-11-01T18:23:10.734394Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ #eigen value","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:24:16.587206Z","iopub.execute_input":"2025-11-01T18:24:16.587997Z","iopub.status.idle":"2025-11-01T18:24:16.595732Z","shell.execute_reply.started":"2025-11-01T18:24:16.587954Z","shell.execute_reply":"2025-11-01T18:24:16.594690Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_ #eigen vector","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:24:46.641594Z","iopub.execute_input":"2025-11-01T18:24:46.641952Z","iopub.status.idle":"2025-11-01T18:24:46.649981Z","shell.execute_reply.started":"2025-11-01T18:24:46.641928Z","shell.execute_reply":"2025-11-01T18:24:46.648811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T18:28:46.941931Z","iopub.execute_input":"2025-11-01T18:28:46.942276Z","iopub.status.idle":"2025-11-01T18:28:46.949288Z","shell.execute_reply.started":"2025-11-01T18:28:46.942254Z","shell.execute_reply":"2025-11-01T18:28:46.948264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}