Kaggle competition: IESB - 2019
X_train = np.asarray(column_choice_training.drop(['Survived'], axis=1))
X_train = preprocessing.StandardScaler().fit(X_train ).transform(X_train)
 X_train[0:5]<normalization>
df = pd.read_csv('.. /input/train.csv')
 ts = pd.read_csv('.. /input/test.csv' )
X_test = np.asarray(column_choice_test)
 X_test = preprocessing.StandardScaler().fit(X_test ).transform(X_test)
 X_test[0:5]<prepare_x_and_y>
df = df.append(ts )
y_train = np.asarray(column_choice_training['Survived'])
 y_train[0:5]<train_model>
df['codigo_mun'] = df['codigo_mun'].str.replace('ID_ID_', '' )
print('Train set:', X_train.shape,y_train.shape)
 print('Test set:', X_test.shape )<define_search_space>
lista = ['estado','municipio','porte','populacao','pib','anos_estudo_empreendedor','jornada_trabalho','gasto_pc_educacao','exp_anos_estudo','nota_mat']
RFC = RandomForestClassifier(oob_score = True, random_state = 1)
 param_grid = {'min_samples_leaf' : [1, 2, 4, 6, 8, 10],
'min_samples_split' : [2, 4, 6, 8, 10, 12, 14, 16],
'n_estimators': [200, 500, 800, 1100]}
param_distributions = param_grid,
n_iter = 100,
cv = cross_validation_split, verbose = 2, random_state = 0, n_jobs = -1)
 print(RS.best_score_)
 print(RS.best_params_ )<train_on_grid>
for i in df[lista].select_dtypes(include=['object']):
df[i] = df[i].astype('category' ).cat.codes
param_grid = { 'criterion' : ['gini', 'entropy'],
'min_samples_leaf' : [1, 2, 3],
'min_samples_split' : [3, 4, 5],
'n_estimators': [1000, 1100, 1200]}
print(GS.best_score_)
 print(GS.best_params_ )<choose_model_class>
ts = df[df['nota_mat']==-2]
df = df[df['nota_mat']!=-2]
best_model = RandomForestClassifier(n_estimators=1000,
oob_score = True,
criterion = 'gini',
min_samples_leaf = 3,
min_samples_split = 8,
max_depth = None,
random_state = 1 ).fit(X_train,y_train)
 yhat = best_model.predict(X_train)
 print("%.4f" % best_model.oob_score_)
 importance_df = pd.concat(( pd.DataFrame(column_choice_training.drop(['Survived'], axis=1 ).columns, columns = ['variable']),
pd.DataFrame(best_model.feature_importances_, columns = ['importance'])) ,
axis = 1 ).sort_values(by='importance', ascending = False)
 importance_df<save_to_csv>
train, valid = train_test_split(df[lista], random_state=42)
 train.shape, valid.shape
yhat_test = best_model.predict(X_test)
 submission = testing_df.copy()
submission['Survived'] = yhat_test
submission.to_csv('submission.csv', columns=['PassengerId', 'Survived'], index=False)
 submission[['PassengerId', 'Survived']].head(15 )<data_type_conversions>
rf = RandomForestRegressor(random_state=42, n_estimators=1000)
 feats = [c for c in df[lista].columns if c not in ['nota_mat']]
rf.fit(train[feats], train['nota_mat'] )
class Dataset(Dataset):
def __init__(self, path, transform=None):
self.data = pd.read_csv(path)
 self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, index):
item = self.data.iloc[index]
image = item[1:].values.astype(np.uint8 ).reshape(( 28, 28))
label = item[0]
if self.transform is not None:
image = self.transform(image)
 return image, label<categorify>
mean_squared_error(rf.predict(valid[feats]), valid['nota_mat'])**(1/2)
 valid_preds = rf.predict(valid[feats])
 mean_squared_error(valid['nota_mat'], valid_preds)**(1/2 )
path = '.. /input/train.csv'
VALID_SIZE = 0.2
train_transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,))
])
 valid_transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor() ,
transforms.Normalize(mean=(0.5,), std=(0.5,))
])
 train_data = Dataset(path, train_transform)
 valid_data = Dataset(path, valid_transform)
 trainloader = DataLoader(train_data, batch_size = 1500, shuffle = True)
 testloader = DataLoader(valid_data, batch_size = 1500, shuffle = False)
 len(trainloader )<import_modules>
ts['nota_mat'] = rf.predict(ts[feats] )
<load_from_url><EOS> 
ts[['codigo_mun','nota_mat']].to_csv('lucio.csv',index=False )