Kaggle competition: IESB Norte - IGM - Maio 2019
criterion = nn.CrossEntropyLoss()<choose_model_class>
dftreino = pd.read_csv('.. /input/train.csv')
 dfteste = pd.read_csv('.. /input/test.csv')
 dftotal = dfteste.append(dftreino )
optimizer = torch.optim.SGD(model.parameters() , lr=0.01 )<define_variables>
dftotal['populacao']=dftotal['populacao'].str.replace('(','' ).str.replace(')',
'' ).str.replace(',','' ).str.replace('.','' ).astype(dtype=np.int )
batch_size = 64
n_epochs = 500
batch_no = len(X_train)// batch_size
train_loss = 0
train_loss_min = np.Inf
for epoch in range(n_epochs):
for i in range(batch_no):
start = i*batch_size
end = start+batch_size
x_var = Variable(torch.FloatTensor(X_train[start:end]))
y_var = Variable(torch.LongTensor(y_train[start:end]))
optimizer.zero_grad()
output = model(x_var)
 loss = criterion(output,y_var)
 loss.backward()
optimizer.step()
values, labels = torch.max(output, 1)
 num_right = np.sum(labels.data.numpy() == y_train[start:end])
 train_loss += loss.item() *batch_size
train_loss = train_loss / len(X_train)
 if train_loss <= train_loss_min:
print("Validation loss decreased({:6f} ===> {:6f} ).Saving the model...".format(train_loss_min,train_loss))
torch.save(model.state_dict() , "model.pt")
 train_loss_min = train_loss
if epoch % 200 == 0:
print('')
 print("Epoch: {} \tTrain Loss: {} \tTrain Accuracy: {}".format(epoch+1, train_loss,num_right / len(y_train[start:end])))
 print('Training Ended! ' )<prepare_x_and_y>
dftotal['area']=dftotal['area'].str.replace(',','' ).astype(float)
X_test = df_test.iloc[:,1:].values
X_test_var = Variable(torch.FloatTensor(X_test), requires_grad=False)
 with torch.no_grad() :
test_result = model(X_test_var)
 values, labels = torch.max(test_result, 1)
 survived = labels.data.numpy()<save_to_csv>
dftotal['densidade_dem']=dftotal['densidade_dem'].str.replace(',','' ).astype(float)
 dftotal['cat_porte'] = dftotal['porte'].astype('category' ).cat.codes
dftotal['cat_regiao'] = dftotal['regiao'].astype('category' ).cat.codes
dftotal['cat_estado'] = dftotal['estado'].astype('category' ).cat.codes
submission = pd.DataFrame({'PassengerId': df_sub['PassengerId'], 'Survived': survived})
 submission.to_csv('submission.csv', index=False )<import_modules>
dftotal['gasto_pc_educacao'].fillna(dftotal['gasto_pc_educacao'].mean() , inplace=True)
 dftotal['perc_pop_econ_ativa'].fillna(dftotal['perc_pop_econ_ativa'].mean() , inplace=True)
import numpy as np
import pandas as pd
import os
import sys
from tqdm import tqdm
from pathlib import Path
import tensorflow as tf
import skimage.io
import matplotlib.pyplot as plt<install_modules>
feats = [c for c in dftotal.columns if c not in ['codigo_mun', 'comissionados_por_servidor','nota_mat', 'densidade_dem',
'participacao_transf_receita', 'servidores',
'gasto_pc_saude', 'hab_p_medico', 'exp_vida',
'exp_anos_estudo', 'regiao', 'estado', 'porte', 'municipio']]
!git clone https://www.github.com/matterport/Mask_RCNN.git
os.chdir('Mask_RCNN')
 !rm -rf.git
!rm -rf images assets<define_variables>
dftreino=dftotal[~dftotal.nota_mat.isnull() ]
dfsubmissao=dftotal[dftotal.nota_mat.isnull() ]
DATA_DIR = Path('/kaggle/input')
 ROOT_DIR = Path('/kaggle/working' )<concatenate>
dftreino2, dfteste = train_test_split(dftreino, test_size=0.20, random_state=42)
 dftreino2, dfvalida = train_test_split(dftreino2, test_size=0.20, random_state=42)
 rf = RandomForestClassifier(random_state=42, n_estimators=200, min_samples_split=5, max_depth=4 )
sys.path.append(ROOT_DIR/'Mask_RCNN' )<install_modules>
rf.fit(dftreino2[feats], dftreino2['nota_mat'])
 predicao = rf.predict(dfvalida[feats] )
!pip install pycocotools<import_modules>
accuracy_score(dfvalida['nota_mat'], predicao )
from mrcnn.config import Config
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
from mrcnn.model import log<load_from_url>
accuracy_score(dfteste['nota_mat'], rf.predict(dfteste[feats]))
!wget https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5<define_variables>
dfsubmissao['nota_mat']=rf.predict(dfsubmissao[feats])
 dfsubmissao[['codigo_mun','nota_mat']].to_csv('dfresultado.csv', index=False)
COCO_MODEL_PATH = 'mask_rcnn_coco.h5'<define_variables>