{"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":"markdown","source":"# HACKATHON - Anna Cho, Dan Rossetti, Derik Vo","metadata":{"id":"_C7lRDGr35N4"}},{"cell_type":"code","source":"# from google.colab import drive\n# drive.mount('/content/drive')","metadata":{"id":"MjEQtcRF5zx6","execution":{"iopub.status.busy":"2023-05-12T20:33:25.055613Z","iopub.execute_input":"2023-05-12T20:33:25.055981Z","iopub.status.idle":"2023-05-12T20:33:25.061101Z","shell.execute_reply.started":"2023-05-12T20:33:25.055953Z","shell.execute_reply":"2023-05-12T20:33:25.060153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make sure the path to the zip is correct\n#!unzip -q Kannada-MNIST.zip","metadata":{"id":"fv5MOcbq3hq7","outputId":"0e84c347-41b5-42e5-d692-0dcddec1a653","execution":{"iopub.status.busy":"2023-05-12T20:33:25.067180Z","iopub.execute_input":"2023-05-12T20:33:25.067998Z","iopub.status.idle":"2023-05-12T20:33:25.072209Z","shell.execute_reply.started":"2023-05-12T20:33:25.067964Z","shell.execute_reply":"2023-05-12T20:33:25.071223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Imports!\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport pandas as pd\nfrom tensorflow.keras.regularizers import l2\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn import metrics\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.layers import Dense, Dropout, BatchNormalization, Flatten, Conv2D, MaxPooling2D, Input\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.utils import to_categorical\n\n# For reproducibility\nnp.random.seed(42)","metadata":{"id":"pClTsLfP47j5","execution":{"iopub.status.busy":"2023-05-12T20:34:50.434739Z","iopub.execute_input":"2023-05-12T20:34:50.435084Z","iopub.status.idle":"2023-05-12T20:34:50.442003Z","shell.execute_reply.started":"2023-05-12T20:34:50.435054Z","shell.execute_reply":"2023-05-12T20:34:50.440865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test","metadata":{"id":"sVlUq07q4ld8"}},{"cell_type":"code","source":"kannada_train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\nkannada_train.head()","metadata":{"id":"fTf96vbk4mNO","outputId":"3ebb4f68-ed6e-4aa9-e552-eb260a187c46","execution":{"iopub.status.busy":"2023-05-12T20:33:25.085449Z","iopub.execute_input":"2023-05-12T20:33:25.086280Z","iopub.status.idle":"2023-05-12T20:33:29.327000Z","shell.execute_reply.started":"2023-05-12T20:33:25.086248Z","shell.execute_reply":"2023-05-12T20:33:29.326090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kannada_test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\nkannada_test.head(20)","metadata":{"id":"iA9Yr1lQ5o80","outputId":"79388c30-c930-418a-fdf4-fcc62922b29a","execution":{"iopub.status.busy":"2023-05-12T20:33:29.329066Z","iopub.execute_input":"2023-05-12T20:33:29.330057Z","iopub.status.idle":"2023-05-12T20:33:29.673530Z","shell.execute_reply.started":"2023-05-12T20:33:29.330024Z","shell.execute_reply":"2023-05-12T20:33:29.672475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kannada_train['label'].value_counts()","metadata":{"id":"pDbyGemy5pCy","outputId":"ab215124-a272-445d-d5a9-c68608efe82a","execution":{"iopub.status.busy":"2023-05-12T20:33:29.674846Z","iopub.execute_input":"2023-05-12T20:33:29.675795Z","iopub.status.idle":"2023-05-12T20:33:29.687561Z","shell.execute_reply.started":"2023-05-12T20:33:29.675761Z","shell.execute_reply":"2023-05-12T20:33:29.685688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kannada_test['id'].value_counts()","metadata":{"id":"Vi32eRNa87eb","outputId":"9390bd0c-5ff8-429b-ae9d-458f9521c3fb","execution":{"iopub.status.busy":"2023-05-12T20:33:29.690971Z","iopub.execute_input":"2023-05-12T20:33:29.691688Z","iopub.status.idle":"2023-05-12T20:33:29.700650Z","shell.execute_reply.started":"2023-05-12T20:33:29.691653Z","shell.execute_reply":"2023-05-12T20:33:29.699694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kannada_train.describe().T['max'].max()","metadata":{"id":"2bD-uIOl-VZy","outputId":"3bab8120-b44b-49d9-98b8-23836e08864a","execution":{"iopub.status.busy":"2023-05-12T20:33:29.702289Z","iopub.execute_input":"2023-05-12T20:33:29.702975Z","iopub.status.idle":"2023-05-12T20:33:32.148224Z","shell.execute_reply.started":"2023-05-12T20:33:29.702940Z","shell.execute_reply":"2023-05-12T20:33:32.147208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array(kannada_train.drop(columns = 'label'))\ny = np.array(kannada_train['label'])","metadata":{"id":"4C6aO7Z85pFq","execution":{"iopub.status.busy":"2023-05-12T20:33:32.149793Z","iopub.execute_input":"2023-05-12T20:33:32.150149Z","iopub.status.idle":"2023-05-12T20:33:32.399456Z","shell.execute_reply.started":"2023-05-12T20:33:32.150116Z","shell.execute_reply":"2023-05-12T20:33:32.398502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = to_categorical(y)","metadata":{"id":"J-nVyDu-BAo7","execution":{"iopub.status.busy":"2023-05-12T20:33:32.401029Z","iopub.execute_input":"2023-05-12T20:33:32.401417Z","iopub.status.idle":"2023-05-12T20:33:32.407320Z","shell.execute_reply.started":"2023-05-12T20:33:32.401374Z","shell.execute_reply":"2023-05-12T20:33:32.406329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape X to make it Keras Friendly\nX = X.reshape(X.shape[0], 28, 28, 1)\nprint(X.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-12T20:33:32.408810Z","iopub.execute_input":"2023-05-12T20:33:32.409440Z","iopub.status.idle":"2023-05-12T20:33:32.417460Z","shell.execute_reply.started":"2023-05-12T20:33:32.409393Z","shell.execute_reply":"2023-05-12T20:33:32.416395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 42)","metadata":{"id":"j2PIY5i15pIi","execution":{"iopub.status.busy":"2023-05-12T20:33:32.418899Z","iopub.execute_input":"2023-05-12T20:33:32.419513Z","iopub.status.idle":"2023-05-12T20:33:32.964689Z","shell.execute_reply.started":"2023-05-12T20:33:32.419483Z","shell.execute_reply":"2023-05-12T20:33:32.963567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'X_train shape: {X_train.shape}')\nprint(f'X_test shape: {X_test.shape}')","metadata":{"id":"vXLp2bzZ9ZYa","outputId":"309581f7-63b7-407f-a9eb-d6761234a4b6","execution":{"iopub.status.busy":"2023-05-12T20:33:32.970980Z","iopub.execute_input":"2023-05-12T20:33:32.972068Z","iopub.status.idle":"2023-05-12T20:33:32.978274Z","shell.execute_reply.started":"2023-05-12T20:33:32.972026Z","shell.execute_reply":"2023-05-12T20:33:32.977151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# normalize and convert to float\nX_train = X_train * (1./255)\nX_test = X_test * (1./255)","metadata":{"id":"Yd6Q9_ezAYVT","execution":{"iopub.status.busy":"2023-05-12T20:33:32.979736Z","iopub.execute_input":"2023-05-12T20:33:32.980703Z","iopub.status.idle":"2023-05-12T20:33:33.144091Z","shell.execute_reply.started":"2023-05-12T20:33:32.980666Z","shell.execute_reply":"2023-05-12T20:33:33.143106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Source: Lesson \nmodel_cnn = Sequential()\n\nmodel_cnn.add(Input(shape = (28,28,1)))\n#First Convolution layer\nmodel_cnn.add(Conv2D(64, (3,3), activation = 'relu'))\n#MaxPooling\nmodel_cnn.add(MaxPooling2D(pool_size = (2,2)))\n\n\n# 2nd Convolution layer\nmodel_cnn.add(Conv2D(64, (3,3), activation = 'relu'))\nmodel_cnn.add(MaxPooling2D(pool_size=(2,2)))\n\n\n#To feed this into our model_cnn, we need to flatten\nmodel_cnn.add(Flatten())\n\n\n#Fully connected layer\n# 1 Hidden Layer\nmodel_cnn.add(Dense(64, activation = 'relu'))\n\n#Output Layer\nmodel_cnn.add(Dense(10, activation = 'softmax'))\n\n# Compile it\nmodel_cnn.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['acc']) \n\n# Fit it\nres_cnn = model_cnn.fit(X_train, y_train,\n               epochs = 25,\n               batch_size = 64,\n               validation_data = (X_test, y_test),\n               verbose = 1)","metadata":{"id":"dY7YTSFO9Zbj","outputId":"e6a51bea-0e8c-4b4e-bc75-d5969ee70830","execution":{"iopub.status.busy":"2023-05-12T20:34:56.379130Z","iopub.execute_input":"2023-05-12T20:34:56.379505Z","iopub.status.idle":"2023-05-12T20:36:19.714271Z","shell.execute_reply.started":"2023-05-12T20:34:56.379476Z","shell.execute_reply":"2023-05-12T20:36:19.713283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(res_cnn.history['acc'])\nplt.plot(res_cnn.history['val_acc'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","metadata":{"id":"ih3JgJtw9ZeK","outputId":"c61bf86c-c1bf-4d55-a2d2-981022346c80","execution":{"iopub.status.busy":"2023-05-12T20:36:20.002375Z","iopub.execute_input":"2023-05-12T20:36:20.002723Z","iopub.status.idle":"2023-05-12T20:36:20.265954Z","shell.execute_reply.started":"2023-05-12T20:36:20.002691Z","shell.execute_reply":"2023-05-12T20:36:20.265058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training & validation loss values\nplt.plot(res_cnn.history['loss'])\nplt.plot(res_cnn.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()","metadata":{"id":"cPG0xYNQQEg8","outputId":"09e62645-5b65-437a-ca4e-7708960d4f03","execution":{"iopub.status.busy":"2023-05-12T20:36:20.268202Z","iopub.execute_input":"2023-05-12T20:36:20.268552Z","iopub.status.idle":"2023-05-12T20:36:20.523943Z","shell.execute_reply.started":"2023-05-12T20:36:20.268519Z","shell.execute_reply":"2023-05-12T20:36:20.523057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making Prediction","metadata":{"id":"8x_IAc8vJOzW"}},{"cell_type":"code","source":"kannada_test.set_index('id', inplace=True)","metadata":{"id":"F4fseS8HMYpM","execution":{"iopub.status.busy":"2023-05-12T20:36:27.077434Z","iopub.execute_input":"2023-05-12T20:36:27.077913Z","iopub.status.idle":"2023-05-12T20:36:27.088232Z","shell.execute_reply.started":"2023-05-12T20:36:27.077872Z","shell.execute_reply":"2023-05-12T20:36:27.087051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kannada_test=kannada_test*(1./255)","metadata":{"id":"hkT4Ck_kMnBH","execution":{"iopub.status.busy":"2023-05-12T20:36:29.451455Z","iopub.execute_input":"2023-05-12T20:36:29.451805Z","iopub.status.idle":"2023-05-12T20:36:29.497595Z","shell.execute_reply.started":"2023-05-12T20:36:29.451778Z","shell.execute_reply":"2023-05-12T20:36:29.496194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# got the source from: https://stackoverflow.com/questions/68836551/keras-attributeerror-sequential-object-has-no-attribute-predict-classes\nX_val = np.array(kannada_test)\nprint(X_val.shape)\nX_val = X_val.reshape(X_val.shape[0], 28, 28, 1)\nprint(X_val.shape)\n\npreds_cnn = model_cnn.predict(X_val)\n\nkannada_test['label'] = np.argmax(preds_cnn, axis=-1)","metadata":{"id":"iDccjGEi9Zg1","outputId":"66ad0d07-89ce-4035-a5ea-a582d0fe5092","execution":{"iopub.status.busy":"2023-05-12T20:36:31.721318Z","iopub.execute_input":"2023-05-12T20:36:31.721681Z","iopub.status.idle":"2023-05-12T20:36:32.383796Z","shell.execute_reply.started":"2023-05-12T20:36:31.721653Z","shell.execute_reply":"2023-05-12T20:36:32.382838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kannada_test['label'].head(10)","metadata":{"id":"GXN0xednJpRl","outputId":"97268f1b-72bc-4c15-8eb2-e88c217ffbb0","execution":{"iopub.status.busy":"2023-05-12T20:36:35.702211Z","iopub.execute_input":"2023-05-12T20:36:35.702589Z","iopub.status.idle":"2023-05-12T20:36:35.710037Z","shell.execute_reply.started":"2023-05-12T20:36:35.702558Z","shell.execute_reply":"2023-05-12T20:36:35.708945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = pd.DataFrame(data= kannada_test, columns=['label'], index=kannada_test.index)","metadata":{"id":"LxJbAsI9Jrho","execution":{"iopub.status.busy":"2023-05-12T20:36:39.668669Z","iopub.execute_input":"2023-05-12T20:36:39.669029Z","iopub.status.idle":"2023-05-12T20:36:39.679018Z","shell.execute_reply.started":"2023-05-12T20:36:39.669000Z","shell.execute_reply":"2023-05-12T20:36:39.677898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction.reset_index(inplace=True)","metadata":{"id":"2n9vx6IzP2r6","execution":{"iopub.status.busy":"2023-05-12T20:36:43.278454Z","iopub.execute_input":"2023-05-12T20:36:43.279212Z","iopub.status.idle":"2023-05-12T20:36:43.286442Z","shell.execute_reply.started":"2023-05-12T20:36:43.279145Z","shell.execute_reply":"2023-05-12T20:36:43.285491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction.head(10)","metadata":{"id":"3JHWAFwbNKgu","outputId":"a0f937a3-24a8-431f-ae0b-2f1df14c776e","execution":{"iopub.status.busy":"2023-05-12T20:36:44.766068Z","iopub.execute_input":"2023-05-12T20:36:44.766756Z","iopub.status.idle":"2023-05-12T20:36:44.775558Z","shell.execute_reply.started":"2023-05-12T20:36:44.766722Z","shell.execute_reply":"2023-05-12T20:36:44.774577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction.to_csv('submission.csv', index=False)","metadata":{"id":"bo8y6yoANMM_","execution":{"iopub.status.busy":"2023-05-12T20:36:47.808611Z","iopub.execute_input":"2023-05-12T20:36:47.809102Z","iopub.status.idle":"2023-05-12T20:36:47.832400Z","shell.execute_reply.started":"2023-05-12T20:36:47.809074Z","shell.execute_reply":"2023-05-12T20:36:47.831485Z"},"trusted":true},"execution_count":null,"outputs":[]}]}