Kaggle competition: IESB Sul - IGM - Maio 2019
train40.drop(['sex', 'age_approx', 'anatom_site_general_challenge'],
axis=1,
inplace=True)
 test40.drop(['sex', 'age_approx', 'anatom_site_general_challenge'],
axis=1,
inplace=True )<concatenate>
train = pd.read_csv('.. /input/train.csv', thousands=',')
 test = pd.read_csv('.. /input/test.csv', thousands=',' )
train = pd.concat([train, train40, trainmet], axis=1)
 test = pd.concat([test, test40, testmet], axis=1 )<categorify>
df = pd.concat([train, test], sort=False)
 df.head(10 ).T
sex_dummies = pd.get_dummies(train['sex'], prefix='sex')
 train = pd.concat([train, sex_dummies], axis=1)
 sex_dummies = pd.get_dummies(test['sex'], prefix='sex')
 test = pd.concat([test, sex_dummies], axis=1)
 train.drop(['sex','res','img_name','id','diagnosis','benign_malignant'], axis=1, inplace=True)
 test.drop(['sex','res','img_name','id'], axis=1, inplace=True )<categorify>
df[df['capital']==1]
anatom_dummies = pd.get_dummies(train['location'], prefix='anatom')
 train = pd.concat([train, anatom_dummies], axis=1)
 anatom_dummies = pd.get_dummies(test['location'], prefix='anatom')
 test = pd.concat([test, anatom_dummies], axis=1)
 train.drop('location', axis=1, inplace=True)
 test.drop('location', axis=1, inplace=True )<import_modules>
df['populacao'] = df['populacao'].str.replace(',', '' ).str.replace('.', '')
 df['populacao'] = df['populacao'].apply(lambda x: re.sub(r'(\(\d\)) ', '', x))
df['populacao'] = df['populacao'].astype(float)
 df['comissionados_por_servidor'] = df['comissionados'] / df['servidores']
df['pib_pc'] = df['pib'] / df['populacao']
df['densidade_dem'] = df['populacao'] / df['area']
import xgboost as xgb
from sklearn.model_selection import StratifiedKFold, train_test_split, cross_val_score, cross_validate
from sklearn.metrics import roc_auc_score, roc_curve<prepare_x_and_y>
df = df.sort_values(['estado', 'municipio'] )
X = train.drop('target', axis=1)
 y = train.target<split>
cols = ['jornada_trabalho', 'anos_estudo_empreendedor', 'taxa_empreendedorismo']
df[cols] = df[cols].replace(0, np.NaN )
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.2,
stratify=y,
random_state=42)
 cv = StratifiedKFold(5, shuffle=True, random_state=42 )<choose_model_class>
print(df.isna().sum())
 cols = df.columns[df.isna().any() ].tolist()
cols.remove('nota_mat')
 print(cols)
 for c in cols:
df[c] = df.groupby('estado')[c].transform(lambda x: x.fillna(x.median()))
xg = xgb.XGBClassifier(
 n_estimators=750,
min_child_weight=0.81,
learning_rate=0.025,
max_depth=2,
subsample=0.80,
colsample_bytree=0.42,
gamma=0.10,
random_state=42,
n_jobs=-1,
)<define_variables>
df['educacao_pelo_pib'] = df['gasto_pc_educacao'] / df['pib_pc']
df['servidores_pc'] = df['servidores'] / df['populacao']
estimators = [xg]<compute_train_metric>
categoricas = ['regiao', 'porte']
for cat in categoricas:
dummies = pd.get_dummies(df[cat], prefix=cat)
 df = pd.concat([df, dummies], axis=1)
 cols = [c for c in df.columns if c not in categoricas]
df = df[cols]
def model_check(X_train, y_train, estimators, cv):
model_table = pd.DataFrame()
row_index = 0
for est in estimators:
MLA_name = est.__class__.__name__
model_table.loc[row_index, 'Model Name'] = MLA_name
cv_results = cross_validate(est,
X_train,
y_train,
cv=cv,
scoring='roc_auc',
return_train_score=True,
n_jobs=-1)
 model_table.loc[row_index,
'Train roc Mean'] = cv_results['train_score'].mean()
model_table.loc[row_index,
'Test roc Mean'] = cv_results['test_score'].mean()
model_table.loc[row_index, 'Test Std'] = cv_results['test_score'].std()
model_table.loc[row_index, 'Time'] = cv_results['fit_time'].mean()
row_index += 1
model_table.sort_values(by=['Test roc Mean'],
ascending=False,
inplace=True)
 return model_table<train_model>
cols = ['populacao', 'area', 'pib', 'pib_pc', 'servidores', 'comissionados', 'educacao_pelo_pib', 'densidade_dem', 'gasto_pc_educacao', 'gasto_pc_saude', 'hab_p_medico']
for col in cols:
df[col] = np.log(df[col]+1)
 df['perc_pop_econ_ativa'] = df['perc_pop_econ_ativa'] / 100
cols = [c for c in df.columns if(( df[c].dtype==np.float64)or(df[c].dtype==np.int64)) ]
cols.remove('nota_mat')
 cols.remove('codigo_mun')
 cols.remove('comissionados_por_servidor')
 for c in ['populacao', 'area', 'pib', 'pib_pc', 'servidores', 'comissionados', 'perc_pop_econ_ativa',]:
cols.remove(c)
 for c in cols:
df[c] =(df[c] - df[c].min())/(df[c].max() - df[c].min())
 df.head()
raw_models = model_check(X_train, y_train, estimators, cv)
 display(raw_models )<compute_train_metric>
train, valid = train_test_split(train, random_state=42, test_size=0.2)
 train.shape, valid.shape
xg.fit(X_train, y_train)
 validation = xg.predict_proba(X_test)[:, 1]
roc_auc_score(y_test, validation )<predict_on_test>
params = {'max_depth': 5, 'min_samples_split': 2, 'n_estimators': 250, 'oob_score': False}
rf = RandomForestClassifier(random_state=42, n_jobs=-1, **params)
 rf.fit(train[feats], train['nota_mat'])
 preds = rf.predict(valid[feats] )
predictions = xg.predict_proba(test)[:, 1]<prepare_output>
print(mean_squared_error(valid['nota_mat'], preds)**(1/2))
print(accuracy_score(valid['nota_mat'], preds))
meta_df = pd.DataFrame(columns=['image_name', 'target'])
 meta_df['image_name'] = sample['image_name']
meta_df['target'] = predictions<save_to_csv>
preds = rf.predict(test[feats])
 test['nota_mat'] = preds
test[['codigo_mun', 'nota_mat']].to_csv('saida_forest.csv', index=False )
meta_df.to_csv('meta_with_img_data.csv', header=True, index=False)
<prepare_x_and_y>
def cv(df, test, feats, y_name, k=5, oob_score=False):
preds, score, fis, acc = [], [], [], []
chunk = df.shape[0] // k
for i in range(k):
if i + 1 < k:
valid = df.iloc[i*chunk:(i+1)*chunk]
train = df.iloc[:i*chunk].append(df.iloc[(i+1)*chunk:])
 else:
valid = df.iloc[i*chunk:]
train = df.iloc[:i*chunk]
rf = RandomForestClassifier(random_state=42, n_jobs=-1, n_estimators=200, max_depth=5, oob_score=oob_score)
 rf.fit(train[feats], train[y_name])
 acc.append(accuracy_score(valid[y_name], rf.predict(valid[feats])))
 score.append(mean_squared_error(valid[y_name], rf.predict(valid[feats])) **(1/2))
preds.append(rf.predict(test[feats]))
fis.append(rf.feature_importances_)
 print(i, 'OK')
 return score, preds, fis, acc
adv_train = train.copy()
adv_train.drop('target', axis=1, inplace=True)
 adv_test = test.copy()
adv_train['dataset_label'] = 0
adv_test['dataset_label'] = 1
adv_master = pd.concat([adv_train, adv_test], axis=0)
 adv_X = adv_master.drop('dataset_label', axis=1)
 adv_y = adv_master['dataset_label']<split>
score, preds, fis, acc = cv(train, test, feats, 'nota_mat' )
adv_X_train, adv_X_test, adv_y_train, adv_y_test = train_test_split(adv_X,
adv_y,
test_size=0.4,
stratify=adv_y,
random_state=42 )<train_model>
score, acc
xg_adv = xgb.XGBClassifier(
 random_state=42,
n_jobs=-1, 
)
 xg_adv.fit(adv_X_train, adv_y_train)
 validation = xg_adv.predict_proba(adv_X_test)[:,1]<split>
x = pd.DataFrame(preds )
adv_X.drop(['n_images', 'image_size','width','height','total_pixels','reds','blues','greens','mean_colors', 'age_min', 'age_max'], axis=1, inplace=True)
 adv_X_train, adv_X_test, adv_y_train, adv_y_test = train_test_split(adv_X,
adv_y,
test_size=0.4,
stratify=adv_y,
random_state=42)
 xg_adv.fit(adv_X_train, adv_y_train)
 validation = xg_adv.predict_proba(adv_X_test)[:,1]<drop_column>
test['nota_mat'] = x.mode(axis=0 ).T
X_train.drop(['n_images', 'image_size','width','height','total_pixels','reds','blues','greens','mean_colors', 'age_min', 'age_max'], axis=1, inplace=True)
 test.drop(['n_images', 'image_size','width','height','total_pixels','reds','blues','greens','mean_colors', 'age_min', 'age_max'], axis=1, inplace=True )<choose_model_class>
test[['codigo_mun', 'nota_mat']].to_csv('saida_cross.csv', index=False )
xg= xgb.XGBClassifier(
 n_estimators=750,
learning_rate=0.015,
min_child_weight= 218,
max_delta_step= 4,
max_depth= 2,
subsample= 0.751,
colsample_bytree= 0.77,
gamma= 24,
reg_lambda= 11,
random_state=42,
n_jobs=-1,
)<train_model>
clf = SVC(gamma='auto')
 clf.fit(train[feats], train['nota_mat'])
 preds = clf.predict(valid[feats] )
raw_models = model_check(X_train, y_train, [xg], cv)
 display(raw_models )<save_to_csv>
print(mean_squared_error(valid['nota_mat'], preds)**(1/2))
print(accuracy_score(valid['nota_mat'], preds))
xg.fit(X_train, y_train)
 predictions = xg.predict_proba(test)[:, 1]
meta_df = pd.DataFrame(columns=['image_name', 'target'])
 meta_df['image_name'] = sample['image_name']
meta_df['target'] = predictions
meta_df.to_csv('meta_simplified_img_data.csv', header=True, index=False)
<define_variables>
lr = LogisticRegression(random_state=42, solver='lbfgs', multi_class='multinomial', max_iter=500)
 lr.fit(train[feats], train['nota_mat'])
 preds = lr.predict(valid[feats] )
tf.random.set_seed(seed_val )<define_variables>
print(mean_squared_error(valid['nota_mat'], preds)**(1/2))
print(accuracy_score(valid['nota_mat'], preds))
GCS_PATH = KaggleDatasets().get_gcs_path('melanoma-384x384')
 filenames_train = np.array(tf.io.gfile.glob(GCS_PATH + '/train*.tfrec'))
filenames_test = np.array(tf.io.gfile.glob(GCS_PATH + '/test*.tfrec'))<train_on_grid>
preds = lr.predict(test[feats])
 test['nota_mat'] = preds
test[['codigo_mun', 'nota_mat']].to_csv('saida_lr.csv', index=False )
DEVICE = 'TPU'
if DEVICE == 'TPU':
print('connecting to TPU...')
 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
print('Could not connect to TPU')
 tpu = None
if tpu:
try:
print('Initializing TPU...')
 tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print('TPU initialized')
 except _:
print('Failed to initialize TPU!')
 else:
DEVICE = 'GPU'
if DEVICE != 'TPU':
print('Using default strategy for CPU and single GPU')
 strategy = tf.distribute.get_strategy()
if DEVICE == 'GPU':
print('Num GPUs Available: ',
len(tf.config.experimental.list_physical_devices('GPU')))
 print('REPLICAS: ', strategy.num_replicas_in_sync)
 AUTO = tf.data.experimental.AUTOTUNE<init_hyperparams>
def moda(melhores, treino, teste, feats, alvo):
preds = []
models = {
'RandomForest': RandomForestClassifier(random_state=42, **melhores['RandomForest'][1]),
'ExtraTrees': ExtraTreesClassifier(random_state=42, **melhores['ExtraTrees'][1]),
'AdaBoost': AdaBoostClassifier(random_state=42, **melhores['AdaBoost'][1]),
'GradientBoosting': GradientBoostingClassifier(random_state=42, **melhores['GradientBoosting'][1]),
'DecisionTree': DecisionTreeClassifier(random_state=42, **melhores['DecisionTree'][1]),
'LogisticRegression': LogisticRegression(penalty='l2', random_state=42, **melhores['LogisticRegression'][1]),
'KNN 11': KNeighborsClassifier(**melhores['KNN 11'][1]),
'MLPClassifier': MLPClassifier(random_state=42, **melhores['MLPClassifier'][1]),
'SVC': SVC(**melhores['SVC'][1]),
'GaussianNB': GaussianNB(**melhores['GaussianNB'][1]),
'BernoulliNB': BernoulliNB(**melhores['BernoulliNB'][1]),
}
for nome, modelo in models.items() :
m = modelo
m.fit(treino[feats], treino[alvo])
 preds.append(m.predict(teste[feats]))
df = pd.DataFrame(preds)
 print(df)
 return df.T.mode(axis=1)
cfg = dict(
 batch_size=32,
img_size=384,
lr_start=0.000005,
lr_max=0.00000125,
lr_min=0.000001,
lr_rampup=5,
lr_sustain=0,
lr_decay=0.8,
epochs=12,
transform_prob=1.0,
rot=180.0,
shr=2.0,
hzoom=8.0,
wzoom=8.0,
hshift=8.0,
wshift=8.0,
optimizer='adam',
label_smooth_fac=0.05,
tta_steps=20
)<normalization>
treino = df[-df['nota_mat'].isnull() ]
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift,
width_shift):

 rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1], dtype='float32')
 zero = tf.constant([0], dtype='float32')
 rotation_matrix = tf.reshape(
 tf.concat([c1, s1, zero, -s1, c1, zero, zero, zero, one], axis=0),
[3, 3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(
 tf.concat([one, s2, zero, zero, c2, zero, zero, zero, one], axis=0),
[3, 3])
 zoom_matrix = tf.reshape(
 tf.concat([
one / height_zoom, zero, zero, zero, one / width_zoom, zero, zero,
zero, one
],
axis=0), [3, 3])
 shift_matrix = tf.reshape(
 tf.concat(
 [one, zero, height_shift, zero, one, width_shift, zero, zero, one],
axis=0), [3, 3])
 return K.dot(K.dot(rotation_matrix, shear_matrix),
K.dot(zoom_matrix, shift_matrix))
def transform(image, cfg):

 DIM = cfg['img_size']
XDIM = DIM % 2
rot = cfg['rot'] * tf.random.normal([1], dtype='float32')
 shr = cfg['shr'] * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ cfg['hzoom']
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ cfg['wzoom']
h_shift = cfg['hshift'] * tf.random.normal([1], dtype='float32')
 w_shift = cfg['wshift'] * tf.random.normal([1], dtype='float32')
 m = get_mat(rot, shr, h_zoom, w_zoom, h_shift, w_shift)
 x = tf.repeat(tf.range(DIM // 2, -DIM // 2, -1), DIM)
 y = tf.tile(tf.range(-DIM // 2, DIM // 2), [DIM])
 z = tf.ones([DIM * DIM], dtype='int32')
 idx = tf.stack([x, y, z])
 idx2 = K.dot(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM // 2 + XDIM + 1, DIM // 2)
 idx3 = tf.stack([DIM // 2 - idx2[0, ], DIM // 2 - 1 + idx2[1, ]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d, [DIM, DIM, 3])
 def prepare_image(img, cfg=None, augment=True):

 img = tf.image.decode_jpeg(img, channels=3)
 img = tf.image.resize(img, [cfg['img_size'], cfg['img_size']],
antialias=True)
 img = tf.cast(img, tf.float32)/ 255.0
if augment:
if cfg['transform_prob'] > tf.random.uniform([1], minval=0, maxval=1):
img = transform(img, cfg)
 img = tf.image.random_flip_left_right(img)
 img = tf.image.random_saturation(img, 0.7, 1.3)
 img = tf.image.random_contrast(img, 0.8, 1.2)
 img = tf.image.random_brightness(img, 0.1)
 return img<categorify>
m = moda(melhores, train, test, feats, 'nota_mat')
 test['nota_mat'] = m
test[['codigo_mun', 'nota_mat']].to_csv('saida_moda.csv', index=False)
 print(test[['codigo_mun', 'nota_mat']] )
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
'image': tf.io.FixedLenFeature([], tf.string),
'image_name': tf.io.FixedLenFeature([], tf.string),
'patient_id': tf.io.FixedLenFeature([], tf.int64),
'sex': tf.io.FixedLenFeature([], tf.int64),
'age_approx': tf.io.FixedLenFeature([], tf.int64),
'anatom_site_general_challenge': tf.io.FixedLenFeature([], tf.int64),
'diagnosis': tf.io.FixedLenFeature([], tf.int64),
'target': tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 return example['image'], example['target']
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
'image': tf.io.FixedLenFeature([], tf.string),
'image_name': tf.io.FixedLenFeature([], tf.string),
'patient_id': tf.io.FixedLenFeature([], tf.int64),
'sex': tf.io.FixedLenFeature([], tf.int64),
'age_approx': tf.io.FixedLenFeature([], tf.int64),
'anatom_site_general_challenge': tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 return example['image'], example['image_name']
def count_data_items(filenames):
n = [
int(re.compile(r'-([0-9]*)\.' ).search(filename ).group(1))
for filename in filenames
]
return np.sum(n )<choose_model_class>
param = {'max_depth': 4, 'max_leaf_nodes': 2, 'min_samples_split': 2, 'n_estimators': 200}
gb = GradientBoostingClassifier(**param)
 gb.fit(treino[feats], treino['nota_mat'])
 preds = gb.predict(test[feats])
 test['nota_mat'] = preds
test[['codigo_mun', 'nota_mat']].to_csv('saida_gb.csv', index=False )
def compileNewModel(cfg):

 with strategy.scope() :
model = get_model()
with strategy.scope() :
model.compile(optimizer=cfg['optimizer'],
loss=[
tf.keras.losses.BinaryCrossentropy(
 label_smoothing=cfg['label_smooth_fac']),
tf.keras.losses.BinaryCrossentropy(
 label_smoothing=cfg['label_smooth_fac']),
tf.keras.losses.BinaryCrossentropy(
 label_smoothing=cfg['label_smooth_fac'])
 ],
metrics=[tf.keras.metrics.AUC(name='auc')])
 return model
def getLearnRateCallback(cfg):

 lr_start = cfg['lr_start']
lr_max = cfg['lr_max'] * strategy.num_replicas_in_sync * cfg['batch_size']
lr_min = cfg['lr_min']
lr_rampup = cfg['lr_rampup']
lr_sustain = cfg['lr_sustain']
lr_decay = cfg['lr_decay']
def lrfn(epoch):
if epoch < lr_rampup:
lr =(lr_max - lr_start)/ lr_rampup * epoch + lr_start
elif epoch < lr_rampup + lr_sustain:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_decay**(epoch - lr_rampup -
lr_sustain)+ lr_min
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)
 return lr_callback
def learnModel(model, ds_train, stepsTrain, cfg, ds_val=None, stepsVal=0):

 callbacks = [getLearnRateCallback(cfg)]
history = model.fit(ds_train,
validation_data=ds_val,
verbose=1,
steps_per_epoch=stepsTrain,
validation_steps=stepsVal,
epochs=cfg['epochs'],
callbacks=callbacks)
 return history<load_pretrained>
pca = PCA(n_components=4)
 pca.fit(treino[feats])
 print(pca.explained_variance_ratio_)
 print(pca.singular_values_)
 novo = pca.fit_transform(treino[feats])
 print(novo, novo.shape)
 novo_valid = pca.transform(valid[feats])
 novo_teste = pca.fit_transform(test[feats])
 params = {'C': 0.1, 'dual': True, 'max_iter': 130}
lg = LogisticRegression(penalty='l2', **params)
 lg.fit(novo, treino['nota_mat'])
 pred = lg.predict(novo_valid)
 print(valid.shape)
 print(accuracy_score(valid['nota_mat'], pred))
print(lg.score(novo_valid, valid['nota_mat']))
test['nota_mat'] = preds
test[['codigo_mun', 'nota_mat']].to_csv('saida_lg2.csv', index=False )
ds_train = getTrainDataset(
 filenames_train, cfg ).map(lambda img, label:(img,(label, label, label)))
 stepsTrain = count_data_items(filenames_train)/ \ 
(cfg['batch_size'] * strategy.num_replicas_in_sync)
 model = compileNewModel(cfg)
 learnModel(model, ds_train, stepsTrain, cfg )<predict_on_test>
gnb = GaussianNB(var_smoothing=1e-9)
 gnb.fit(train[feats], train['nota_mat'])
 print(gnb.score(valid[feats], valid['nota_mat']))
pred = gnb.predict(test[feats])
 test[['codigo_mun', 'nota_mat']].to_csv('saida_gnb.csv', index=False )
steps = count_data_items(filenames_test)/ \ 
(cfg['batch_size'] * strategy.num_replicas_in_sync)
 z = np.zeros(( cfg['batch_size'] * strategy.num_replicas_in_sync))
ds_testAug = getTestDataset(
 filenames_test, cfg, augment=True,
repeat=True ).map(lambda img, label:(img,(z, z, z)))
 probs = model.predict(ds_testAug, verbose=1, steps=steps * cfg['tta_steps'])
 probs = np.stack(probs)
 probs = probs[:, :count_data_items(filenames_test)* cfg['tta_steps']]
probs = np.stack(np.split(probs, cfg['tta_steps'], axis=1), axis=1)
 probs = np.mean(probs, axis=1)
 test = pd.read_csv(os.path.join(base_path, 'test.csv'))
y_test_sorted = np.zeros(( 3, probs.shape[1]))
test = test.reset_index()
test = test.set_index('image_name')
 i = 0
ds_test = getTestDataset(filenames_test, cfg)
 for img, imgid in tqdm(iter(ds_test.unbatch())) :
imgid = imgid.numpy().decode('utf-8')
 y_test_sorted[:, test.loc[imgid]['index']] = probs[:, i, 0]
i += 1
for i in range(y_test_sorted.shape[0]):
submission = sample
submission['target'] = y_test_sorted[i]
submission.to_csv('submission_model_%s.csv' % i, index=False)
 submission = sample
submission['target'] = np.mean(y_test_sorted, axis=0)
 submission.to_csv('blended_effnets.csv', index=False )<save_to_csv>
mnb = MultinomialNB()
mnb.fit(train[feats], train['nota_mat'])
 print(mnb.score(valid[feats], valid['nota_mat']))
pred = mnb.predict(test[feats])
 test['nota_mat'] = pred
test[['codigo_mun', 'nota_mat']].to_csv('saida_mnb.csv', index=False )
effnet = pd.read_csv('./blended_effnets.csv')
 meta = pd.read_csv('./meta_simplified_img_data.csv')
 sample['target'] =(
 effnet['target'] * 0.9 +
meta['target'] * 0.1 
)
 sample.to_csv('ensembled.csv', header=True, index=False )<install_modules>
bnb = BernoulliNB(alpha=0)
 bnb.fit(train[feats], train['nota_mat'])
 print(bnb.score(valid[feats], valid['nota_mat']))
pred = bnb.predict(test[feats])
 test['nota_mat'] = pred
test[['codigo_mun', 'nota_mat']].to_csv('saida_bnb.csv', index=False )
!pip install -q efficientnet_pytorch<import_modules>
melhores = {'max_depth': 2, 'min_samples_split': 2, 'n_estimators': 100, 'oob_score': False}
lr = LogisticRegression(solver='lbfgs', multi_class='multinomial', random_state=42)
 rf = RandomForestClassifier(random_state=42, **melhores)
 bnb = BernoulliNB(alpha=0)
 gnb = GaussianNB(var_smoothing=1e-9)
 vc = VotingClassifier(estimators=[('lr', lr),('rf', rf),('gnb', gnb),('bnb', bnb)], voting='hard')
 vc.fit(train[feats], train['nota_mat'])
 vc.score(valid[feats], valid['nota_mat'])
 pred = vc.predict(test[feats])
 test['nota_mat'] = pred
test[['codigo_mun', 'nota_mat']].to_csv('voting.csv', index=False)