Kaggle competition: IESB - 2019
score = model_sol_1.evaluate(splitted_test_X, ohe_splitted_test_y, verbose=0)
 accuracy = 100 * score[1]
print('Test accuracy: %4f%%' % accuracy )<train_model>
df = pd.read_csv(".. /input/train.csv")
 ts = pd.read_csv(".. /input/test.csv" )
checkpointer = ModelCheckpoint(filepath='mnist.model.best.hdf5', verbose=1, save_best_only=True)
 hist_sol_1 = model_sol_1.fit(splitted_train_X, ohe_splitted_train_y, batch_size=128, epochs=10,
validation_split=0.2, callbacks=[checkpointer],
verbose=2, shuffle=True )<train_model>
import math
model_sol_1.load_weights('mnist.model.best.hdf5')
 score = model_sol_1.evaluate(splitted_test_X, ohe_splitted_test_y, verbose=0)
 accuracy = 100 * score[1]
print('Test accuracy: %.4f%%' % accuracy )<predict_on_test>
math.log(1000)- math.log(1100), math.log(10)- math.log(11 )
predictions = model_sol_1.predict(test_x)
 predictions = [ np.argmax(x)for x in predictions ]<save_to_csv>
test = df[df['nota_mat'].isnull() ]
df = df[~df['nota_mat'].isnull() ]
submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 submission.drop('Label', axis=1, inplace=True)
 submission['Label'] = predictions
submission.to_csv('submission1.csv', index=False )<concatenate>
for c in df.columns:
if df[c].dtype == 'object':
df[c] = df[c].astype('category' ).cat.codes
extended_splitted_train_X = splitted_train_X[..., tf.newaxis]
extended_splitted_test_X = splitted_test_X[..., tf.newaxis]
extended_splitted_test_X.shape<choose_model_class>
from sklearn.model_selection import train_test_split
model_sol_2 = Sequential()
model_sol_2.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu', input_shape=extended_splitted_train_X.shape[1:]))
model_sol_2.add(MaxPooling2D(pool_size=2))
model_sol_2.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
model_sol_2.add(MaxPooling2D(pool_size=2))
model_sol_2.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
model_sol_2.add(MaxPooling2D(pool_size=2))
model_sol_2.add(Flatten())
 model_sol_2.add(Dense(64))
model_sol_2.add(Activation('relu'))
model_sol_2.add(Dropout(0.2))
model_sol_2.add(Dense(10, activation='softmax')) 
<choose_model_class>
from sklearn.model_selection import train_test_split
model_sol_2.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'] )<compute_test_metric>
df, valid = train_test_split(df, random_state=42 )
score = model_sol_2.evaluate(extended_splitted_test_X, ohe_splitted_test_y, verbose=0)
 accuracy = 100 * score[1]
print('Test accuracy: %4f%%' % accuracy )<train_model>
removed_cols = ['municipio','codigo_mun']
checkpointer = ModelCheckpoint(filepath='mnist.model.best.hdf5', verbose=1, save_best_only=True)
 hist_sol_2 = model_sol_2.fit(extended_splitted_train_X, ohe_splitted_train_y, batch_size=128,
epochs=10, callbacks=[checkpointer],
verbose=2, validation_data=(extended_splitted_test_X, ohe_splitted_test_y), shuffle=True )<train_model>
from sklearn.tree import DecisionTreeRegressor
model_sol_2.load_weights('mnist.model.best.hdf5')
 score = model_sol_2.evaluate(extended_splitted_test_X, ohe_splitted_test_y, verbose=0)
 accuracy = 100 * score[1]
print('Test accuracy: %.4f%%' % accuracy )<save_to_csv>
dt = DecisionTreeRegressor(random_state=42, max_depth=2 )
extended_test_x = test_x[..., tf.newaxis]
predictions = model_sol_2.predict(extended_test_x)
 predictions = [ np.argmax(x)for x in predictions ]
submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 submission.drop('Label', axis=1, inplace=True)
 submission['Label'] = predictions
submission.to_csv('submission2.csv', index=False )<define_variables>
lista= ['estado','anos_estudo_empreendedor','nota_mat', 'municipio','codigo_mun','area','capital','codigo_mun','comissionados','comissionados_por_servidor','densidade_dem','estado','exp_anos_estudo','exp_vida','gasto_pc_educacao','gasto_pc_saude','hab_p_medico','idhm','indice_governanca','jornada_trabalho','municipio','nota_mat','participacao_transf_receita', 'perc_pop_econ_ativa','pib','pib_pc','populacao','porte','ranking_igm','regiao','servidores','taxa_empreendedorismo']
image_augmentator = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.2,
zoom_range=0.1,
fill_mode='nearest')
 batch_size = 32
train_batches = image_augmentator.flow(extended_splitted_train_X, ohe_splitted_train_y, batch_size=batch_size)
 val_batches = image_augmentator.flow(extended_splitted_test_X, ohe_splitted_test_y, batch_size=batch_size )<choose_model_class>
train,valid = train_test_split(df,random_state=42)
 train.shape,valid.shape
model_sol_3 = Sequential()
model_sol_3.add(Conv2D(filters=16, kernel_size=3, padding='same', activation='relu', input_shape=extended_splitted_train_X.shape[1:]))
model_sol_3.add(Conv2D(filters=16, kernel_size=3, padding='same', activation='relu'))
model_sol_3.add(MaxPooling2D(pool_size=2))
model_sol_3.add(Dropout(0.1))
model_sol_3.add(Conv2D(filters=16, kernel_size=3, padding='same', activation='relu'))
model_sol_3.add(MaxPooling2D(pool_size=2))
model_sol_3.add(Flatten())
 model_sol_3.add(Dense(64))
model_sol_3.add(Activation('relu'))
model_sol_3.add(Dropout(0.2))
model_sol_3.add(Dense(10, activation='softmax')) 
<choose_model_class>
rf = RandomForestRegressor(random_state=42,n_estimators=100)
 feats = [c for c in df[lista].columns if c not in ['nota_mat']]
rf.fit(train[feats], train['nota_mat'] )
model_sol_3.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'] )<train_model>
math.exp(2.78), math.exp(5.27 )
checkpointer = ModelCheckpoint(filepath='mnist.model.best.hdf5', verbose=1, save_best_only=True)
 hist_sol_3 = model_sol_3.fit_generator(generator=train_batches, steps_per_epoch =extended_splitted_train_X.shape[0] // batch_size,
epochs=32, callbacks=[checkpointer],
validation_data=val_batches, validation_steps=extended_splitted_test_X.shape[0] // batch_size,
verbose=2 )<train_model>
from sklearn.metrics import mean_squared_error
model_sol_3.load_weights('mnist.model.best.hdf5')
 score = model_sol_3.evaluate(extended_splitted_test_X, ohe_splitted_test_y, verbose=0)
 accuracy = 100 * score[1]
print('Test accuracy: %.4f%%' % accuracy )<save_to_csv>
from sklearn.metrics import mean_squared_error
extended_test_x = test_x[..., tf.newaxis]
predictions = model_sol_3.predict(extended_test_x)
 predictions = [ np.argmax(x)for x in predictions ]
submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 submission.drop('Label', axis=1, inplace=True)
 submission['Label'] = predictions
submission.to_csv('submission3.csv', index=False )<choose_model_class>
df['preds'] = df['nota_mat'].mean()
model_sol_4_1 = Sequential()
model_sol_4_1.add(Conv2D(filters=16, kernel_size=3, padding='same', activation='relu', input_shape=extended_splitted_train_X.shape[1:]))
model_sol_4_1.add(BatchNormalization())
 model_sol_4_1.add(Conv2D(filters=16, kernel_size=3, padding='same', activation='relu'))
model_sol_4_1.add(MaxPooling2D(pool_size=2))
model_sol_4_1.add(Dropout(0.1))
model_sol_4_1.add(BatchNormalization())
 model_sol_4_1.add(Conv2D(filters=16, kernel_size=3, padding='same', activation='relu'))
model_sol_4_1.add(MaxPooling2D(pool_size=2))
model_sol_4_1.add(Flatten())
 model_sol_4_1.add(BatchNormalization())
 model_sol_4_1.add(Dense(64))
model_sol_4_1.add(Activation('relu'))
model_sol_4_1.add(Dropout(0.2))
model_sol_4_1.add(BatchNormalization())
 model_sol_4_1.add(Dense(10, activation='softmax')) 
<choose_model_class>
from sklearn.ensemble import RandomForestRegressor
model_sol_4_1.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'] )<train_model>
from sklearn.ensemble import RandomForestRegressor
checkpointer = ModelCheckpoint(filepath='mnist.model.best.hdf5', verbose=1, save_best_only=True)
 hist_sol_4 = model_sol_4_1.fit_generator(generator=train_batches, steps_per_epoch =extended_splitted_train_X.shape[0] // batch_size,
epochs=32, callbacks=[checkpointer],
validation_data=val_batches, validation_steps=extended_splitted_test_X.shape[0] // batch_size,
verbose=2 )<train_model>
df_preds = rf.predict(valid[feats] )
model_sol_4_1.load_weights('mnist.model.best.hdf5')
 score = model_sol_4_1.evaluate(extended_splitted_test_X, ohe_splitted_test_y, verbose=0)
 accuracy = 100 * score[1]
print('Test accuracy: %.4f%%' % accuracy )<choose_model_class>
mean_squared_error(valid['nota_mat'], df_preds)**(1/2 )
model_sol_4_2 = Sequential()
model_sol_4_2.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu', input_shape=extended_splitted_train_X.shape[1:]))
model_sol_4_2.add(BatchNormalization())
 model_sol_4_2.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
model_sol_4_2.add(MaxPooling2D(pool_size=2))
model_sol_4_2.add(Dropout(0.1))
model_sol_4_2.add(BatchNormalization())
 model_sol_4_2.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
model_sol_4_2.add(MaxPooling2D(pool_size=2))
model_sol_4_2.add(Flatten())
 model_sol_4_2.add(BatchNormalization())
 model_sol_4_2.add(Dense(64))
model_sol_4_2.add(Activation('relu'))
model_sol_4_2.add(Dropout(0.2))
model_sol_4_2.add(BatchNormalization())
 model_sol_4_2.add(Dense(10, activation='softmax')) 
<choose_model_class>
valid_preds=rf.predict(valid[feats] )
model_sol_4_2.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'] )<train_model>
mean_squared_error(valid['nota_mat'], valid_preds)**(1/2 )
checkpointer = ModelCheckpoint(filepath='mnist.model.best.hdf5', verbose=1, save_best_only=True)
 hist_sol_4 = model_sol_4_2.fit_generator(generator=train_batches, steps_per_epoch=extended_splitted_train_X.shape[0] // batch_size,
epochs=32, callbacks=[checkpointer],
validation_data=val_batches, validation_steps=extended_splitted_test_X.shape[0] // batch_size,
verbose=2 )<train_model>
ts.fillna(-1.0, inplace=True )
model_sol_4_2.load_weights('mnist.model.best.hdf5')
 score = model_sol_4_2.evaluate(extended_splitted_test_X, ohe_splitted_test_y, verbose=0)
 accuracy = 100 * score[1]
print('Test accuracy: %.4f%%' % accuracy )<save_to_csv>
ts['codigo_mun'] = ts['codigo_mun'].str.replace('ID_ID_','')
 ts['codigo_mun'] = ts['codigo_mun'].values.astype('int64' )
extended_test_x = test_x[..., tf.newaxis]
predictions = model_sol_4_2.predict(extended_test_x)
 predictions = [ np.argmax(x)for x in predictions ]
submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 submission.drop('Label', axis=1, inplace=True)
 submission['Label'] = predictions
submission.to_csv('submission4.csv', index=False )<data_type_conversions>
for c in ts.columns:
if ts[c].dtype == 'object':
ts[c] = df[c].astype('category' ).cat.codes
train_y_sol5 = mnist_train_complete.iloc[:, 0].values.astype('int32')
 train_x_sol5 = mnist_train_complete.iloc[:, 1:].values.astype('float32')
 test_x_sol5 = mnist_test_complete.values.astype('float32')
 train_x_sol5 = train_x_sol5.reshape(train_x_sol5.shape[0], 28, 28)
 test_x_sol5 = test_x_sol5.reshape(test_x_sol5.shape[0], 28, 28)
 train_x_sol5 = train_x_sol5[..., tf.newaxis]
test_x_sol5 = test_x_sol5[..., tf.newaxis]<data_type_conversions>
ts.fillna(-1.0, inplace=True )
mean_px = train_x_sol5.mean().astype(np.float32)
 std_px = train_x_sol5.std().astype(np.float32)
 def standardize(x):
return(x-mean_px)/std_px<split>
ts['nota_mat']=rf.predict(ts[feats] )
<choose_model_class><EOS> 
ts[['codigo_mun','nota_mat']].to_csv('trabalho.csv', index=False )