{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport matplotlib.pyplot as plt\nprint(os.listdir(\"../input\"))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-08T06:36:41.568666Z","iopub.execute_input":"2022-06-08T06:36:41.569257Z","iopub.status.idle":"2022-06-08T06:36:41.575601Z","shell.execute_reply.started":"2022-06-08T06:36:41.569194Z","shell.execute_reply":"2022-06-08T06:36:41.574919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-08T06:36:57.813675Z","iopub.execute_input":"2022-06-08T06:36:57.814244Z","iopub.status.idle":"2022-06-08T06:37:02.232072Z","shell.execute_reply.started":"2022-06-08T06:36:57.814183Z","shell.execute_reply":"2022-06-08T06:37:02.231232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = df.values\nrows = 28\ncolumns = 28\nX = data[:,1:]\nY = data[:,0]\n\nX = X.reshape(X.shape[0],rows,columns,1)\nX.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-08T06:48:00.265341Z","iopub.execute_input":"2022-06-08T06:48:00.265660Z","iopub.status.idle":"2022-06-08T06:48:00.283786Z","shell.execute_reply.started":"2022-06-08T06:48:00.265593Z","shell.execute_reply":"2022-06-08T06:48:00.282996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X[9].reshape(28,28))\nY[9]","metadata":{"execution":{"iopub.status.busy":"2022-06-08T06:48:01.767675Z","iopub.execute_input":"2022-06-08T06:48:01.768069Z","iopub.status.idle":"2022-06-08T06:48:01.997509Z","shell.execute_reply.started":"2022-06-08T06:48:01.767984Z","shell.execute_reply":"2022-06-08T06:48:01.996512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Flatten,Dense,Dropout\nfrom keras.layers.convolutional import MaxPooling2D,Conv2D\nfrom keras.models import Sequential\nfrom keras.utils import np_utils\nfrom sklearn.model_selection import train_test_split","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.status.busy":"2022-06-08T06:48:03.357628Z","iopub.execute_input":"2022-06-08T06:48:03.358045Z","iopub.status.idle":"2022-06-08T06:48:03.363672Z","shell.execute_reply.started":"2022-06-08T06:48:03.357972Z","shell.execute_reply":"2022-06-08T06:48:03.362714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 3\noutputs = 10\ncolumns = 28\nrows = 28","metadata":{"_uuid":"69db12a4dd820069a5e300f04cd8e326e585655c","execution":{"iopub.status.busy":"2022-06-08T06:48:04.774319Z","iopub.execute_input":"2022-06-08T06:48:04.774821Z","iopub.status.idle":"2022-06-08T06:48:04.778690Z","shell.execute_reply.started":"2022-06-08T06:48:04.774756Z","shell.execute_reply":"2022-06-08T06:48:04.777758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X/255\nY = np_utils.to_categorical(Y)","metadata":{"_uuid":"ca2c39078de7e40415fcd3163f8af5104a5d130e","execution":{"iopub.status.busy":"2022-06-08T06:48:05.983629Z","iopub.execute_input":"2022-06-08T06:48:05.984292Z","iopub.status.idle":"2022-06-08T06:48:06.243634Z","shell.execute_reply.started":"2022-06-08T06:48:05.984224Z","shell.execute_reply":"2022-06-08T06:48:06.242723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xtrain,Xtest,Ytrain,Ytest = train_test_split(X,Y,test_size=.3)","metadata":{"_uuid":"a660f35ae03843d2f4e5cca003dc29272a905ccc","execution":{"iopub.status.busy":"2022-06-08T06:48:07.859862Z","iopub.execute_input":"2022-06-08T06:48:07.860456Z","iopub.status.idle":"2022-06-08T06:48:08.737525Z","shell.execute_reply.started":"2022-06-08T06:48:07.860385Z","shell.execute_reply":"2022-06-08T06:48:08.736870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32,kernel_size=(3,3),input_shape=(28,28,1),activation='relu'))\nmodel.add(Conv2D(32,kernel_size=(3,3),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(.2))\nmodel.add(Flatten())\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dropout(.25))\nmodel.add(Dense(outputs,activation='softmax'))\n\nmodel.compile(optimizer= 'adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"_uuid":"5b1b46a37c6b7e80a46b94ac612cf55020ff43b5","execution":{"iopub.status.busy":"2022-06-08T06:48:10.375186Z","iopub.execute_input":"2022-06-08T06:48:10.375878Z","iopub.status.idle":"2022-06-08T06:48:10.529483Z","shell.execute_reply.started":"2022-06-08T06:48:10.375811Z","shell.execute_reply":"2022-06-08T06:48:10.528363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(Xtrain,Ytrain,epochs = epochs)","metadata":{"_uuid":"7a72bb111805b6e1abd984035e0e5baaa87f8329","execution":{"iopub.status.busy":"2022-06-08T06:48:14.788745Z","iopub.execute_input":"2022-06-08T06:48:14.789306Z","iopub.status.idle":"2022-06-08T06:50:37.963698Z","shell.execute_reply.started":"2022-06-08T06:48:14.788977Z","shell.execute_reply":"2022-06-08T06:50:37.962972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(Xtest,Ytest)\nprint('accuracy : ',score[1])","metadata":{"_uuid":"1886a48807468eedb86f7f366b40e1bed977fbe3","execution":{"iopub.status.busy":"2022-06-08T05:34:02.013181Z","iopub.execute_input":"2022-06-08T05:34:02.013827Z","iopub.status.idle":"2022-06-08T05:34:07.257190Z","shell.execute_reply.started":"2022-06-08T05:34:02.013763Z","shell.execute_reply":"2022-06-08T05:34:07.256203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_cnn(n):\n    pred = model.predict_classes(np.expand_dims(Xtest[n],axis=0))\n    plt.imshow(Xtest[n].reshape(28,28))\n    plt.title(\"prediction : \"+str(pred[0]))\n","metadata":{"_uuid":"8a64c5798a38ecdf58517c92295f46b373c5fb79","execution":{"iopub.status.busy":"2022-06-08T06:58:30.516566Z","iopub.execute_input":"2022-06-08T06:58:30.516880Z","iopub.status.idle":"2022-06-08T06:58:30.521778Z","shell.execute_reply.started":"2022-06-08T06:58:30.516842Z","shell.execute_reply":"2022-06-08T06:58:30.520749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_cnn(74)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T06:58:38.419782Z","iopub.execute_input":"2022-06-08T06:58:38.420312Z","iopub.status.idle":"2022-06-08T06:58:38.632321Z","shell.execute_reply.started":"2022-06-08T06:58:38.420241Z","shell.execute_reply":"2022-06-08T06:58:38.631391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense\nnn_model = Sequential()\nnn_model.add(Dense(12, activation='relu',input_dim=784))\nnn_model.add(Dense(8, activation='relu'))\nnn_model.add(Dense(10, activation='sigmoid'))\n\nnn_model.compile(optimizer= 'adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-06-08T06:42:50.891083Z","iopub.execute_input":"2022-06-08T06:42:50.891548Z","iopub.status.idle":"2022-06-08T06:42:50.970011Z","shell.execute_reply.started":"2022-06-08T06:42:50.891506Z","shell.execute_reply":"2022-06-08T06:42:50.969349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = df.values\nX_nn = data[:,1:]\nY_nn = data[:,0]\n\nX_nn = X_nn/255\nY_nn = np_utils.to_categorical(Y_nn)\n\nXtrain_nn,Xtest_nn,Ytrain_nn,Ytest_nn = train_test_split(X_nn,Y_nn,test_size=.2)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T06:59:37.926938Z","iopub.execute_input":"2022-06-08T06:59:37.927575Z","iopub.status.idle":"2022-06-08T06:59:39.543104Z","shell.execute_reply.started":"2022-06-08T06:59:37.927525Z","shell.execute_reply":"2022-06-08T06:59:39.542098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn_model.fit(Xtrain_nn,Ytrain_nn,epochs=20,validation_data=(Xtest_nn,Ytest_nn))","metadata":{"execution":{"iopub.status.busy":"2022-06-08T06:46:51.564162Z","iopub.execute_input":"2022-06-08T06:46:51.564911Z","iopub.status.idle":"2022-06-08T06:47:19.489896Z","shell.execute_reply.started":"2022-06-08T06:46:51.564839Z","shell.execute_reply":"2022-06-08T06:47:19.488972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_nn(model, n):\n    pred = model.predict_classes(np.expand_dims(Xtest_nn[n],axis=0))\n    plt.imshow(Xtest_nn[n].reshape(28,28))\n    plt.title(\"prediction : \"+str(pred[0]))\n\npredict_nn(nn_model, 6)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:01:28.334467Z","iopub.execute_input":"2022-06-08T07:01:28.335120Z","iopub.status.idle":"2022-06-08T07:01:28.544516Z","shell.execute_reply.started":"2022-06-08T07:01:28.335065Z","shell.execute_reply":"2022-06-08T07:01:28.543581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}