{"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-16T20:11:16.813124Z","iopub.execute_input":"2021-11-16T20:11:16.813602Z","iopub.status.idle":"2021-11-16T20:11:16.823937Z","shell.execute_reply.started":"2021-11-16T20:11:16.813547Z","shell.execute_reply":"2021-11-16T20:11:16.823140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2021-11-16T20:11:16.825487Z","iopub.execute_input":"2021-11-16T20:11:16.825825Z","iopub.status.idle":"2021-11-16T20:11:16.835482Z","shell.execute_reply.started":"2021-11-16T20:11:16.825798Z","shell.execute_reply":"2021-11-16T20:11:16.834954Z"},"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) # dane przechowujemy jako DataFrame\nMNIST_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-16T20:11:16.836562Z","iopub.execute_input":"2021-11-16T20:11:16.837087Z","iopub.status.idle":"2021-11-16T20:11:19.291010Z","shell.execute_reply.started":"2021-11-16T20:11:16.837045Z","shell.execute_reply":"2021-11-16T20:11:19.290094Z"},"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-16T20:11:19.292686Z","iopub.execute_input":"2021-11-16T20:11:19.293051Z","iopub.status.idle":"2021-11-16T20:11:19.300804Z","shell.execute_reply.started":"2021-11-16T20:11:19.293009Z","shell.execute_reply":"2021-11-16T20:11:19.299968Z"},"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-16T20:11:19.303621Z","iopub.execute_input":"2021-11-16T20:11:19.303969Z","iopub.status.idle":"2021-11-16T20:11:19.418432Z","shell.execute_reply.started":"2021-11-16T20:11:19.303930Z","shell.execute_reply":"2021-11-16T20:11:19.417621Z"},"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-16T20:11:19.419859Z","iopub.execute_input":"2021-11-16T20:11:19.420113Z","iopub.status.idle":"2021-11-16T20:11:19.484588Z","shell.execute_reply.started":"2021-11-16T20:11:19.420086Z","shell.execute_reply":"2021-11-16T20:11:19.483958Z"},"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-16T20:11:19.485431Z","iopub.execute_input":"2021-11-16T20:11:19.485611Z","iopub.status.idle":"2021-11-16T20:11:19.496619Z","shell.execute_reply.started":"2021-11-16T20:11:19.485587Z","shell.execute_reply":"2021-11-16T20:11:19.496096Z"},"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(7)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T20:11:19.497458Z","iopub.execute_input":"2021-11-16T20:11:19.498117Z","iopub.status.idle":"2021-11-16T20:11:19.602937Z","shell.execute_reply.started":"2021-11-16T20:11:19.498079Z","shell.execute_reply":"2021-11-16T20:11:19.601961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers\n\nmodel = keras.models.Sequential([ # model sekwencyjny zawiera kolejno ułożone warstwy neuronów\n    layers.InputLayer(input_shape=[784]), # warstwa wejściowa nie ma funkcji aktywacji\n     # relu jest najpopularniejszą funkcją aktywacji\n    layers.Dense(70, activation='relu'), # w warstwach ukrytych\n    layers.Dense(50, activation='relu'), # 50 neuronów ułożonych w jednym rzędzie\n    layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2021-11-16T20:11:19.604643Z","iopub.execute_input":"2021-11-16T20:11:19.605059Z","iopub.status.idle":"2021-11-16T20:11:19.659818Z","shell.execute_reply.started":"2021-11-16T20:11:19.605007Z","shell.execute_reply":"2021-11-16T20:11:19.658933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-16T20:11:19.660984Z","iopub.execute_input":"2021-11-16T20:11:19.661250Z","iopub.status.idle":"2021-11-16T20:11:19.668338Z","shell.execute_reply.started":"2021-11-16T20:11:19.661206Z","shell.execute_reply":"2021-11-16T20:11:19.667603Z"},"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-16T20:11:19.669687Z","iopub.execute_input":"2021-11-16T20:11:19.670126Z","iopub.status.idle":"2021-11-16T20:11:19.859443Z","shell.execute_reply.started":"2021-11-16T20:11:19.670082Z","shell.execute_reply":"2021-11-16T20:11:19.858557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='sparse_categorical_crossentropy',\n              optimizer=keras.optimizers.SGD(lr=0.1), \n              metrics=['accuracy']\n             )","metadata":{"execution":{"iopub.status.busy":"2021-11-16T20:11:19.861592Z","iopub.execute_input":"2021-11-16T20:11:19.861959Z","iopub.status.idle":"2021-11-16T20:11:19.875423Z","shell.execute_reply.started":"2021-11-16T20:11:19.861889Z","shell.execute_reply":"2021-11-16T20:11:19.874488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, epochs=25,\n                    validation_data=(X_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2021-11-16T20:11:19.877012Z","iopub.execute_input":"2021-11-16T20:11:19.877601Z","iopub.status.idle":"2021-11-16T20:12:19.288734Z","shell.execute_reply.started":"2021-11-16T20:11:19.877537Z","shell.execute_reply":"2021-11-16T20:12:19.287879Z"},"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-16T20:12:19.291746Z","iopub.execute_input":"2021-11-16T20:12:19.292027Z","iopub.status.idle":"2021-11-16T20:12:19.533512Z","shell.execute_reply.started":"2021-11-16T20:12:19.291996Z","shell.execute_reply":"2021-11-16T20:12:19.532958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T20:12:19.534820Z","iopub.execute_input":"2021-11-16T20:12:19.535584Z","iopub.status.idle":"2021-11-16T20:12:19.719313Z","shell.execute_reply.started":"2021-11-16T20:12:19.535539Z","shell.execute_reply":"2021-11-16T20:12:19.718613Z"},"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-16T20:12:19.720487Z","iopub.execute_input":"2021-11-16T20:12:19.720695Z","iopub.status.idle":"2021-11-16T20:12:19.834662Z","shell.execute_reply.started":"2021-11-16T20:12:19.720670Z","shell.execute_reply":"2021-11-16T20:12:19.833879Z"},"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-16T20:12:19.836107Z","iopub.execute_input":"2021-11-16T20:12:19.836419Z","iopub.status.idle":"2021-11-16T20:12:21.338067Z","shell.execute_reply.started":"2021-11-16T20:12:19.836379Z","shell.execute_reply":"2021-11-16T20:12:21.337313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_proba = model.predict(TEST_data)","metadata":{"execution":{"iopub.status.busy":"2021-11-16T20:12:21.339172Z","iopub.execute_input":"2021-11-16T20:12:21.339408Z","iopub.status.idle":"2021-11-16T20:12:22.986331Z","shell.execute_reply.started":"2021-11-16T20:12:21.339365Z","shell.execute_reply":"2021-11-16T20:12:22.985682Z"},"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-16T20:12:22.987307Z","iopub.execute_input":"2021-11-16T20:12:22.988006Z","iopub.status.idle":"2021-11-16T20:12:23.083786Z","shell.execute_reply.started":"2021-11-16T20:12:22.987976Z","shell.execute_reply":"2021-11-16T20:12:23.083100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageId[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-16T20:12:23.084872Z","iopub.execute_input":"2021-11-16T20:12:23.085131Z","iopub.status.idle":"2021-11-16T20:12:23.089851Z","shell.execute_reply.started":"2021-11-16T20:12:23.085104Z","shell.execute_reply":"2021-11-16T20:12:23.089299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Label[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-16T20:12:23.090631Z","iopub.execute_input":"2021-11-16T20:12:23.091333Z","iopub.status.idle":"2021-11-16T20:12:23.103467Z","shell.execute_reply.started":"2021-11-16T20:12:23.091300Z","shell.execute_reply":"2021-11-16T20:12:23.102729Z"},"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-16T20:12:23.106466Z","iopub.execute_input":"2021-11-16T20:12:23.107141Z","iopub.status.idle":"2021-11-16T20:12:23.193644Z","shell.execute_reply.started":"2021-11-16T20:12:23.107098Z","shell.execute_reply":"2021-11-16T20:12:23.192943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}