{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Try using Keras","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"### Imports","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from keras.utils import to_categorical\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report,accuracy_score\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.regularizers import l2","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Read the dataframe","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/fashionmnist/fashion-mnist_train.csv')\ntest= pd.read_csv('../input/fashionmnist/fashion-mnist_test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Define the target and the features\nx_train = train.drop(columns=['label'])\ny_train = train.label\n\nx_test = test.drop(columns=['label'])\ny_test = test.label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Print the shape of target\ny_train.shape , y_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Print the number of class in the train target\nlist(y_train.unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Print the number of class in the test target\nlist(y_test.unique())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Scale Pixel image\nFirst off all, let's identify that the max number in pixel image\n\nSo, after that I'll divide the target by this number to scale the target in this model","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train.max().sort_values().tail(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test.max().sort_values().tail(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#The max number is 255, so lets divide the target by this number\nx_train = x_train/255\nx_test = x_test/255","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Transform features in float\nAfter scaling the target let's transform the feature in float in order to reduce the risk of It converting in integer number ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = x_train.astype(float) \nx_test = x_test.astype(float)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Finally, lets get dummies of the target using the function presents in keras\nfrom keras.utils import to_categorical\n\ny_train = to_categorical(y_train,10) #10 levels of image\ny_test =  to_categorical(y_test,10) #10 levels of image","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Applying Deep learning model to predict the image","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"#Starting a neural network\nmodelo = Sequential()\n\n#Input the first layer in model with 50 neurals and the activation function will be Relu\nmodelo.add(Dense(50 #number os neurals\n                ,activation = 'relu' #activation function\n                ,input_shape = (784,) #Number of features in dataframe, let's pay attention, because keras need to receive a tuple, its the reason of (784,0)\n                ,kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)\n                ))\n\n#Input the second layer in model with 50 neurals and the same activation function\nmodelo.add(Dense(30 \n                ,activation = 'relu' \n                ,kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01) \n                ))\n\n#Input the third layer in model with 20 neurals and the same activation function\nmodelo.add(Dense(20\n                ,activation = 'relu' \n                ,kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01) \n                ))\n\n#The final layes will be with 10 neurals because we have 10 class in this dataframe\n#The activation function is softmax because its will be normalize the output of neurals and it will be easier identify the probably of each class\nmodelo.add(Dense(10 #numero de classs\n                ,activation = 'softmax' #Vai normaliza as probabilidades por exponencial\n                ))\n\n#Finally, lets see the summary of the model and see how many parameters its have.\nmodelo.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#I use the croos entropy, because its penalty high error for bad clasification and a loss error for good erro, its a commum metric using in classification problems\nmodelo.compile(\n                loss='categorical_crossentropy' \n               ,optimizer='adam' \n               ,metrics=['accuracy'] )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = modelo.fit(x_train,y_train\n         ,epochs=30 #number of times that model will through in train \n         ,batch_size = 128 #number of rows that will be consider to update the weights of layers\n         ,verbose = 1\n         ,validation_data=(x_test,y_test)\n         )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Print loss and accuracy\nmodelo.evaluate(x_test,y_test,verbose = 0)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### PLotting the accuracy and loss during the increment of epochs","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.subplots(figsize=(13, 8))\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.subplots(figsize=(13, 8))\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Classification report of each class","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"#Predict the x_test\np = modelo.predict(x_test)\np = (p > 0.5)\nprint('ACC: %.3f%%' % (accuracy_score(y_test, p)*100))\nprint('---------')\nprint(classification_report(y_test, p))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Try to do the same model in Scikit-learn","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.neural_network import MLPClassifier\nmodel = MLPClassifier(hidden_layer_sizes=(50,30,20) #define 3 layers with 50, 30 and 20 neurals\n                      , batch_size=32 #Define the same bacth_size\n                      , solver = 'adam' #Define the optimization\n                      ,activation='relu' #Activation function\n                      , max_iter=30 #Number of epochs\n                      ,verbose=1\n                      , random_state=42)\n\nmodel.fit(x_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Print the accuracy\nprint('Accuracy:', model.score(x_test, y_test))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### PLotting the loss during the increment of epochs","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams['figure.figsize'] = 10, 10\n\nplt.plot(list(range(len(model.loss_curve_))), model.loss_curve_)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Predict the x_test\np = model.predict(x_test)\np = (p > 0.5)\nprint('ACC: %.3f%%' % (accuracy_score(y_test, p)*100))\nprint('---------')\nprint(classification_report(y_test, p))","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}