{"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":"2023-08-25T04:12:02.986902Z","iopub.status.idle":"2023-08-25T04:12:02.988254Z","shell.execute_reply.started":"2023-08-25T04:12:02.987986Z","shell.execute_reply":"2023-08-25T04:12:02.988015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing Dependencies","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom numpy import linalg as LA\n\n\nfrom torch.utils.data import Dataset\nimport os\nfrom torchvision.io import read_image\nimport torch\nfrom torch.utils.data import DataLoader\n\n# For plotting\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.decomposition import PCA\nimport sklearn.decomposition","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:29:09.680888Z","iopub.execute_input":"2023-08-25T04:29:09.681277Z","iopub.status.idle":"2023-08-25T04:29:09.692048Z","shell.execute_reply.started":"2023-08-25T04:29:09.681247Z","shell.execute_reply":"2023-08-25T04:29:09.690680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('../input/Kannada-MNIST/test.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:19:12.832770Z","iopub.execute_input":"2023-08-25T04:19:12.833172Z","iopub.status.idle":"2023-08-25T04:19:17.627893Z","shell.execute_reply.started":"2023-08-25T04:19:12.833141Z","shell.execute_reply":"2023-08-25T04:19:17.626589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, val_df = train_test_split(train, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:20:12.290068Z","iopub.execute_input":"2023-08-25T04:20:12.290896Z","iopub.status.idle":"2023-08-25T04:20:12.633401Z","shell.execute_reply.started":"2023-08-25T04:20:12.290859Z","shell.execute_reply":"2023-08-25T04:20:12.632164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class KannadaDataset(Dataset):\n    def __init__(self, image_df, transform=None, target_transform=None):\n        self.img_labels = image_df.iloc[:,0]\n        self.img_df = image_df.iloc[:,1:]\n        self.transform = transform\n        self.target_transform = target_transform\n\n    def __len__(self):\n        return len(self.img_labels)\n\n    def __getitem__(self, idx):\n        image = torch.tensor(self.img_df.iloc[idx].values)\n        label = self.img_labels.iloc[idx]\n        if self.transform:\n            image = self.transform(image)\n        if self.target_transform:\n            label = self.target_transform(label)\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:20:19.334042Z","iopub.execute_input":"2023-08-25T04:20:19.334587Z","iopub.status.idle":"2023-08-25T04:20:19.346326Z","shell.execute_reply.started":"2023-08-25T04:20:19.334541Z","shell.execute_reply":"2023-08-25T04:20:19.344914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = KannadaDataset(train_df)\ntest_dataset = KannadaDataset(test)\nval_dataset = KannadaDataset(val_df)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:22:07.943167Z","iopub.execute_input":"2023-08-25T04:22:07.943643Z","iopub.status.idle":"2023-08-25T04:22:07.950326Z","shell.execute_reply.started":"2023-08-25T04:22:07.943606Z","shell.execute_reply":"2023-08-25T04:22:07.948954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(6):\n    label = val_df.iloc[:,0].values[i]\n    image = torch.tensor(val_df.drop(val_df.iloc[:,0].name, axis=1).iloc[i,:].values).reshape(28,28)\n    plt.subplot(2,3,i+1)\n    plt.imshow(image, cmap='gray')\n    plt.text(14, -2, f\"Label: {label}\", ha='center', fontsize=20, color='blue')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:22:53.793603Z","iopub.execute_input":"2023-08-25T04:22:53.794030Z","iopub.status.idle":"2023-08-25T04:22:54.670965Z","shell.execute_reply.started":"2023-08-25T04:22:53.793997Z","shell.execute_reply":"2023-08-25T04:22:54.669760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# some parameters\ninput_size = 784 # 28x28\nhidden_size = 500\nnum_classes = 10\nnum_epochs = 50\nbatch_size = 64\nlearning_rate = 1e-3","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:23:28.954620Z","iopub.execute_input":"2023-08-25T04:23:28.955095Z","iopub.status.idle":"2023-08-25T04:23:28.961351Z","shell.execute_reply.started":"2023-08-25T04:23:28.955059Z","shell.execute_reply":"2023-08-25T04:23:28.960158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True)\nval_loader = DataLoader(dataset=val_dataset,batch_size=batch_size,shuffle=True)\ntest_loader = DataLoader(dataset=test_dataset, batch_size=batch_size,shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:23:56.376788Z","iopub.execute_input":"2023-08-25T04:23:56.377218Z","iopub.status.idle":"2023-08-25T04:23:56.383671Z","shell.execute_reply.started":"2023-08-25T04:23:56.377187Z","shell.execute_reply":"2023-08-25T04:23:56.382486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training = train_df.iloc[:,1:]\ntesting = test.iloc[:,1:]\ntraining.values.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:24:20.314396Z","iopub.execute_input":"2023-08-25T04:24:20.314807Z","iopub.status.idle":"2023-08-25T04:24:20.323607Z","shell.execute_reply.started":"2023-08-25T04:24:20.314773Z","shell.execute_reply":"2023-08-25T04:24:20.321996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\n\ntraining_df=training.copy()\ntraining_df=pd.DataFrame(scaler.fit_transform(training_df), columns=training_df.columns)\ntraining_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:24:54.243258Z","iopub.execute_input":"2023-08-25T04:24:54.243686Z","iopub.status.idle":"2023-08-25T04:24:55.238187Z","shell.execute_reply.started":"2023-08-25T04:24:54.243652Z","shell.execute_reply":"2023-08-25T04:24:55.236660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=6, svd_solver = 'auto')\nPrincipal_components=pca.fit_transform(training_df)\ndf= pd.DataFrame(data = Principal_components, columns = ['PC 1', 'PC 2', 'PC 3', 'PC 4', 'PC 5', 'PC 6'])\nprint(df)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:27:33.816242Z","iopub.execute_input":"2023-08-25T04:27:33.816700Z","iopub.status.idle":"2023-08-25T04:27:37.104719Z","shell.execute_reply.started":"2023-08-25T04:27:33.816663Z","shell.execute_reply":"2023-08-25T04:27:37.103341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PC_values = np.arange(pca.n_components_) + 1\nplt.plot(PC_values, pca.explained_variance_ratio_, 'ro-', linewidth=2)\nplt.title('Scree Plot')\nplt.xlabel('Principal Component')\nplt.ylabel('Proportion of Variance Explained')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:28:07.119960Z","iopub.execute_input":"2023-08-25T04:28:07.120410Z","iopub.status.idle":"2023-08-25T04:28:07.370254Z","shell.execute_reply.started":"2023-08-25T04:28:07.120379Z","shell.execute_reply":"2023-08-25T04:28:07.368794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = sklearn.decomposition.PCA()\npca.fit(training.values)\nmu = np.mean(training.values, axis=0)\n\nnComp = 4\nXhat = np.dot(pca.transform(training.values)[:,:nComp], pca.components_[:nComp,:])\nXhat += mu","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:29:13.801283Z","iopub.execute_input":"2023-08-25T04:29:13.801710Z","iopub.status.idle":"2023-08-25T04:29:22.055461Z","shell.execute_reply.started":"2023-08-25T04:29:13.801674Z","shell.execute_reply":"2023-08-25T04:29:22.053747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(training.iloc[3].values.reshape(28,28), cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:30:03.398703Z","iopub.execute_input":"2023-08-25T04:30:03.399107Z","iopub.status.idle":"2023-08-25T04:30:03.709169Z","shell.execute_reply.started":"2023-08-25T04:30:03.399075Z","shell.execute_reply":"2023-08-25T04:30:03.708072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(Xhat[3].reshape(28,28), cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:30:18.910798Z","iopub.execute_input":"2023-08-25T04:30:18.911241Z","iopub.status.idle":"2023-08-25T04:30:19.208265Z","shell.execute_reply.started":"2023-08-25T04:30:18.911205Z","shell.execute_reply":"2023-08-25T04:30:19.206887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(6):\n    label = train_df.iloc[:,0].values[i]\n    image_actual = training.iloc[i].values.reshape(28,28)\n    image_predicted = Xhat[i].reshape(28,28)\n    plt.subplot(3,3,2)\n    plt.imshow(image_actual, cmap='gray')\n    plt.imshow(image_predicted, cmap='gray')\n    plt.text(14, -2, f\"Label: {label}\", ha='center', fontsize=10, color='blue')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-25T04:31:44.390453Z","iopub.execute_input":"2023-08-25T04:31:44.390904Z","iopub.status.idle":"2023-08-25T04:31:44.672563Z","shell.execute_reply.started":"2023-08-25T04:31:44.390871Z","shell.execute_reply":"2023-08-25T04:31:44.671123Z"},"trusted":true},"execution_count":null,"outputs":[]}]}