{"cells":[{"metadata":{"trusted":true,"_uuid":"4c4e92fdcabf3205923b61c2adcf9161765abca5"},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import Lambda\nimport keras.backend as K\nfrom keras.utils.np_utils import to_categorical\nfrom keras.utils import np_utils\nfrom sklearn.preprocessing import LabelEncoder","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b06119f698dface7adbc86654a5c822b2fe14afc"},"cell_type":"code","source":"train_data = pd.read_csv(\"../input/train.csv\")\ntrain_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40badfc2be2f46ed57615bf24580222bf0741096"},"cell_type":"code","source":"test_data = pd.read_csv(\"../input/test.csv\")\ntest_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2026842e248d92072b917037b7aa1c749741c69b"},"cell_type":"code","source":"for r in range(10):\n    # The first column is the label\n    data = train_data.iloc[[r]]\n    label = data.iloc[0]['label']\n    data.drop(columns=\"label\", inplace=True)\n\n\n\n    # Make those columns into a array of 8-bits pixels\n    # This array will be of 1D with length 784\n    # The pixel intensity values are integers from 0 to 255\n    pixels = np.array(data, dtype='uint8')\n\n    # Reshape the array into 28 x 28 array (2-dimensional array)\n    pixels = pixels.reshape((28, 28))\n\n    # Plot\n    plt.title('Label is {label}'.format(label=label))\n    plt.imshow(pixels, cmap='gray')\n    plt.show()\n\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9c311bb608d316584784f7166696fb58b9ddc1a8"},"cell_type":"code","source":"test_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"93a1b4e77d12327d52a75215345b13a98ce4a1e9"},"cell_type":"code","source":"y = train_data[\"label\"]\ntrain_data.drop(columns='label', inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2fb8e29f13577526ce32f6442997b02dd3d6abaa"},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(train_data, y, test_size=0.2)    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c6db4efe284f5fa5aee852863976934cca2d7ed8"},"cell_type":"code","source":"encoder = LabelEncoder()\nencoder.fit(y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7eb746e05a4fabe27a7c4bc64b0ba1cc73949a12"},"cell_type":"code","source":"y_categorical_train = pd.get_dummies(y_train)\ny_categorical_test = pd.get_dummies(y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83cd4a749cf04224c4cf87031cd3d51423628af3"},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense\n\nmodel = Sequential()\nmodel.add(Dense(250, input_dim=784,activation='relu'))\nmodel.add(Dense(100, activation='relu'))\nmodel.add(Dense(100, activation='relu'))\nmodel.add(Dense(100, activation='relu'))\nmodel.add(Dense(100, activation='relu'))\nmodel.add(Dense(100, activation='relu'))\nmodel.add(Dense(25, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\nmodel.fit(X_train, y_categorical_train, epochs=25, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"2dde564a12097d59bfecc804bba18da6a2ae38ed"},"cell_type":"code","source":"scores = model.evaluate(X_test, y_categorical_test)\nprint(\"\\n%s: %.2f%%\" % (model.metrics_names[1], scores[1]*100))","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"600c68cae50582d6d9ae561d06b3a6b2d58f0f61"},"cell_type":"code","source":"predictions = model.predict_classes(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"795a6ec036db9f42caedb489b2343a8c5dfe5a3d"},"cell_type":"code","source":"prediction_ = np.argmax(to_categorical(predictions), axis = 1)\nprediction_ = encoder.inverse_transform(prediction_)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"13e3f23c07a1a50f43e129c3eb258d22a25f38a5"},"cell_type":"code","source":"len(prediction_)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ddeeb6bed33447a7a8ffb85437e9b940ba0cc63"},"cell_type":"code","source":"submission = pd.read_csv(\"../input/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"168dc5489f541f8fa8bd431cf8f35fd5660ff663"},"cell_type":"code","source":"submission[\"label\"] = prediction_","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"e8fa3de4a3d453574bc906a26494c0ca091a8ec6"},"cell_type":"code","source":"submission.to_csv(\"submit.csv\")","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"trusted":false,"_uuid":"97d78a0b54bb2fd0f70f4d40560821168c25d18b"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}