{"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 os\nfrom PIL import Image\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-06T02:51:54.051969Z","iopub.execute_input":"2023-06-06T02:51:54.052638Z","iopub.status.idle":"2023-06-06T02:51:54.057908Z","shell.execute_reply.started":"2023-06-06T02:51:54.052594Z","shell.execute_reply":"2023-06-06T02:51:54.056803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('/kaggle/input/digit-recognizer/train.csv','r')as file:\n    csv_lines = file.readlines()\nx=[]\ny=[]\nfor i in range(1, len(csv_lines)):\n    row=csv_lines[i].replace('\\n','').split(',')\n    x.append(list(map(int, row[1:])))\n    y.append(list(map(int, row[0])))\n\nfrom keras.utils import to_categorical\ny=to_categorical(y,num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T02:51:54.060576Z","iopub.execute_input":"2023-06-06T02:51:54.061050Z","iopub.status.idle":"2023-06-06T02:52:00.450715Z","shell.execute_reply.started":"2023-06-06T02:51:54.061012Z","shell.execute_reply":"2023-06-06T02:52:00.449555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=np.array(x)/255.0\ny=np.array(y)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T02:52:00.452135Z","iopub.execute_input":"2023-06-06T02:52:00.453317Z","iopub.status.idle":"2023-06-06T02:52:05.494034Z","shell.execute_reply.started":"2023-06-06T02:52:00.453275Z","shell.execute_reply":"2023-06-06T02:52:05.492866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense\n\nmodel= Sequential()\nmodel.add(Dense(512, activation='relu',input_shape=(784,)))\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-06T02:52:05.496310Z","iopub.execute_input":"2023-06-06T02:52:05.496680Z","iopub.status.idle":"2023-06-06T02:52:05.567410Z","shell.execute_reply.started":"2023-06-06T02:52:05.496646Z","shell.execute_reply":"2023-06-06T02:52:05.566491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import RMSprop\nmodel.compile(loss='categorical_crossentropy',\n            optimizer=RMSprop(),\n            metrics=['acc'])\nhistory = model.fit(x,y,\n                   batch_size=128,\n                   epochs=20,\n                   validation_split=0.2,\n                   verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T02:56:33.022435Z","iopub.execute_input":"2023-06-06T02:56:33.023368Z","iopub.status.idle":"2023-06-06T02:57:45.773913Z","shell.execute_reply.started":"2023-06-06T02:56:33.023327Z","shell.execute_reply":"2023-06-06T02:57:45.772833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef show_train_history(train_history):\n    plt.plot(train_history.history['acc'])\n    plt.plot(train_history.history['val_acc'])\n    plt.xticks([i for i in range(0, len(train_history.history['acc']))])\n    plt.title('Train History')\n    plt.ylabel('acc')\n    plt.xlabel('epoch')\n    plt.legend(['train','validation'], loc='upper left')\n    plt.show()\n\nshow_train_history(history)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T03:03:40.608347Z","iopub.execute_input":"2023-06-06T03:03:40.608784Z","iopub.status.idle":"2023-06-06T03:03:40.988178Z","shell.execute_reply.started":"2023-06-06T03:03:40.608749Z","shell.execute_reply":"2023-06-06T03:03:40.987093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = 'ImageId,Label\\n'\nwith open('/kaggle/input/digit-recognizer/test.csv','r') as file:\n    csv_lines=file.readlines()\nimage_id= 1\nfor i in range(1, len(csv_lines)):\n    row = csv_lines[i].replace('\\n','').split(',')\n    predict_x = model.predict(np.array([list(map(int,row))])/255.0)\n    result = np.argmax(predict_x, axis=-1)\n    submit += str(image_id)+','+str(result)+'\\n'\n    image_id += 1\nopen('answer.csv','w').write(submit)","metadata":{"execution":{"iopub.status.busy":"2023-06-06T03:09:51.611710Z","iopub.execute_input":"2023-06-06T03:09:51.612130Z","iopub.status.idle":"2023-06-06T03:10:41.827656Z","shell.execute_reply.started":"2023-06-06T03:09:51.612099Z","shell.execute_reply":"2023-06-06T03:10:41.826525Z"},"trusted":true},"execution_count":null,"outputs":[]}]}