{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30918,"isInternetEnabled":true,"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-02-26T17:21:21.602721Z","iopub.execute_input":"2025-02-26T17:21:21.603097Z","iopub.status.idle":"2025-02-26T17:21:21.610534Z","shell.execute_reply.started":"2025-02-26T17:21:21.603063Z","shell.execute_reply":"2025-02-26T17:21:21.609582Z"}},"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-02-26T17:21:21.613744Z","iopub.execute_input":"2025-02-26T17:21:21.613990Z","iopub.status.idle":"2025-02-26T17:21:22.724245Z","shell.execute_reply.started":"2025-02-26T17:21:21.613971Z","shell.execute_reply":"2025-02-26T17:21:22.723114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:22.725174Z","iopub.execute_input":"2025-02-26T17:21:22.725385Z","iopub.status.idle":"2025-02-26T17:21:22.730289Z","shell.execute_reply.started":"2025-02-26T17:21:22.725366Z","shell.execute_reply":"2025-02-26T17:21:22.729451Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:22.731682Z","iopub.execute_input":"2025-02-26T17:21:22.731976Z","iopub.status.idle":"2025-02-26T17:21:22.768673Z","shell.execute_reply.started":"2025-02-26T17:21:22.731952Z","shell.execute_reply":"2025-02-26T17:21:22.767562Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nplt.imshow(df.iloc[15057,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:22.769911Z","iopub.execute_input":"2025-02-26T17:21:22.770170Z","iopub.status.idle":"2025-02-26T17:21:22.902618Z","shell.execute_reply.started":"2025-02-26T17:21:22.770145Z","shell.execute_reply":"2025-02-26T17:21:22.901612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:22.903408Z","iopub.execute_input":"2025-02-26T17:21:22.903601Z","iopub.status.idle":"2025-02-26T17:21:22.907327Z","shell.execute_reply.started":"2025-02-26T17:21:22.903583Z","shell.execute_reply":"2025-02-26T17:21:22.906486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:22.908134Z","iopub.execute_input":"2025-02-26T17:21:22.908381Z","iopub.status.idle":"2025-02-26T17:21:22.924123Z","shell.execute_reply.started":"2025-02-26T17:21:22.908358Z","shell.execute_reply":"2025-02-26T17:21:22.923517Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:22.925084Z","iopub.execute_input":"2025-02-26T17:21:22.925530Z","iopub.status.idle":"2025-02-26T17:21:22.958667Z","shell.execute_reply.started":"2025-02-26T17:21:22.925502Z","shell.execute_reply":"2025-02-26T17:21:22.957632Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:22.961258Z","iopub.execute_input":"2025-02-26T17:21:22.961536Z","iopub.status.idle":"2025-02-26T17:21:22.973264Z","shell.execute_reply.started":"2025-02-26T17:21:22.961491Z","shell.execute_reply":"2025-02-26T17:21:22.972315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"z=df.iloc[0:8400,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:22.974678Z","iopub.execute_input":"2025-02-26T17:21:22.974906Z","iopub.status.idle":"2025-02-26T17:21:22.990411Z","shell.execute_reply.started":"2025-02-26T17:21:22.974888Z","shell.execute_reply":"2025-02-26T17:21:22.989621Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"z.values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:22.991156Z","iopub.execute_input":"2025-02-26T17:21:22.991405Z","iopub.status.idle":"2025-02-26T17:21:23.013679Z","shell.execute_reply.started":"2025-02-26T17:21:22.991382Z","shell.execute_reply":"2025-02-26T17:21:23.012745Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nx_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:23.014423Z","iopub.execute_input":"2025-02-26T17:21:23.014658Z","iopub.status.idle":"2025-02-26T17:21:23.229316Z","shell.execute_reply.started":"2025-02-26T17:21:23.014637Z","shell.execute_reply":"2025-02-26T17:21:23.228187Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:23.230061Z","iopub.execute_input":"2025-02-26T17:21:23.230268Z","iopub.status.idle":"2025-02-26T17:21:23.235297Z","shell.execute_reply.started":"2025-02-26T17:21:23.230250Z","shell.execute_reply":"2025-02-26T17:21:23.234401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:23.236023Z","iopub.execute_input":"2025-02-26T17:21:23.236218Z","iopub.status.idle":"2025-02-26T17:21:23.254185Z","shell.execute_reply.started":"2025-02-26T17:21:23.236201Z","shell.execute_reply":"2025-02-26T17:21:23.253246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:23.254943Z","iopub.execute_input":"2025-02-26T17:21:23.255191Z","iopub.status.idle":"2025-02-26T17:21:23.270443Z","shell.execute_reply.started":"2025-02-26T17:21:23.255163Z","shell.execute_reply":"2025-02-26T17:21:23.269595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train=scaler.fit_transform(x_test)\nx_test=scaler.transform(x_test)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:23.271208Z","iopub.execute_input":"2025-02-26T17:21:23.271496Z","iopub.status.idle":"2025-02-26T17:21:23.425323Z","shell.execute_reply.started":"2025-02-26T17:21:23.271469Z","shell.execute_reply":"2025-02-26T17:21:23.424326Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:23.426175Z","iopub.execute_input":"2025-02-26T17:21:23.426428Z","iopub.status.idle":"2025-02-26T17:21:23.430500Z","shell.execute_reply.started":"2025-02-26T17:21:23.426408Z","shell.execute_reply":"2025-02-26T17:21:23.429542Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf_1=pca.fit_transform(x_train)\nx_test_trf_1=pca.fit_transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:23.431381Z","iopub.execute_input":"2025-02-26T17:21:23.431604Z","iopub.status.idle":"2025-02-26T17:21:23.669812Z","shell.execute_reply.started":"2025-02-26T17:21:23.431588Z","shell.execute_reply":"2025-02-26T17:21:23.669002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf_1.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:23.670515Z","iopub.execute_input":"2025-02-26T17:21:23.670778Z","iopub.status.idle":"2025-02-26T17:21:23.676121Z","shell.execute_reply.started":"2025-02-26T17:21:23.670754Z","shell.execute_reply":"2025-02-26T17:21:23.675161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf_1= pd.DataFrame(x_train_trf_1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:23.677064Z","iopub.execute_input":"2025-02-26T17:21:23.677339Z","iopub.status.idle":"2025-02-26T17:21:23.692596Z","shell.execute_reply.started":"2025-02-26T17:21:23.677313Z","shell.execute_reply":"2025-02-26T17:21:23.691662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf_1.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:23.693418Z","iopub.execute_input":"2025-02-26T17:21:23.693768Z","iopub.status.idle":"2025-02-26T17:21:23.712499Z","shell.execute_reply.started":"2025-02-26T17:21:23.693656Z","shell.execute_reply":"2025-02-26T17:21:23.711591Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Example scatter plot\nplt.figure(figsize=(8, 6))\nsns.scatterplot(x=x_train_trf_1[0], y=x_train_trf_1[1], hue=df['label'])\nplt.title(\"Seaborn Scatter Plot\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:23.713430Z","iopub.execute_input":"2025-02-26T17:21:23.713671Z","iopub.status.idle":"2025-02-26T17:21:24.250683Z","shell.execute_reply.started":"2025-02-26T17:21:23.713652Z","shell.execute_reply":"2025-02-26T17:21:24.249633Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\n\ny_train = y_train[:len(x_train_trf_1)]  # ✅ Ensure lengths match\n\nfig = px.scatter(\n    x=x_train_trf_1.iloc[:, 0], \n    y=x_train_trf_1.iloc[:, 1], \n    color=y_train,\n    color_discrete_sequence=px.colors.qualitative.G10  \n)\n\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:24.251705Z","iopub.execute_input":"2025-02-26T17:21:24.252062Z","iopub.status.idle":"2025-02-26T17:21:24.297627Z","shell.execute_reply.started":"2025-02-26T17:21:24.252031Z","shell.execute_reply":"2025-02-26T17:21:24.296498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:24.301102Z","iopub.execute_input":"2025-02-26T17:21:24.301310Z","iopub.status.idle":"2025-02-26T17:21:24.304752Z","shell.execute_reply.started":"2025-02-26T17:21:24.301293Z","shell.execute_reply":"2025-02-26T17:21:24.303960Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf_2=pca.fit_transform(x_train)\nx_test_trf_2=pca.fit_transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:24.306113Z","iopub.execute_input":"2025-02-26T17:21:24.306384Z","iopub.status.idle":"2025-02-26T17:21:24.721934Z","shell.execute_reply.started":"2025-02-26T17:21:24.306358Z","shell.execute_reply":"2025-02-26T17:21:24.721020Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf_2.shape ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:24.722820Z","iopub.execute_input":"2025-02-26T17:21:24.723154Z","iopub.status.idle":"2025-02-26T17:21:24.728061Z","shell.execute_reply.started":"2025-02-26T17:21:24.723129Z","shell.execute_reply":"2025-02-26T17:21:24.727272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_trf=y_train.astype(str)\nfig=px.scatter_3d(df,x=x_train_trf_2[:,0],y=x_train_trf_2[:,1],z=x_train_trf_2[:,2],color=y_train_trf)\nfig.update_layout(\n    margin=dict(l=20,r=20,t=20,b=20)\n)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:24.728739Z","iopub.execute_input":"2025-02-26T17:21:24.729007Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:24.804619Z","iopub.execute_input":"2025-02-26T17:21:24.804851Z","iopub.status.idle":"2025-02-26T17:21:24.810688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:24.811465Z","iopub.execute_input":"2025-02-26T17:21:24.811717Z","iopub.status.idle":"2025-02-26T17:21:24.828764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:24.829746Z","iopub.execute_input":"2025-02-26T17:21:24.830223Z","iopub.status.idle":"2025-02-26T17:21:24.846004Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:24.846883Z","iopub.execute_input":"2025-02-26T17:21:24.847210Z","iopub.status.idle":"2025-02-26T17:21:24.861662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components=None)\nx_train_trf_2=pca.fit_transform(x_train)\nx_test_trf_2=pca.fit_transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:24.862434Z","iopub.execute_input":"2025-02-26T17:21:24.862643Z","iopub.status.idle":"2025-02-26T17:21:26.351084Z","shell.execute_reply.started":"2025-02-26T17:21:24.862623Z","shell.execute_reply":"2025-02-26T17:21:26.350328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:26.351709Z","iopub.execute_input":"2025-02-26T17:21:26.351903Z","iopub.status.idle":"2025-02-26T17:21:26.360590Z","shell.execute_reply.started":"2025-02-26T17:21:26.351887Z","shell.execute_reply":"2025-02-26T17:21:26.359764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:26.361445Z","iopub.execute_input":"2025-02-26T17:21:26.361658Z","iopub.status.idle":"2025-02-26T17:21:26.381067Z","shell.execute_reply.started":"2025-02-26T17:21:26.361641Z","shell.execute_reply":"2025-02-26T17:21:26.380118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:26.381680Z","iopub.execute_input":"2025-02-26T17:21:26.381923Z","iopub.status.idle":"2025-02-26T17:21:26.397274Z","shell.execute_reply.started":"2025-02-26T17:21:26.381899Z","shell.execute_reply":"2025-02-26T17:21:26.396643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:26.398006Z","iopub.execute_input":"2025-02-26T17:21:26.398212Z","iopub.status.idle":"2025-02-26T17:21:26.418980Z","shell.execute_reply.started":"2025-02-26T17:21:26.398192Z","shell.execute_reply":"2025-02-26T17:21:26.418066Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T17:21:26.419754Z","iopub.execute_input":"2025-02-26T17:21:26.419999Z","iopub.status.idle":"2025-02-26T17:21:26.557392Z","shell.execute_reply.started":"2025-02-26T17:21:26.419981Z","shell.execute_reply":"2025-02-26T17:21:26.556654Z"}},"outputs":[],"execution_count":null}]}