{"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":"# By using RandomForestClassifier","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nfrom matplotlib import pyplot as plt\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split\n%matplotlib inline  ","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:43:10.083069Z","iopub.execute_input":"2021-09-03T20:43:10.083822Z","iopub.status.idle":"2021-09-03T20:43:11.442868Z","shell.execute_reply.started":"2021-09-03T20:43:10.083677Z","shell.execute_reply":"2021-09-03T20:43:11.4416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv('../input/Kannada-MNIST/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:43:15.478227Z","iopub.execute_input":"2021-09-03T20:43:15.478639Z","iopub.status.idle":"2021-09-03T20:43:20.998356Z","shell.execute_reply.started":"2021-09-03T20:43:15.478604Z","shell.execute_reply":"2021-09-03T20:43:20.997133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:43:25.91283Z","iopub.execute_input":"2021-09-03T20:43:25.913255Z","iopub.status.idle":"2021-09-03T20:43:25.949236Z","shell.execute_reply.started":"2021-09-03T20:43:25.913192Z","shell.execute_reply":"2021-09-03T20:43:25.948213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    a=data.iloc[i,1:].values\n    a=a.reshape(28,28).astype('uint8')\n    fig = plt.figure(figsize=(4,3))\n    #plt.subplot(1, 3, 1)\n    plt.imshow(a)\n    \n\n    ","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:43:28.48627Z","iopub.execute_input":"2021-09-03T20:43:28.486627Z","iopub.status.idle":"2021-09-03T20:43:29.133984Z","shell.execute_reply.started":"2021-09-03T20:43:28.486596Z","shell.execute_reply":"2021-09-03T20:43:29.132899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_x=data.iloc[:,1:]\ndf_y=data.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:43:31.883919Z","iopub.execute_input":"2021-09-03T20:43:31.88429Z","iopub.status.idle":"2021-09-03T20:43:31.890281Z","shell.execute_reply.started":"2021-09-03T20:43:31.884257Z","shell.execute_reply":"2021-09-03T20:43:31.889012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, test_size=0.2, random_state=4)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:43:35.26761Z","iopub.execute_input":"2021-09-03T20:43:35.267982Z","iopub.status.idle":"2021-09-03T20:43:35.728629Z","shell.execute_reply.started":"2021-09-03T20:43:35.26795Z","shell.execute_reply":"2021-09-03T20:43:35.727337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:43:37.585103Z","iopub.execute_input":"2021-09-03T20:43:37.585511Z","iopub.status.idle":"2021-09-03T20:43:37.604593Z","shell.execute_reply.started":"2021-09-03T20:43:37.585475Z","shell.execute_reply":"2021-09-03T20:43:37.603672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:43:39.985305Z","iopub.execute_input":"2021-09-03T20:43:39.98574Z","iopub.status.idle":"2021-09-03T20:43:39.994316Z","shell.execute_reply.started":"2021-09-03T20:43:39.985702Z","shell.execute_reply":"2021-09-03T20:43:39.993016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf=RandomForestClassifier(n_estimators=100)\nrf.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:43:42.904566Z","iopub.execute_input":"2021-09-03T20:43:42.904986Z","iopub.status.idle":"2021-09-03T20:44:17.456328Z","shell.execute_reply.started":"2021-09-03T20:43:42.904952Z","shell.execute_reply":"2021-09-03T20:44:17.455293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"../input/Kannada-MNIST/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:44:24.875286Z","iopub.execute_input":"2021-09-03T20:44:24.875666Z","iopub.status.idle":"2021-09-03T20:44:25.323463Z","shell.execute_reply.started":"2021-09-03T20:44:24.87563Z","shell.execute_reply":"2021-09-03T20:44:25.322471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = df.iloc[:,1:]\ntest_df","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:44:30.217057Z","iopub.execute_input":"2021-09-03T20:44:30.217583Z","iopub.status.idle":"2021-09-03T20:44:30.24342Z","shell.execute_reply.started":"2021-09-03T20:44:30.217551Z","shell.execute_reply":"2021-09-03T20:44:30.242206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=rf.predict(test_df)\npred.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:44:34.320344Z","iopub.execute_input":"2021-09-03T20:44:34.320693Z","iopub.status.idle":"2021-09-03T20:44:34.526547Z","shell.execute_reply.started":"2021-09-03T20:44:34.320664Z","shell.execute_reply":"2021-09-03T20:44:34.525263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Submission File","metadata":{}},{"cell_type":"code","source":"sample = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nmy_submission = pd.DataFrame()\nmy_submission['id'] = sample['id']\nmy_submission['label'] = pred\n\nmy_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:44:37.886889Z","iopub.execute_input":"2021-09-03T20:44:37.887421Z","iopub.status.idle":"2021-09-03T20:44:37.918165Z","shell.execute_reply.started":"2021-09-03T20:44:37.887389Z","shell.execute_reply":"2021-09-03T20:44:37.917154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(21,10)) \nmy_submission[\"label\"].value_counts().plot(kind=\"pie\",autopct=\"%1.1f%%\")\nplt.axis(\"equal\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:44:49.111571Z","iopub.execute_input":"2021-09-03T20:44:49.111975Z","iopub.status.idle":"2021-09-03T20:44:49.536732Z","shell.execute_reply.started":"2021-09-03T20:44:49.111939Z","shell.execute_reply":"2021-09-03T20:44:49.535566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# By using ANN  ","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nimport tensorflow","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:44:56.24814Z","iopub.execute_input":"2021-09-03T20:44:56.248579Z","iopub.status.idle":"2021-09-03T20:45:02.011581Z","shell.execute_reply.started":"2021-09-03T20:44:56.24854Z","shell.execute_reply":"2021-09-03T20:45:02.010235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:45:02.013471Z","iopub.execute_input":"2021-09-03T20:45:02.013864Z","iopub.status.idle":"2021-09-03T20:45:02.039222Z","shell.execute_reply.started":"2021-09-03T20:45:02.013824Z","shell.execute_reply":"2021-09-03T20:45:02.037933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.iloc[:,1:]\nX.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:45:04.884348Z","iopub.execute_input":"2021-09-03T20:45:04.884742Z","iopub.status.idle":"2021-09-03T20:45:04.904101Z","shell.execute_reply.started":"2021-09-03T20:45:04.884707Z","shell.execute_reply":"2021-09-03T20:45:04.903368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = data.iloc[:,0]\ny.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:45:07.3884Z","iopub.execute_input":"2021-09-03T20:45:07.38883Z","iopub.status.idle":"2021-09-03T20:45:07.396522Z","shell.execute_reply.started":"2021-09-03T20:45:07.388792Z","shell.execute_reply":"2021-09-03T20:45:07.395617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building Model & Compiling Model","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(256,input_dim=784,activation='sigmoid'))\nmodel.add(Dense(132,activation='relu'))\nmodel.add(Dense(64,activation='tanh'))\n\nmodel.add(Dense(10,activation='softmax'))\nmodel.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:45:31.92381Z","iopub.execute_input":"2021-09-03T20:45:31.924226Z","iopub.status.idle":"2021-09-03T20:45:32.05372Z","shell.execute_reply.started":"2021-09-03T20:45:31.924173Z","shell.execute_reply":"2021-09-03T20:45:32.052642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import to_categorical\n\n","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:45:35.21632Z","iopub.execute_input":"2021-09-03T20:45:35.216782Z","iopub.status.idle":"2021-09-03T20:45:35.280857Z","shell.execute_reply.started":"2021-09-03T20:45:35.216741Z","shell.execute_reply":"2021-09-03T20:45:35.279934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = to_categorical(y)\ny","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:45:38.925978Z","iopub.execute_input":"2021-09-03T20:45:38.926612Z","iopub.status.idle":"2021-09-03T20:45:38.93725Z","shell.execute_reply.started":"2021-09-03T20:45:38.926552Z","shell.execute_reply":"2021-09-03T20:45:38.936229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Training","metadata":{}},{"cell_type":"code","source":"history = model.fit(X,y,validation_split=0.2,epochs=30,verbose=2)\nhistory","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:45:43.927969Z","iopub.execute_input":"2021-09-03T20:45:43.928619Z","iopub.status.idle":"2021-09-03T20:47:55.568243Z","shell.execute_reply.started":"2021-09-03T20:45:43.928561Z","shell.execute_reply":"2021-09-03T20:47:55.567028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T20:47:55.570248Z","iopub.execute_input":"2021-09-03T20:47:55.570547Z","iopub.status.idle":"2021-09-03T20:47:55.578607Z","shell.execute_reply.started":"2021-09-03T20:47:55.570519Z","shell.execute_reply":"2021-09-03T20:47:55.577378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.iloc[:,1:]\nX.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-03T21:00:48.2424Z","iopub.execute_input":"2021-09-03T21:00:48.242792Z","iopub.status.idle":"2021-09-03T21:00:48.262847Z","shell.execute_reply.started":"2021-09-03T21:00:48.242759Z","shell.execute_reply":"2021-09-03T21:00:48.261605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = data.iloc[:,0]\ny_test","metadata":{"execution":{"iopub.status.busy":"2021-09-03T21:00:53.283763Z","iopub.execute_input":"2021-09-03T21:00:53.284118Z","iopub.status.idle":"2021-09-03T21:00:53.293012Z","shell.execute_reply.started":"2021-09-03T21:00:53.284089Z","shell.execute_reply":"2021-09-03T21:00:53.291782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=model.predict_classes(X)\npred","metadata":{"execution":{"iopub.status.busy":"2021-09-03T21:01:01.090427Z","iopub.execute_input":"2021-09-03T21:01:01.090787Z","iopub.status.idle":"2021-09-03T21:01:04.245889Z","shell.execute_reply.started":"2021-09-03T21:01:01.090758Z","shell.execute_reply":"2021-09-03T21:01:04.245102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\nprint('Test Data accuracy: ',accuracy_score(y_test, pred)*100)","metadata":{"execution":{"iopub.status.busy":"2021-09-03T21:01:30.821701Z","iopub.execute_input":"2021-09-03T21:01:30.822377Z","iopub.status.idle":"2021-09-03T21:01:30.83369Z","shell.execute_reply.started":"2021-09-03T21:01:30.822327Z","shell.execute_reply":"2021-09-03T21:01:30.832616Z"},"trusted":true},"execution_count":null,"outputs":[]}]}