{"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 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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Arquivo para treinamento do modelo\ndf_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nprint(df_train.shape)\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Arquivo para teste do modelo\ndf_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ndf_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 5 ultimas linhas\ndf_test.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# iloc[<linhas>, <colunas>]\n# todos os dados de linha e coluna, com exceção da primeira coluna\ndf_train.iloc[:,1:].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# armazena todas as colunas referentes à pixel na variavel X_train\nX_train = (df_train.iloc[:,1:].values).astype('float32')\n# armazena a coluna label - target, na variavel X_target\nX_target = df_train['label'] #df_train.iloc[:,0].values.astype('int32')\n\n# armazena todas as colunas de teste na variavel X_test\nX_test = df_test.values.astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# converte o dataframe X_train (num_images, img_rows, img_cols)\n# 28x28 pixels\nX_train = X_train.reshape(X_train.shape[0], 28, 28)\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfor i in range(1, 3):\n    plt.subplot(330 + (i+1))\n    plt.imshow(X_train[i], cmap=plt.get_cmap('gray'))\n    plt.title(X_target[i]);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(3, 6):\n    plt.subplot(330 + (i+1))\n    plt.imshow(X_train[i], cmap=plt.get_cmap('gray'))\n    plt.title(X_target[i]);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(6, 9):\n    plt.subplot(330 + (i+1))\n    plt.imshow(X_train[i], cmap=plt.get_cmap('gray'))\n    plt.title(X_target[i]);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# apresenta estatísticas descritivas do dataframe\ndf_train.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Standardize the data\nfrom sklearn.preprocessing import StandardScaler\nX = df_train.values\nX_std = StandardScaler().fit_transform(X)\n\n# Calculating Eigenvectors and eigenvalues of Cov matirx\nmean_vec = np.mean(X_std, axis=0)\ncov_mat = np.cov(X_std.T)\neig_vals, eig_vecs = np.linalg.eig(cov_mat)\n# Create a list of (eigenvalue, eigenvector) tuples\neig_pairs = [ (np.abs(eig_vals[i]),eig_vecs[:,i]) for i in range(len(eig_vals))]\n\n# Sort the eigenvalue, eigenvector pair from high to low\neig_pairs.sort(key = lambda x: x[0], reverse= True)\n\n# Calculation of Explained Variance from the eigenvalues\ntot = sum(eig_vals)\nvar_exp = [(i/tot)*100 for i in sorted(eig_vals, reverse=True)] # Individual explained variance\ncum_var_exp = np.cumsum(var_exp) # Cumulative explained variance","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot de alguns numeros\nplt.figure(figsize=(14,12))\nfor digit_num in range(0,70):\n    plt.subplot(7,10,digit_num+1)\n    grid_data = train.iloc[digit_num].values.reshape(28,28)  # reshape from 1d to 2d pixel array\n    plt.imshow(grid_data, interpolation = \"none\", cmap = \"afmhot\")\n    plt.xticks([])\n    plt.yticks([])\nplt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.cluster import KMeans\n# define o numero de clusters do KMeans (10 de classe)\nkmeans = KMeans(n_clusters=10)\n# computa os centros de cluster e prever o índice de cluster para cada amostra\nX_clustered = kmeans.fit_predict(X_5d)\n\n# plota pontos por classe \ntrace_Kmeans = go.Scatter(x=X_5d[:, 0], y= X_5d[:, 1], mode=\"markers\",\n                    showlegend=False,\n                    marker=dict(\n                            size=8,\n                            color = X_clustered,\n                            colorscale = 'Portland',\n                            showscale=False, \n                            line = dict(\n            width = 2,\n            color = 'rgb(255, 255, 255)'\n        )\n))\n\nlayout = go.Layout(\n    title= 'KMeans Clustering',\n    hovermode= 'closest',\n    xaxis= dict(\n         title= 'First Principal Component',\n        ticklen= 5,\n        zeroline= False,\n        gridwidth= 2,\n    ),\n    yaxis=dict(\n        title= 'Second Principal Component',\n        ticklen= 5,\n        gridwidth= 2,\n    ),\n    showlegend= True\n)\n\ndata = [trace_Kmeans]\nfig1 = dict(data=data, layout= layout)\n# \npy.iplot(fig1, filename=\"svm\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.utils import np_utils\nfrom keras.layers.core import Dense, Activation, Dropout\n\n# reseta valor das variaveis X_train e X_target pois foram utilizadas a cima\nX_train = (df_train.iloc[:,1:].values).astype('float32')\nX_target = df_train['label']\nX_test = df_test.astype('float32')\n\n# converte o vetor de classe (labels) em uma matriz de classe binária\ny_train = np_utils.to_categorical(X_target)\n\n# pré-processamento\n# divide valor dos dataframes X pela média máxima\nscale = np.max(X_train)\nX_train /= scale\nX_test /= scale\n\n# subtrai valor dos dataframes X pela média\nmean = np.std(X_train)\nX_train -= mean\nX_test -= mean\n\ninput_dim = X_train.shape[1]\nnb_classes = y_train.shape[1]\n\n# cria um modelo Sequential\nmodel = Sequential()\n# define o formato de entrada\nmodel.add(Dense(128, input_dim=input_dim))\n# função de ativação de Unidade linear retificada (Rectified Linear Unit)\nmodel.add(Activation('relu'))\n# aplica o Dropout ao input - eliminamos algumas unidades aleatoriamente (15% de chance de eliminação)\nmodel.add(Dropout(0.15))\nmodel.add(Dense(128))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.15))\nmodel.add(Dense(nb_classes))\n# função de ativação Softmax\nmodel.add(Activation('softmax'))\n\n# configura o modelo para treinamento\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])\n\nprint(\"Treinamento do modelo\")\n# epochs - cutoff, separa o treino em fases distintas\nmodel.fit(X_train, y_train, epochs=10, batch_size=16, validation_split=0.1, verbose=2)\n\nprint(\"Gera predições\")\n_predict = model.predict_classes(X_test, verbose=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# cria dataframe para geração do arquivo de saida\ndf_predict = pd.DataFrame()\ndf_predict[\"ImageId\"] = np.arange(len(_predict))+1\ndf_predict[\"Label\"] = _predict\n\ndf_predict.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Gerar arquivo csv de saida\ndf_predict.to_csv('gender_submission.csv', index=False)","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":1}