{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_dir = \"../input\"\nfname = os.path.join(base_dir,\"train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def preprocessing(csvfile):\n    data= pd.read_csv(csvfile)\n    label = data.iloc[:,0:1]\n    data = data.drop(\"label\",axis=1)\n    values = data.values\n    image_data= np.zeros((len(label),28,28,1))\n    for i,pixel in enumerate(values):\n        image_data[i,:] = np.reshape(pixel,(28,28,1))\n    return image_data, label\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\ndata= pd.read_csv(os.path.join(base_dir,\"train.csv\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label = data.iloc[:,0:1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = data.drop(\"label\",axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"values = data.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimage_data= np.zeros((42000,28,28,1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i,pixel in enumerate(values):\n    image_data[i,:] = np.reshape(pixel,(28,28,1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras import Input,layers\nfrom keras.models import Model\nfrom keras import optimizers\n\ndef model(inputshape):\n    X_input = Input(inputshape)\n    X = layers.ZeroPadding2D((2,2))(X_input)\n    X = layers.Conv2D(64,(5,5),strides=1)(X)\n    X = layers.MaxPooling2D((2,2))(X)\n    X = layers.BatchNormalization()(X)\n    X = layers.Conv2D(32,(3,3),strides=1)(X)\n    X = layers.MaxPooling2D((2,2))(X)\n    X = layers.BatchNormalization()(X)\n    X = layers.Flatten()(X)\n    X = layers.Dense(256,activation =\"relu\")(X)\n    X = layers.Dense(256,activation =\"relu\")(X)\n    X = layers.Dense(10,activation =\"softmax\")(X)\n    model = Model(inputs=X_input,outputs=X)\n    model.compile(optimizers.Adam(),loss=\"sparse_categorical_crossentropy\",metrics=[\"acc\"])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = model(image_data[0].shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test = train_test_split(image_data,label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_data[0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(X_train,y_train,epochs=25,validation_data=[X_test,y_test])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mkdir my_log_dir","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\ncallbacks =[\n    keras.callbacks.TensorBoard(\n    log_dir = \"my_log_dir\",\n    histogram_freq=1,\n    embeddings_freq=1)\n]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def test_preprocessing(csvfile):\n    data= pd.read_csv(csvfile)\n    values = data.values\n    image_data= np.zeros((len(data),28,28,1))\n    for i,pixel in enumerate(values):\n        image_data[i,:] = np.reshape(pixel,(28,28,1))\n    return image_data\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data =test_preprocessing(os.path.join(base_dir,\"test.csv\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = model.predict(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result = np.argmax(y_pred,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result= pd.Series(result,name=\"Label\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),result],axis = 1)\n\nsubmission.to_csv(\"sub.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"a = pd.read_csv(os.path.join(base_dir,\"test.csv\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}