{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.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":31259,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# In this notebook, I practice Principal Component Analysis (PCA) using a digit recognition dataset. I apply PCA step by step to understand how dimensionality reduction works in machine learning. In the final part,\n\n# I also solve another dataset to reinforce the concepts and verify my understanding of PCA.","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nprint(\"\\nLibraries Loaded\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:50:59.386791Z","iopub.execute_input":"2026-01-31T10:50:59.387577Z","iopub.status.idle":"2026-01-31T10:50:59.398421Z","shell.execute_reply.started":"2026-01-31T10:50:59.387538Z","shell.execute_reply":"2026-01-31T10:50:59.396616Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Loading and Understanding Dataset","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:50:59.404846Z","iopub.execute_input":"2026-01-31T10:50:59.405927Z","iopub.status.idle":"2026-01-31T10:51:01.690168Z","shell.execute_reply.started":"2026-01-31T10:50:59.405872Z","shell.execute_reply":"2026-01-31T10:51:01.687818Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:01.692467Z","iopub.execute_input":"2026-01-31T10:51:01.694541Z","iopub.status.idle":"2026-01-31T10:51:01.716001Z","shell.execute_reply.started":"2026-01-31T10:51:01.694452Z","shell.execute_reply":"2026-01-31T10:51:01.714455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[1672,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:01.717943Z","iopub.execute_input":"2026-01-31T10:51:01.718311Z","iopub.status.idle":"2026-01-31T10:51:02.136814Z","shell.execute_reply.started":"2026-01-31T10:51:01.718280Z","shell.execute_reply":"2026-01-31T10:51:02.135759Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Performing train_test_split","metadata":{}},{"cell_type":"code","source":"x = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:02.139040Z","iopub.execute_input":"2026-01-31T10:51:02.139689Z","iopub.status.idle":"2026-01-31T10:51:02.146612Z","shell.execute_reply.started":"2026-01-31T10:51:02.139608Z","shell.execute_reply":"2026-01-31T10:51:02.145337Z"}},"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,test_size = 0.2,random_state = 42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:02.147900Z","iopub.execute_input":"2026-01-31T10:51:02.148271Z","iopub.status.idle":"2026-01-31T10:51:02.426618Z","shell.execute_reply.started":"2026-01-31T10:51:02.148227Z","shell.execute_reply":"2026-01-31T10:51:02.425666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:02.427928Z","iopub.execute_input":"2026-01-31T10:51:02.428316Z","iopub.status.idle":"2026-01-31T10:51:02.434865Z","shell.execute_reply.started":"2026-01-31T10:51:02.428273Z","shell.execute_reply":"2026-01-31T10:51:02.433781Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Apply Scaling","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:02.435973Z","iopub.execute_input":"2026-01-31T10:51:02.436265Z","iopub.status.idle":"2026-01-31T10:51:02.455976Z","shell.execute_reply.started":"2026-01-31T10:51:02.436239Z","shell.execute_reply":"2026-01-31T10:51:02.454410Z"}},"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":"2026-01-31T10:51:02.458062Z","iopub.execute_input":"2026-01-31T10:51:02.458511Z","iopub.status.idle":"2026-01-31T10:51:03.008023Z","shell.execute_reply.started":"2026-01-31T10:51:02.458469Z","shell.execute_reply":"2026-01-31T10:51:03.006974Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Performing PCA","metadata":{}},{"cell_type":"code","source":"#PCA\nfrom sklearn.decomposition import PCA\npca = PCA(n_components=50)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:03.009184Z","iopub.execute_input":"2026-01-31T10:51:03.009822Z","iopub.status.idle":"2026-01-31T10:51:03.014985Z","shell.execute_reply.started":"2026-01-31T10:51:03.009775Z","shell.execute_reply":"2026-01-31T10:51:03.013669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf = pca.fit_transform(x_train)\nx_test_trf = pca.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:03.018942Z","iopub.execute_input":"2026-01-31T10:51:03.019311Z","iopub.status.idle":"2026-01-31T10:51:03.780316Z","shell.execute_reply.started":"2026-01-31T10:51:03.019280Z","shell.execute_reply":"2026-01-31T10:51:03.779525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:03.781191Z","iopub.execute_input":"2026-01-31T10:51:03.781497Z","iopub.status.idle":"2026-01-31T10:51:03.786384Z","shell.execute_reply.started":"2026-01-31T10:51:03.781467Z","shell.execute_reply":"2026-01-31T10:51:03.785613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:03.788050Z","iopub.execute_input":"2026-01-31T10:51:03.788795Z","iopub.status.idle":"2026-01-31T10:51:03.811601Z","shell.execute_reply.started":"2026-01-31T10:51:03.788757Z","shell.execute_reply":"2026-01-31T10:51:03.810673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:03.813844Z","iopub.execute_input":"2026-01-31T10:51:03.814243Z","iopub.status.idle":"2026-01-31T10:51:03.844390Z","shell.execute_reply.started":"2026-01-31T10:51:03.814207Z","shell.execute_reply":"2026-01-31T10:51:03.842704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(x_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:03.845583Z","iopub.execute_input":"2026-01-31T10:51:03.845939Z","iopub.status.idle":"2026-01-31T10:51:05.084433Z","shell.execute_reply.started":"2026-01-31T10:51:03.845905Z","shell.execute_reply":"2026-01-31T10:51:05.083608Z"}},"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":"2026-01-31T10:51:05.085924Z","iopub.execute_input":"2026-01-31T10:51:05.086303Z","iopub.status.idle":"2026-01-31T10:51:05.094330Z","shell.execute_reply.started":"2026-01-31T10:51:05.086272Z","shell.execute_reply":"2026-01-31T10:51:05.093235Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# transforming to a 2D coordinate system","metadata":{}},{"cell_type":"code","source":"# transforming to a 2D coordinate system\npca = PCA(n_components=2)\nx_train_trf = pca.fit_transform(x_train)\nx_test_trf = pca.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:05.096254Z","iopub.execute_input":"2026-01-31T10:51:05.096986Z","iopub.status.idle":"2026-01-31T10:51:05.785898Z","shell.execute_reply.started":"2026-01-31T10:51:05.096948Z","shell.execute_reply":"2026-01-31T10:51:05.785095Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_trf = y_train.astype(str)\nfig = px.scatter(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                )\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:05.786808Z","iopub.execute_input":"2026-01-31T10:51:05.787120Z","iopub.status.idle":"2026-01-31T10:51:05.928164Z","shell.execute_reply.started":"2026-01-31T10:51:05.787090Z","shell.execute_reply":"2026-01-31T10:51:05.926815Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# transforming in 3D","metadata":{}},{"cell_type":"code","source":"pca = PCA(n_components=0.9)\nx_train_trf = pca.fit_transform(x_train)\nx_test_trf = pca.transform(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:05.929864Z","iopub.execute_input":"2026-01-31T10:51:05.930314Z","iopub.status.idle":"2026-01-31T10:51:06.847662Z","shell.execute_reply.started":"2026-01-31T10:51:05.930282Z","shell.execute_reply":"2026-01-31T10:51:06.846845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_trf = y_train.astype(str)\nfig = px.scatter_3d(df,x=x_train_trf[:,0], y=x_train_trf[:,1], z=x_train_trf[:,2],\n              color=y_train_trf)\nfig.update_layout(\n    margin=dict(l=20, r=20, t=20, b=20),\n    paper_bgcolor=\"LightSteelBlue\",\n)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:06.848536Z","iopub.execute_input":"2026-01-31T10:51:06.848855Z","iopub.status.idle":"2026-01-31T10:51:06.999871Z","shell.execute_reply.started":"2026-01-31T10:51:06.848823Z","shell.execute_reply":"2026-01-31T10:51:06.998766Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Analyzing Variance","metadata":{}},{"cell_type":"code","source":"# Eigen values\npca.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.001473Z","iopub.execute_input":"2026-01-31T10:51:07.001847Z","iopub.status.idle":"2026-01-31T10:51:07.010152Z","shell.execute_reply.started":"2026-01-31T10:51:07.001816Z","shell.execute_reply":"2026-01-31T10:51:07.008499Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Eigen Vector\npca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.011770Z","iopub.execute_input":"2026-01-31T10:51:07.012125Z","iopub.status.idle":"2026-01-31T10:51:07.041798Z","shell.execute_reply.started":"2026-01-31T10:51:07.012092Z","shell.execute_reply":"2026-01-31T10:51:07.040383Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Finding Optinum Num for PCs","metadata":{}},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.043102Z","iopub.execute_input":"2026-01-31T10:51:07.043533Z","iopub.status.idle":"2026-01-31T10:51:07.067837Z","shell.execute_reply.started":"2026-01-31T10:51:07.043500Z","shell.execute_reply":"2026-01-31T10:51:07.066773Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.lineplot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.069012Z","iopub.execute_input":"2026-01-31T10:51:07.069462Z","iopub.status.idle":"2026-01-31T10:51:07.220673Z","shell.execute_reply.started":"2026-01-31T10:51:07.069419Z","shell.execute_reply":"2026-01-31T10:51:07.219603Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Another Dataset for practice","metadata":{}},{"cell_type":"code","source":"from sklearn.datasets import load_breast_cancer","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.222008Z","iopub.execute_input":"2026-01-31T10:51:07.222388Z","iopub.status.idle":"2026-01-31T10:51:07.227595Z","shell.execute_reply.started":"2026-01-31T10:51:07.222347Z","shell.execute_reply":"2026-01-31T10:51:07.226523Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data = load_breast_cancer()\nX = data.data\nY = data.target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.228775Z","iopub.execute_input":"2026-01-31T10:51:07.229192Z","iopub.status.idle":"2026-01-31T10:51:07.256254Z","shell.execute_reply.started":"2026-01-31T10:51:07.229147Z","shell.execute_reply":"2026-01-31T10:51:07.255016Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scaler = StandardScaler()\nX_scaled = scaler.fit_transform(X)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.257850Z","iopub.execute_input":"2026-01-31T10:51:07.258258Z","iopub.status.idle":"2026-01-31T10:51:07.280225Z","shell.execute_reply.started":"2026-01-31T10:51:07.258224Z","shell.execute_reply":"2026-01-31T10:51:07.278999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components=0.95)\nX_pca = pca.fit_transform(X_scaled)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.281670Z","iopub.execute_input":"2026-01-31T10:51:07.282039Z","iopub.status.idle":"2026-01-31T10:51:07.300567Z","shell.execute_reply.started":"2026-01-31T10:51:07.282001Z","shell.execute_reply":"2026-01-31T10:51:07.299446Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_pca.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.304566Z","iopub.execute_input":"2026-01-31T10:51:07.305363Z","iopub.status.idle":"2026-01-31T10:51:07.320994Z","shell.execute_reply.started":"2026-01-31T10:51:07.305314Z","shell.execute_reply":"2026-01-31T10:51:07.319882Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Analyzing Varaince(Another Dataset)","metadata":{}},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.322398Z","iopub.execute_input":"2026-01-31T10:51:07.322824Z","iopub.status.idle":"2026-01-31T10:51:07.346340Z","shell.execute_reply.started":"2026-01-31T10:51:07.322783Z","shell.execute_reply":"2026-01-31T10:51:07.344740Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.348233Z","iopub.execute_input":"2026-01-31T10:51:07.348853Z","iopub.status.idle":"2026-01-31T10:51:07.383783Z","shell.execute_reply.started":"2026-01-31T10:51:07.348812Z","shell.execute_reply":"2026-01-31T10:51:07.380972Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Finding Optinum Num for PCs(Another datset)","metadata":{}},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.386659Z","iopub.execute_input":"2026-01-31T10:51:07.387222Z","iopub.status.idle":"2026-01-31T10:51:07.410080Z","shell.execute_reply.started":"2026-01-31T10:51:07.387181Z","shell.execute_reply":"2026-01-31T10:51:07.408796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.lineplot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-31T10:51:07.411693Z","iopub.execute_input":"2026-01-31T10:51:07.412099Z","iopub.status.idle":"2026-01-31T10:51:07.616813Z","shell.execute_reply.started":"2026-01-31T10:51:07.412065Z","shell.execute_reply":"2026-01-31T10:51:07.615608Z"}},"outputs":[],"execution_count":null}]}