{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Activation, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.utils import np_utils\nfrom keras.datasets import mnist","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ath = '../input/aiforcegen3-challenge1/'\n\ntrain_df = pd.read_csv(path + 'train.csv')\ntest_df = pd.read_csv(path + 'test.csv')\ntrain_df['label'].value_counts().plot(kind='bar')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train=np.asarray(train_df['label'])\nX_train=np.asarray(train_df.drop('label',axis=1))\nX_test=np.asarray(test_df)\n\nprint(X_train.shape,Y_train.shape,X_test.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\nX_test = X_test.reshape(X_test.shape[0], 28, 28, 1)\nY_train = np_utils.to_categorical(Y_train, 10)\n\nprint(Y_train.shape, X_test.shape, X_train.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 5. Định nghĩa model\nmodel = Sequential()\n \n# Thêm Convolutional layer với 32 kernel, kích thước kernel 3*3\n# dùng hàm sigmoid làm activation và chỉ rõ input_shape cho layer đầu tiên\nmodel.add(Conv2D(32, (3, 3), activation='sigmoid', input_shape=(28,28,1)))\n\n# Thêm Convolutional layer\nmodel.add(Conv2D(32, (3, 3), activation='sigmoid'))\n\n# Thêm Max pooling layer\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\n# Flatten layer chuyển từ tensor sang vector\nmodel.add(Flatten())\n\n# Thêm Fully Connected layer với 128 nodes và dùng hàm sigmoid\nmodel.add(Dense(128, activation='sigmoid'))\n\n# Output layer với 10 node và dùng softmax function để chuyển sang xác xuất.\nmodel.add(Dense(10, activation='softmax'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 7. Thực hiện train model với data\nX_val=X_train\nY_val=Y_train\nH = model.fit(X_train, Y_train, validation_data=(X_val, Y_val),\n          batch_size=32, epochs=10, verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict = model.predict(X_test)\n\ny=[]\nfor a in y_predict:\n    y.append(a.argmax())\ny=np.array(y)\nprint(y.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgid=np.array(range(0,X_test.shape[0])).astype(int)\nlabel=pd.Series(imgid,name='ImageId').astype(int)\nprd=pd.Series(y,name='Label').astype(int)\nresults = pd.concat([label,prd],axis=1)\nresults.to_csv('output.csv',index=False)\nresults['Label'].value_counts().plot(kind='bar')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_test[100].reshape(28,28), cmap='gray')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}