{"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":"2021-11-11T20:01:58.649555Z","iopub.execute_input":"2021-11-11T20:01:58.649844Z","iopub.status.idle":"2021-11-11T20:01:58.65859Z","shell.execute_reply.started":"2021-11-11T20:01:58.649814Z","shell.execute_reply":"2021-11-11T20:01:58.657757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from  tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:01:58.66011Z","iopub.execute_input":"2021-11-11T20:01:58.66039Z","iopub.status.idle":"2021-11-11T20:02:03.539613Z","shell.execute_reply.started":"2021-11-11T20:01:58.660352Z","shell.execute_reply":"2021-11-11T20:02:03.53877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MNIST_file_path = '../input/digit-recognizer/train.csv'\nMNIST_data = pd.read_csv(MNIST_file_path)\nMNIST_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:03.540691Z","iopub.execute_input":"2021-11-11T20:02:03.540904Z","iopub.status.idle":"2021-11-11T20:02:06.428035Z","shell.execute_reply.started":"2021-11-11T20:02:03.540876Z","shell.execute_reply":"2021-11-11T20:02:06.427273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MNIST_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:06.42964Z","iopub.execute_input":"2021-11-11T20:02:06.42983Z","iopub.status.idle":"2021-11-11T20:02:06.451938Z","shell.execute_reply.started":"2021-11-11T20:02:06.429807Z","shell.execute_reply":"2021-11-11T20:02:06.451211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set, valid_set, test_set = MNIST_data[:30000], MNIST_data[30000:40000], MNIST_data[40000:]\n[train_set.shape, valid_set.shape, test_set.shape]","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:06.453108Z","iopub.execute_input":"2021-11-11T20:02:06.453335Z","iopub.status.idle":"2021-11-11T20:02:06.459976Z","shell.execute_reply.started":"2021-11-11T20:02:06.453307Z","shell.execute_reply":"2021-11-11T20:02:06.459136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_set.drop('label', axis=1)\nX_valid = valid_set.drop('label', axis=1)\nX_test = test_set.drop('label', axis=1)\nX_train.columns","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:06.461156Z","iopub.execute_input":"2021-11-11T20:02:06.461483Z","iopub.status.idle":"2021-11-11T20:02:06.583138Z","shell.execute_reply.started":"2021-11-11T20:02:06.461452Z","shell.execute_reply":"2021-11-11T20:02:06.582275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train / 255.\nX_valid = X_valid / 255.\nX_test = X_test / 255.","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:06.584686Z","iopub.execute_input":"2021-11-11T20:02:06.584986Z","iopub.status.idle":"2021-11-11T20:02:06.657778Z","shell.execute_reply.started":"2021-11-11T20:02:06.584948Z","shell.execute_reply":"2021-11-11T20:02:06.657073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_set.label\ny_valid = valid_set.label\ny_test = test_set.label\ny_train.dtype","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:06.658922Z","iopub.execute_input":"2021-11-11T20:02:06.65916Z","iopub.status.idle":"2021-11-11T20:02:06.666712Z","shell.execute_reply.started":"2021-11-11T20:02:06.659132Z","shell.execute_reply":"2021-11-11T20:02:06.666107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib as mpl\nimport matplotlib.pyplot as plt\n\ndef print_image(index):\n    sample_digit = X_train.to_numpy()[index]\n    sample_digit_image = sample_digit.reshape(28,28)\n\n    plt.imshow(sample_digit_image, cmap=\"binary\")\n    plt.axis('off')\n    plt.show()\n\nprint_image(8)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:06.66891Z","iopub.execute_input":"2021-11-11T20:02:06.669679Z","iopub.status.idle":"2021-11-11T20:02:06.787136Z","shell.execute_reply.started":"2021-11-11T20:02:06.669637Z","shell.execute_reply":"2021-11-11T20:02:06.786294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers\n\nmodel = keras.models.Sequential([\n    layers.InputLayer(input_shape=[784]),\n    layers.Dense(100, activation='relu'),\n    layers.Dense(100, activation='relu'),\n    layers.Dense(100, activation='relu'), \n    layers.Dense(100, activation='relu'), \n    layers.Dense(100, activation='relu'), \n    layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:06.789085Z","iopub.execute_input":"2021-11-11T20:02:06.789751Z","iopub.status.idle":"2021-11-11T20:02:07.904895Z","shell.execute_reply.started":"2021-11-11T20:02:06.7897Z","shell.execute_reply":"2021-11-11T20:02:07.904067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:07.905892Z","iopub.execute_input":"2021-11-11T20:02:07.90611Z","iopub.status.idle":"2021-11-11T20:02:07.916103Z","shell.execute_reply.started":"2021-11-11T20:02:07.906084Z","shell.execute_reply":"2021-11-11T20:02:07.915091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.utils.plot_model(model, to_file='model_plot.png', show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:07.917388Z","iopub.execute_input":"2021-11-11T20:02:07.917666Z","iopub.status.idle":"2021-11-11T20:02:08.887322Z","shell.execute_reply.started":"2021-11-11T20:02:07.917627Z","shell.execute_reply":"2021-11-11T20:02:08.886322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='sparse_categorical_crossentropy',\n              optimizer=keras.optimizers.SGD(learning_rate=0.01),\n              metrics=['accuracy']\n             )","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:08.889311Z","iopub.execute_input":"2021-11-11T20:02:08.889732Z","iopub.status.idle":"2021-11-11T20:02:08.905111Z","shell.execute_reply.started":"2021-11-11T20:02:08.889696Z","shell.execute_reply":"2021-11-11T20:02:08.904437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, epochs=60,\n                    validation_data=(X_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:02:08.906362Z","iopub.execute_input":"2021-11-11T20:02:08.907165Z","iopub.status.idle":"2021-11-11T20:06:31.917414Z","shell.execute_reply.started":"2021-11-11T20:02:08.907132Z","shell.execute_reply":"2021-11-11T20:06:31.916499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history.history).plot(figsize=(8,5))\nplt.grid(True)\nplt.gca().set_ylim(0,1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:06:31.919058Z","iopub.execute_input":"2021-11-11T20:06:31.91931Z","iopub.status.idle":"2021-11-11T20:06:32.200153Z","shell.execute_reply.started":"2021-11-11T20:06:31.91928Z","shell.execute_reply":"2021-11-11T20:06:32.198904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:06:32.201542Z","iopub.execute_input":"2021-11-11T20:06:32.201775Z","iopub.status.idle":"2021-11-11T20:06:32.48674Z","shell.execute_reply.started":"2021-11-11T20:06:32.201748Z","shell.execute_reply":"2021-11-11T20:06:32.485966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_new = X_test[:5]\ny_proba = model.predict(X_new)\ny_proba.round(2)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:06:32.488059Z","iopub.execute_input":"2021-11-11T20:06:32.489104Z","iopub.status.idle":"2021-11-11T20:06:32.671082Z","shell.execute_reply.started":"2021-11-11T20:06:32.489056Z","shell.execute_reply":"2021-11-11T20:06:32.669981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_data_filepath='../input/digit-recognizer/test.csv'\nTEST_data = pd.read_csv(TEST_data_filepath)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:06:32.672543Z","iopub.execute_input":"2021-11-11T20:06:32.672789Z","iopub.status.idle":"2021-11-11T20:06:34.095203Z","shell.execute_reply.started":"2021-11-11T20:06:32.672757Z","shell.execute_reply":"2021-11-11T20:06:34.094502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_proba = model.predict(TEST_data)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:06:34.097006Z","iopub.execute_input":"2021-11-11T20:06:34.097664Z","iopub.status.idle":"2021-11-11T20:06:35.98015Z","shell.execute_reply.started":"2021-11-11T20:06:34.097615Z","shell.execute_reply":"2021-11-11T20:06:35.979288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageId = [id for id in range(1,len(TEST_proba)+1)]\n\nLabel = []\nfor image_proba in TEST_proba:\n    highest_value_index = np.argmax(image_proba) # indeks największego prawdopodobieństwa w tablicy\n    Label.append(highest_value_index)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:06:35.981571Z","iopub.execute_input":"2021-11-11T20:06:35.98188Z","iopub.status.idle":"2021-11-11T20:06:36.045157Z","shell.execute_reply.started":"2021-11-11T20:06:35.98184Z","shell.execute_reply":"2021-11-11T20:06:36.044299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageId[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:06:36.046353Z","iopub.execute_input":"2021-11-11T20:06:36.046565Z","iopub.status.idle":"2021-11-11T20:06:36.054093Z","shell.execute_reply.started":"2021-11-11T20:06:36.046539Z","shell.execute_reply":"2021-11-11T20:06:36.053484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Label[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:06:36.055192Z","iopub.execute_input":"2021-11-11T20:06:36.055792Z","iopub.status.idle":"2021-11-11T20:06:36.065368Z","shell.execute_reply.started":"2021-11-11T20:06:36.055755Z","shell.execute_reply":"2021-11-11T20:06:36.06429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_proba[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:06:36.067177Z","iopub.execute_input":"2021-11-11T20:06:36.068278Z","iopub.status.idle":"2021-11-11T20:06:36.078516Z","shell.execute_reply.started":"2021-11-11T20:06:36.068215Z","shell.execute_reply":"2021-11-11T20:06:36.077889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': ImageId,\n                      'Label': Label})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T20:10:20.28536Z","iopub.execute_input":"2021-11-11T20:10:20.28695Z","iopub.status.idle":"2021-11-11T20:10:20.346178Z","shell.execute_reply.started":"2021-11-11T20:10:20.286852Z","shell.execute_reply":"2021-11-11T20:10:20.345561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}