Kaggle competition: Costa Rican Household Poverty Level Prediction
<SOS> metric: MacroFScore Kaggle data source: costa-rican-household-poverty-level-prediction<import_modules>
%matplotlib inline
plt.style.use('fivethirtyeight')
 plt.rcParams['font.size'] = 18
plt.rcParams['patch.edgecolor'] = 'k'
Input,
Dense,
LSTM,
GlobalAveragePooling1D,
AveragePooling1D,
TimeDistributed,
Flatten,
Bidirectional,
Dropout,
Masking,
Layer,
BatchNormalization 
)
<choose_model_class>
pd.options.display.max_columns = 150
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train.head()
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=5)
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, min_delta=0.01 )<define_variables>
from collections import OrderedDict
BATCH_SIZE = 1024
N_EPOCHS = 30<categorify>
map={'yes':1,"no":0}
for df in [train,test]:
df['dependency']=df['dependency'].replace(map ).astype(np.float64)
 df['edjefe']=df['edjefe'].replace(map ).astype(np.float64)
 df['edjefa']=df['edjefa'].replace(map ).astype(np.float64)
 train[['dependency','edjefe','edjefa']].describe()
class NonMasking(Layer):
def __init__(self, **kwargs):
self.supports_masking = True
super(NonMasking, self ).__init__(**kwargs)
 def build(self, input_shape):
input_shape = input_shape
def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
return x
def get_output_shape_for(self, input_shape):
return input_shape<define_search_model>
test['Target']=np.nan
data=train.append(test,ignore_index=True )
def get_model_deep(shape=(MAX_SEQ_LEN, N_FEATURES)) :
inp = Input(shape)
 x = Dense(16 )(inp)
 x = BatchNormalization()(x)
 x = Masking(mask_value=0.)(x)
 x = Bidirectional(LSTM(64, return_sequences=True))(x)
 x = BatchNormalization()(x)
 x = TimeDistributed(Dense(64))(x)
 x = BatchNormalization()(x)
 x = Bidirectional(LSTM(128, return_sequences=True))(x)
 x = BatchNormalization()(x)
 x = TimeDistributed(Dense(1))(x)
 x = BatchNormalization()(x)
 x = NonMasking()(x)
 x = Flatten()(x)
 x = Dropout(0.5 )(x)
 x = Dense(1 )(x)
 model = Model(inp, x)
 return model<choose_model_class>
X = [[0],[1],[2]]
Y = [ 1, 2, 1 ]
dtc = DTC(max_depth=1 )
model = get_model_deep()
model.compile(optimizer=Nadam(lr=0.001), loss='mae')
 model.summary()<train_model>
dtc.fit(X,Y)
 print(dtc.tree_.threshold)
 print(dtc.tree_.impurity)
history = model.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[early_stopping, reduce_lr] )<save_to_csv>
dtc.fit(X,Y,sample_weight=[1,2,3])
 print(dtc.tree_.threshold)
 print(dtc.tree_.impurity)
y_pred = model.predict(X_test, batch_size=BATCH_SIZE)
 submission = pd.DataFrame({'Id': test_ids, 'Expected': y_pred.reshape(-1)})
 submission.to_csv('submission.csv', index=False )<define_variables>
data['Target'].value_counts()
INPUT_WIDTH = 19
N_FEATURES = 22
THRESHOLD = 73<load_from_csv>
heads=data.loc[data['parentesco1'] == 1].copy()
train_labels=data.loc[(data['Target'].notnull())&(data['parentesco1']==1),['Target','idhogar']]
train_df = pd.read_csv(".. /input/train.csv")
 train_df[train_df.columns[1:]] = train_df[train_df.columns[1:]].astype(np.float32)
 train_df.head(20 )<categorify>
type_counts=train_labels['Target'].value_counts()
train_new = train_new.fillna(0.0)
 train_new = train_new.reset_index(drop=True)
 train_new.head()<create_dataframe>
type_counts=pd.DataFrame(type_counts )
df_temp = pd.DataFrame(train_new.groupby('Id')['Expected'].mean())
 meaningful_ids = np.array(df_temp[df_temp['Expected'] < THRESHOLD].index)
 del df_temp
train_final = train_new[np.in1d(train_new.Id, meaningful_ids)]
del train_new, meaningful_ids
train_final.shape<prepare_x_and_y>
type_counts['level']=type_counts.index
train_gp = train_final.groupby("Id")
 train_size = len(train_gp)
 del train_final
X_train = np.zeros(( train_size, INPUT_WIDTH, N_FEATURES), dtype=np.float32)
 y_train = np.zeros(train_size, dtype=np.float32)
 seq_len_train = np.zeros(train_size, dtype=np.float32)
 i = 0
for _, group in train_gp:
X = group.values
seq_len = X.shape[0]
X_train[i,:seq_len,:] = X[:,1:23]
y_train[i] = X[0,23]
seq_len_train[i] = seq_len
i += 1
del X
del train_gp
X_train.shape, y_train.shape<load_from_csv>
type_counts['level']=type_counts['level'].replace(poverty_mapping)
test_df = pd.read_csv(".. /input/test.csv")
 test_df[test_df.columns[1:]] = test_df[test_df.columns[1:]].astype(np.float32)
 test_ids = np.array(test_df.Id.unique())
 test_final = test_df.fillna(0.0)
 test_final = test_final.reset_index(drop=True)
 del test_df<categorify>
type_counts['Count']=type_counts['Target']
test_gp = test_final.groupby("Id")
 test_size = len(test_gp)
 del test_final
X_test = np.zeros(( test_size, INPUT_WIDTH, N_FEATURES), dtype=np.float32)
 seq_len_test = np.zeros(test_size, dtype=np.float32)
 i = 0
for _, group in test_gp:
X = group.values
seq_len = X.shape[0]
X_test[i,:seq_len,:] = X[:,1:23]
seq_len_test[i] = seq_len
i += 1
del X
del test_gp
X_test.shape<import_modules>
all_equal=train.groupby('idhogar')['Target'].apply(lambda x: x.nunique() ==1 )
TimeDistributed, Flatten, Bidirectional 
<choose_model_class>
not_equal= all_equal[all_equal!=True]
es_callback = EarlyStopping(monitor='val_loss', min_delta=0, patience=5 )<define_variables>
example=train.loc[train['idhogar']==not_equal.index[0],['idhogar', 'parentesco1', 'Target']]
BATCH_SIZE = 1024
N_EPOCHS = 50<choose_model_class>
households_leader=train.groupby('idhogar')['parentesco1'].sum()
def get_model_simple(shape=(19,22)) :
inp = Input(shape)
 x = CuDNNLSTM(64, return_sequences=False )(inp)
 x = Dense(1 )(x)
 model = Model(inp, x)
 return model<train_model>
households_leader_flase=train.loc[train['idhogar'].isin(households_leader
[households_leader !=1].index),:]
model_0.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[es_callback] )<save_to_csv>
for household in not_equal.index:
true_target= int(train[(train['idhogar']==household)& 
(train['parentesco1']==1.0)]['Target'])
 train.loc[train['idhogar']==household, 'Target']=true_target
y_pred_0 = model_0.predict(X_test)
 submission_0 = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_0.reshape(-1)})
 submission_0.to_csv('submission_0.csv', index=False )<choose_model_class>
missing = pd.DataFrame(data.isnull().sum() ).rename(columns={0:'total'})
 missing['Percent']=missing['total']/len(data)
 missing.sort_values('Percent',ascending=False ).head(10 )
def get_model_seq(shape=(19,22)) :
inp = Input(shape)
 x = CuDNNLSTM(64, return_sequences=True )(inp)
 x = TimeDistributed(Dense(10))(x)
 x = Flatten()(x)
 x = Dense(1 )(x)
 model = Model(inp, x)
 return model<train_model>
heads.groupby('v18q')['v18q1'].apply(lambda x:x.isnull().sum() )
model_1.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[es_callback] )<save_to_csv>
data['v18q1']=data['v18q1'].fillna(0 )
y_pred_1 = model_1.predict(X_test)
 submission_1 = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_1.reshape(-1)})
 submission_1.to_csv('submission_1.csv', index=False )<choose_model_class>
own_variables=[x for x in data if x.startswith('tipo')]
def get_model_bilstm(shape=(19,22)) :
inp = Input(shape)
 x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(inp)
 x = TimeDistributed(Dense(10))(x)
 x = Flatten()(x)
 x = Dense(1 )(x)
 model = Model(inp, x)
 return model<train_model>
owns=pd.DataFrame(data.loc[data['tipovivi3']==1,'Target'] )
model_2.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[es_callback] )<save_to_csv>
owns['Target'].value_counts()
y_pred_2 = model_2.predict(X_test)
 submission_2 = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_2.reshape(-1)})
 submission_2.to_csv('submission_2.csv', index=False )<choose_model_class>
data.loc[(data['tipovivi1'] == 1), 'v2a1'] = 0
def get_model_deep(shape=(19,22)) :
inp = Input(shape)
 x = Dense(16 )(inp)
 x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(x)
 x = TimeDistributed(Dense(64))(x)
 x = Bidirectional(CuDNNLSTM(128, return_sequences=True))(x)
 x = TimeDistributed(Dense(1))(x)
 x = AveragePooling1D()(x)
 x = Flatten()(x)
 x = Dense(1 )(x)
 model = Model(inp, x)
 return model<train_model>
data['v2a1-missing'] = data['v2a1'].isnull()
model_3.fit(X_train, y_train,
batch_size=BATCH_SIZE, epochs=N_EPOCHS,
validation_split=0.2, callbacks=[es_callback] )<save_to_csv>
data['v2a1-missing'].value_counts()
y_pred_3 = model_3.predict(X_test)
 submission_3 = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_3.reshape(-1)})
 submission_3.to_csv('submission_3.csv', index=False )<save_to_csv>
data['v2a1'] = data['v2a1'].fillna(value=data['tipovivi3'] )
y_pred_avg =(y_pred_0 + y_pred_1 + y_pred_2 + y_pred_3)/ 4
submission_avg = pd.DataFrame({'Id': test_ids, 'Expected': y_pred_avg.reshape(-1)})
 submission_avg.to_csv('submission_avg.csv', index=False )<load_from_csv>
data.loc[(( data['age']>19)|(data['age']<7)) &(data['rez_esc'].isnull()), 'rez_esc']=0
x_train = []
x_test = []
y_train = []
df_test = pd.read_csv('.. /input/plant-seedlings-classification/sample_submission.csv')
 label_map = { "Black-grass" :0,
"Charlock" :1,
"Cleavers" :2,
"Common Chickweed" :3,
"Common wheat" :4,
"Fat Hen" :5,
"Loose Silky-bent" :6,
"Maize" :7,
"Scentless Mayweed" :8,
"Shepherds Purse" :9,
"Small-flowered Cranesbill" :10,
"Sugar beet" :11} 
<prepare_x_and_y>
data['rez_esc-missing'] = data['rez_esc'].isnull()
dim = 256
dirs = os.listdir(".. /input/plant-seedlings-classification/train/")
 for k in tqdm(range(len(dirs))):
files = os.listdir(".. /input/plant-seedlings-classification/train/{}".format(dirs[k]))
for f in range(len(files)) :
img = cv2.imread('.. /input/plant-seedlings-classification/train/{}/{}'.format(dirs[k], files[f]))
targets = np.zeros(12)
 targets[label_map[dirs[k]]] = 1
x_train.append(cv2.resize(img,(dim, dim)))
 y_train.append(targets)
 y_train = np.array(y_train, np.uint8)
 x_train = np.array(x_train, np.float32)
 print(x_train.shape)
 print(y_train.shape)
<split>
data['rez_esc']=data['rez_esc'].fillna(0 )
sss = StratifiedShuffleSplit(n_splits=1, test_size=0.16, random_state=42)
 for train_index, test_index in sss.split(x_train, y_train):
print("Using {} for training and {} for validation".format(len(train_index), len(test_index)))
 x_train, x_valid = x_train[train_index], x_train[test_index]
y_train, y_valid = y_train[train_index], y_train[test_index] 
<choose_model_class>
data.loc[data['rez_esc'] > 5, 'rez_esc'] = 5
datagen = ImageDataGenerator(horizontal_flip=True,
vertical_flip=True)
 weights = os.path.join('', 'weights.h5')
 epochs = 100
learning_rate = 0.0001
batch_size = 32
callbacks = [ EarlyStopping(monitor='val_loss', patience=5, verbose=0),
ModelCheckpoint(weights, monitor='val_loss', save_best_only=True, verbose=0),
ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=2, verbose=0, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0)] 
<choose_model_class>
id_ = ['Id', 'idhogar', 'Target']
base_model = Xception(input_shape=(dim, dim, 3), include_top=False, weights='imagenet', pooling='avg')
 x = base_model.output
x = Dense(256, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 predictions = Dense(12, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 print(model.summary())
<choose_model_class>
ind_bool = ['v18q', 'dis', 'male', 'female', 'estadocivil1', 'estadocivil2', 'estadocivil3',
'estadocivil4', 'estadocivil5', 'estadocivil6', 'estadocivil7',
'parentesco1', 'parentesco2', 'parentesco3', 'parentesco4', 'parentesco5',
'parentesco6', 'parentesco7', 'parentesco8', 'parentesco9', 'parentesco10',
'parentesco11', 'parentesco12', 'instlevel1', 'instlevel2', 'instlevel3',
'instlevel4', 'instlevel5', 'instlevel6', 'instlevel7', 'instlevel8',
'instlevel9', 'mobilephone', 'rez_esc-missing']
ind_ordered = ['rez_esc', 'escolari', 'age']
model.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam(lr=learning_rate), metrics=['accuracy'])
<train_model>
hh_bool = ['hacdor', 'hacapo', 'v14a', 'refrig', 'paredblolad', 'paredzocalo',
'paredpreb','pisocemento', 'pareddes', 'paredmad',
'paredzinc', 'paredfibras', 'paredother', 'pisomoscer', 'pisoother',
'pisonatur', 'pisonotiene', 'pisomadera',
'techozinc', 'techoentrepiso', 'techocane', 'techootro', 'cielorazo',
'abastaguadentro', 'abastaguafuera', 'abastaguano',
'public', 'planpri', 'noelec', 'coopele', 'sanitario1',
'sanitario2', 'sanitario3', 'sanitario5', 'sanitario6',
'energcocinar1', 'energcocinar2', 'energcocinar3', 'energcocinar4',
'elimbasu1', 'elimbasu2', 'elimbasu3', 'elimbasu4',
'elimbasu5', 'elimbasu6', 'epared1', 'epared2', 'epared3',
'etecho1', 'etecho2', 'etecho3', 'eviv1', 'eviv2', 'eviv3',
'tipovivi1', 'tipovivi2', 'tipovivi3', 'tipovivi4', 'tipovivi5',
'computer', 'television', 'lugar1', 'lugar2', 'lugar3',
'lugar4', 'lugar5', 'lugar6', 'area1', 'area2', 'v2a1-missing']
hh_ordered = [ 'rooms', 'r4h1', 'r4h2', 'r4h3', 'r4m1','r4m2','r4m3', 'r4t1', 'r4t2',
'r4t3', 'v18q1', 'tamhog','tamviv','hhsize','hogar_nin',
'hogar_adul','hogar_mayor','hogar_total', 'bedrooms', 'qmobilephone']
hh_cont = ['v2a1', 'dependency', 'edjefe', 'edjefa', 'meaneduc', 'overcrowding']
model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=len(x_train)/batch_size,
validation_data=datagen.flow(x_valid, y_valid, batch_size=batch_size),
validation_steps=len(x_valid)/batch_size,
callbacks=callbacks,
epochs=10,
verbose=1 )<train_model>
sqr_ = ['SQBescolari', 'SQBage', 'SQBhogar_total', 'SQBedjefe',
'SQBhogar_nin', 'SQBovercrowding', 'SQBdependency', 'SQBmeaned', 'agesq']
for f, species in tqdm(df_test.values, miniters=100):
img = cv2.imread('.. /input/plant-seedlings-classification/test/{}'.format(f))
x_test.append(cv2.resize(img,(dim, dim)))
 x_test = np.array(x_test, np.float32)
 print(x_test.shape )<load_pretrained>
heads = data.loc[data['parentesco1'] == 1, :]
heads = heads[id_ + hh_bool + hh_cont + hh_ordered]
heads.shape
if os.path.isfile(weights):
model.load_weights(weights )<predict_on_test>
elec = []
for i, row in heads.iterrows() :
if row['noelec'] == 1:
elec.append(0)
 elif row['coopele'] == 1:
elec.append(1)
 elif row['public'] == 1:
elec.append(2)
 elif row['planpri'] == 1:
elec.append(3)
 else:
elec.append(np.nan)
 heads['elec'] = elec
heads['elec-missing'] = heads['elec'].isnull()
p_test = model.predict(x_test, verbose=1)
 preds = []
for i in range(len(p_test)) :
pos = np.argmax(p_test[i])
 preds.append(list(label_map.keys())[list(label_map.values() ).index(pos)])
<save_to_csv>
heads = heads.drop(columns = 'area2')
 heads.groupby('area1')['Target'].value_counts(normalize = True )
df_test['species'] = preds
df_test.to_csv('submission.csv', index=False )<import_modules>
heads['roof'] = np.argmax(np.array(heads[['etecho1', 'etecho2', 'etecho3']]),
axis = 1)
 heads['floor'] = np.argmax(np.array(heads[['eviv1', 'eviv2', 'eviv3']]),
axis = 1)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import tensorflow as tf
import os
from IPython.display import clear_output<define_variables>
heads['walls+roof+floor'] = heads['walls'] + heads['roof'] + heads['floor']
CATEGORIES = os.listdir("/kaggle/input/plant-seedlings-classification/train")
 CATEGORIES<prepare_x_and_y>
heads['warning'] = 1 *(heads['sanitario1'] + 
(heads['elec'] == 0)+
heads['pisonotiene'] +
heads['abastaguano'] + 
(heads['cielorazo'] == 0))
X=[]
Y=[]
IMG_S1,IMG_S2 = 140,140
def createTrainingData() :

 a=0
pathh = "/kaggle/input/plant-seedlings-classification/"
types = ["train"]
for typ in types:
datadir = os.path.join(pathh,typ)
 for ele in CATEGORIES:
PATH = os.path.join(datadir,ele)
 class_num = CATEGORIES.index(ele)
 for img in os.listdir(PATH):
image = os.path.join(PATH, img)
 image = cv2.imread(image, cv2.IMREAD_ANYCOLOR)
 image = cv2.resize(image ,(IMG_S1, IMG_S2))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 X.append(image)
 X.append(cv2.flip(image,-1))
Y.append(class_num)
 Y.append(class_num)
 a+=1
clear_output()
print(a)
<train_model>
heads['bonus'] = 1 *(heads['refrig'] +
heads['computer'] + 
(heads['v18q1'] > 0)+
heads['television'])
createTrainingData()<categorify>
heads['phones-per-capita'] = heads['qmobilephone'] / heads['tamviv']
heads['tablets-per-capita'] = heads['v18q1'] / heads['tamviv']
heads['rooms-per-capita'] = heads['rooms'] / heads['tamviv']
heads['rent-per-capita'] = heads['v2a1'] / heads['tamviv']
def one_hottie(labels,C):

 One_hot_matrix = tf.one_hot(labels,C)
 return tf.keras.backend.eval(One_hot_matrix)
 Y = one_hottie(Y, 12)
 print("Y shape: " + str(Y.shape))<split>
heads.loc[(heads.v14a == 1)&(heads.sanitario1 == 1)&(heads.abastaguano == 0), "v14a"] = 0
heads.loc[(heads.v14a == 1)&(heads.sanitario1 == 1)&(heads.abastaguano == 0), "sanitario1"] = 0
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.20, random_state = 8)
 del X
del Y<choose_model_class>
range_ = lambda x: x.max() - x.min()
range_.__name__ = 'range_'
ind_agg = ind.drop(columns = 'Target' ).groupby('idhogar' ).agg(['min', 'max', 'sum', 'count', 'std', range_])
 ind_agg.head()
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(64, 1, activation=None,kernel_regularizer=tf.keras.regularizers.l2(0.1), input_shape=(140,140,3)) ,
tf.keras.layers.BatchNormalization(axis=3),
tf.keras.layers.LeakyReLU(0.1),
tf.keras.layers.MaxPool2D(strides=2),
tf.keras.layers.Conv2D(128, 3, activation=None,padding="same",kernel_regularizer=tf.keras.regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization(axis=3),
tf.keras.layers.LeakyReLU(0.1),
tf.keras.layers.MaxPool2D(strides=2),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Conv2D(256, 5, activation=None,kernel_regularizer=tf.keras.regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization(axis=3),
tf.keras.layers.LeakyReLU(0.1),
tf.keras.layers.MaxPool2D(strides=2),
tf.keras.layers.Conv2D(64, 5, activation=None,padding="same",kernel_regularizer=tf.keras.regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization(axis=3),
tf.keras.layers.LeakyReLU(0.1),
tf.keras.layers.MaxPool2D(strides=2),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(100,kernel_regularizer=tf.keras.regularizers.l2(0.01), activation=None),
tf.keras.layers.BatchNormalization(axis=1),
tf.keras.layers.ReLU() ,
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(50,kernel_regularizer=tf.keras.regularizers.l2(0.01), activation=None),
tf.keras.layers.BatchNormalization(axis=1),
tf.keras.layers.ReLU() ,
tf.keras.layers.Dense(12, kernel_regularizer=tf.keras.regularizers.l2(0.01),activation='softmax')
 ])
<init_hyperparams>
new_col = []
for c in ind_agg.columns.levels[0]:
for stat in ind_agg.columns.levels[1]:
new_col.append(f'{c}-{stat}')
 ind_agg.columns = new_col
ind_agg.head()
initial_learning_rate = 0.001
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
 initial_learning_rate,
decay_steps=800,
decay_rate=0.5,
staircase=True )<choose_model_class>
ind_feats = list(ind_agg.columns)
 final = heads.merge(ind_agg, on = 'idhogar', how = 'left')
 print('Final features shape: ', final.shape )
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule),loss=tf.keras.losses.CategoricalCrossentropy() ,
metrics=['accuracy'] )<train_model>
scorer = make_scorer(f1_score, greater_is_better=True, average = 'macro' )
result = model.fit(x=X_train,y=Y_train,batch_size=64,epochs=120,verbose=1,shuffle=False,initial_epoch=0,
validation_split=0.2 )<compute_test_metric>
train_labels = np.array(list(final[final['Target'].notnull() ]['Target'].astype(np.uint8)))
 train_set = final[final['Target'].notnull() ].drop(columns = ['Id', 'Target'])
 test_set = final[final['Target'].isnull() ].drop(columns = ['Id', 'Target'])
 submission_base = test[['Id', 'idhogar']].copy()
valid = model.evaluate(X_test,Y_test,verbose=2 )<define_variables>
train_set['adult'] = train_set['hogar_adul'] - train_set['hogar_mayor']
train_set['dependency_count'] = train_set['hogar_nin'] + train_set['hogar_mayor']
train_set['dependency'] = train_set['dependency_count'] / train_set['adult']
train_set['child_percent'] = train_set['hogar_nin']/train_set['hogar_total']
train_set['elder_percent'] = train_set['hogar_mayor']/train_set['hogar_total']
train_set['adult_percent'] = train_set['hogar_adul']/train_set['hogar_total']
test_set['adult'] = test_set['hogar_adul'] - test_set['hogar_mayor']
test_set['dependency_count'] = test_set['hogar_nin'] + test_set['hogar_mayor']
test_set['dependency'] = test_set['dependency_count'] / test_set['adult']
test_set['child_percent'] = test_set['hogar_nin']/test_set['hogar_total']
test_set['elder_percent'] = test_set['hogar_mayor']/test_set['hogar_total']
test_set['adult_percent'] = test_set['hogar_adul']/test_set['hogar_total']
train_set['rent_per_adult'] = train_set['v2a1']/train_set['hogar_adul']
train_set['rent_per_person'] = train_set['v2a1']/train_set['hhsize']
test_set['rent_per_adult'] = test_set['v2a1']/test_set['hogar_adul']
test_set['rent_per_person'] = test_set['v2a1']/test_set['hhsize']
train_set['overcrowding_room_and_bedroom'] =(train_set['hacdor'] + train_set['hacapo'])/2
test_set['overcrowding_room_and_bedroom'] =(test_set['hacdor'] + test_set['hacapo'])/2
Records = []<concatenate>
train_set['r4h1_percent_in_male'] = train_set['r4h1'] / train_set['r4h3']
train_set['r4m1_percent_in_female'] = train_set['r4m1'] / train_set['r4m3']
train_set['r4h1_percent_in_total'] = train_set['r4h1'] / train_set['hhsize']
train_set['r4m1_percent_in_total'] = train_set['r4m1'] / train_set['hhsize']
train_set['r4t1_percent_in_total'] = train_set['r4t1'] / train_set['hhsize']
test_set['r4h1_percent_in_male'] = test_set['r4h1'] / test_set['r4h3']
test_set['r4m1_percent_in_female'] = test_set['r4m1'] / test_set['r4m3']
test_set['r4h1_percent_in_total'] = test_set['r4h1'] / test_set['hhsize']
test_set['r4m1_percent_in_total'] = test_set['r4m1'] / test_set['hhsize']
test_set['r4t1_percent_in_total'] = test_set['r4t1'] / test_set['hhsize']
Records.append(valid )<predict_on_test>
train_set['rent_per_room'] = train_set['v2a1']/train_set['rooms']
train_set['bedroom_per_room'] = train_set['bedrooms']/train_set['rooms']
train_set['elder_per_room'] = train_set['hogar_mayor']/train_set['rooms']
train_set['adults_per_room'] = train_set['adult']/train_set['rooms']
train_set['child_per_room'] = train_set['hogar_nin']/train_set['rooms']
train_set['male_per_room'] = train_set['r4h3']/train_set['rooms']
train_set['female_per_room'] = train_set['r4m3']/train_set['rooms']
train_set['room_per_person_household'] = train_set['hhsize']/train_set['rooms']
test_set['rent_per_room'] = test_set['v2a1']/test_set['rooms']
test_set['bedroom_per_room'] = test_set['bedrooms']/test_set['rooms']
test_set['elder_per_room'] = test_set['hogar_mayor']/test_set['rooms']
test_set['adults_per_room'] = test_set['adult']/test_set['rooms']
test_set['child_per_room'] = test_set['hogar_nin']/test_set['rooms']
test_set['male_per_room'] = test_set['r4h3']/test_set['rooms']
test_set['female_per_room'] = test_set['r4m3']/test_set['rooms']
test_set['room_per_person_household'] = test_set['hhsize']/test_set['rooms']
train_set['rent_per_bedroom'] = train_set['v2a1']/train_set['bedrooms']
train_set['edler_per_bedroom'] = train_set['hogar_mayor']/train_set['bedrooms']
train_set['adults_per_bedroom'] = train_set['adult']/train_set['bedrooms']
train_set['child_per_bedroom'] = train_set['hogar_nin']/train_set['bedrooms']
train_set['male_per_bedroom'] = train_set['r4h3']/train_set['bedrooms']
train_set['female_per_bedroom'] = train_set['r4m3']/train_set['bedrooms']
train_set['bedrooms_per_person_household'] = train_set['hhsize']/train_set['bedrooms']
test_set['rent_per_bedroom'] = test_set['v2a1']/test_set['bedrooms']
test_set['edler_per_bedroom'] = test_set['hogar_mayor']/test_set['bedrooms']
test_set['adults_per_bedroom'] = test_set['adult']/test_set['bedrooms']
test_set['child_per_bedroom'] = test_set['hogar_nin']/test_set['bedrooms']
test_set['male_per_bedroom'] = test_set['r4h3']/test_set['bedrooms']
test_set['female_per_bedroom'] = test_set['r4m3']/test_set['bedrooms']
test_set['bedrooms_per_person_household'] = test_set['hhsize']/test_set['bedrooms']
train_set['tablet_per_person_household'] = train_set['v18q1']/train_set['hhsize']
train_set['phone_per_person_household'] = train_set['qmobilephone']/train_set['hhsize']
test_set['tablet_per_person_household'] = test_set['v18q1']/test_set['hhsize']
test_set['phone_per_person_household'] = test_set['qmobilephone']/test_set['hhsize']
train_set['age_12_19'] = train_set['hogar_nin'] - train_set['r4t1']
test_set['age_12_19'] = test_set['hogar_nin'] - test_set['r4t1']
species = model.predict_classes(X)
 print(species.shape)
 print(species )<create_dataframe>
train_set['num_over_18'] = 0
train_set['num_over_18'] = train_set[train.age >= 18].groupby('idhogar' ).transform("count")
 train_set['num_over_18'] = train_set.groupby("idhogar")["num_over_18"].transform("max")
 train_set['num_over_18'] = train_set['num_over_18'].fillna(0)
 test_set['num_over_18'] = 0
test_set['num_over_18'] = test_set[test.age >= 18].groupby('idhogar' ).transform("count")
 test_set['num_over_18'] = test_set.groupby("idhogar")["num_over_18"].transform("max")
 test_set['num_over_18'] = test_set['num_over_18'].fillna(0 )
ans = pd.DataFrame(file,columns = ["file"] )<create_dataframe>
train_set=train_set.drop(columns='idhogar')
 test_set=test_set.drop(columns='idhogar' )
ans = ans.join(pd.DataFrame(species,columns=["species"]))
ans["species"] = ans["species"].apply(lambda x: CATEGORIES[int(x)])
 ans.head(20 )<save_to_csv>
needless_cols = ['r4t3', 'tamhog', 'tamviv', 'hhsize', 'v14a']
train_set = train_set.drop(needless_cols, axis=1)
 test_set = test_set.drop(needless_cols, axis=1 )
ans.to_csv("answers.csv",index=False )<save_model>
train_set= train_set.fillna(0)
 test_set= test_set.fillna(0 )
model.save("saved_model" )<define_variables>
test_ids = list(final.loc[final['Target'].isnull() , 'idhogar'] )
ScaleTo = 70
seed = 7
path = '.. /input/plant-seedlings-classification/train/*/*.png'
files = glob(path)
 trainImg = []
trainLabel = []
j = 1
num = len(files)
 for img in files:
trainImg.append(cv2.resize(cv2.imread(img),(ScaleTo, ScaleTo)))
 trainLabel.append(img.split('/')[-2])
 j += 1
trainImg = np.asarray(trainImg)
 trainLabel = pd.DataFrame(trainLabel )<feature_engineering>
train_set = pd.DataFrame(train_set, columns = features)
 corr_matrix = train_set.corr()
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
to_drop = [column for column in upper.columns if any(abs(upper[column])> 1)]
to_drop
clearTrainImg = clearTrainImg / 255<categorify>
test_set = pd.DataFrame(test_set, columns = features)
 train_set, test_set = train_set.align(test_set, axis = 1, join = 'inner')
 features = list(train_set.columns )
le = preprocessing.LabelEncoder()
le.fit(trainLabel[0])
 print("Classes: " + str(le.classes_))
encodeTrainLabels = le.transform(trainLabel[0])
 clearTrainLabel = np_utils.to_categorical(encodeTrainLabels)
 num_clases = clearTrainLabel.shape[1]
print("Number of classes: " + str(num_clases))
trainLabel[0].value_counts().plot(kind='bar' )<split>
estimator = RandomForestClassifier(random_state = 10, n_estimators = 100, n_jobs = -1)
 selector = RFECV(estimator, step = 1, cv = 3, scoring= scorer, n_jobs = -1 )
trainX, testX, trainY, testY = train_test_split(clearTrainImg, clearTrainLabel,
test_size=0.1, random_state=seed,
stratify = clearTrainLabel )<train_model>
selector.fit(train_set, train_labels )
datagen = ImageDataGenerator(
 rotation_range=180,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=True 
)
 datagen.fit(trainX )<choose_model_class>
train_selected = selector.transform(train_set)
 test_selected = selector.transform(test_set )
numpy.random.seed(seed)
 model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(5, 5), input_shape=(ScaleTo, ScaleTo, 3), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(num_clases, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'] )<choose_model_class>
selected_features = train_set.columns[np.where(selector.ranking_==1)]
train_selected = pd.DataFrame(train_selected, columns = selected_features)
 test_selected = pd.DataFrame(test_selected, columns = selected_features )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.4,
min_lr=0.00001)
 filepath="drive/DataScience/PlantReco/weights.best_{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc',
verbose=1, save_best_only=True, mode='max')
 filepath="drive/DataScience/PlantReco/weights.last_auto4.hdf5"
checkpoint_all = ModelCheckpoint(filepath, monitor='val_acc',
verbose=1, save_best_only=False, mode='max')
 callbacks_list = [checkpoint, learning_rate_reduction] 
<train_model>
def macro_f1_score(labels, predictions):
predictions = predictions.reshape(len(np.unique(labels)) , -1 ).argmax(axis = 0)
 metric_value = f1_score(labels, predictions, average = 'macro')
 return 'macro_f1', metric_value, True
model.load_weights(".. /input/plantrecomodels/weights.best_17-0.96.hdf5")
 data = np.load(".. /input/plantrecomodels/Data.npz")
 d = dict(zip(( "trainX","testX","trainY", "testY"),(data[k] for k in data)))
 trainX = d['trainX']
testX = d['testX']
trainY = d['trainY']
testY = d['testY']
print(model.evaluate(trainX, trainY))
print(model.evaluate(testX, testY))<feature_engineering>
def learning_rate_power_0997(current_iter):
base_learning_rate = 0.1
min_learning_rate = 0.02
lr = base_learning_rate * np.power (.995, current_iter)
 return max(lr, min_learning_rate)
clearTestImg = clearTestImg / 255<save_to_csv>
def model_gbm(features, labels, test_features, test_ids,
nfolds = 5, return_preds = False, hyp = None,random_state_int=101,early_stopping=300):

 feature_names = list(features.columns)
 if hyp is not None:
if 'n_estimators' in hyp:
del hyp['n_estimators']
params = hyp
else:
params = {
'colsample_bytree': 0.88,
'learning_rate': 0.028,
'min_child_samples': 10,
'num_leaves': 36,
'subsample': 0.54,
'class_weight': 'balanced'}
model = lgb.LGBMClassifier(**params, objective = 'multiclass',
n_jobs = 4, n_estimators = 10000,
random_state = 10)
 strkfold = StratifiedKFold(n_splits = nfolds, shuffle = True, random_state= random_state_int)
 predictions = pd.DataFrame()
importances = np.zeros(len(feature_names))
features = np.array(features)
 test_features = np.array(test_features)
 labels = np.array(labels ).reshape(( -1))
valid_scores = []
for i,(train_indices, valid_indices)in enumerate(strkfold.split(features, labels)) :
fold_predictions = pd.DataFrame()
X_train = features[train_indices]
X_valid = features[valid_indices]
y_train = labels[train_indices]
y_valid = labels[valid_indices]
model.fit(X_train, y_train, early_stopping_rounds =early_stopping,
eval_metric = macro_f1_score,
eval_set = [(X_train, y_train),(X_valid, y_valid)],
eval_names = ['train', 'valid'],
callbacks=[lgb.reset_parameter(learning_rate=learning_rate_power_0997)])
 valid_scores.append(model.best_score_['valid']['macro_f1'])
 fold_probabilitites = model.predict_proba(test_features)
 for j in range(4):
fold_predictions[(j + 1)] = fold_probabilitites[:, j]
fold_predictions['idhogar'] = test_ids
fold_predictions['fold'] =(i+1)
 predictions = predictions.append(fold_predictions)
 importances += model.feature_importances_ / nfolds
display(f'Fold {i + 1}, Validation Score: {round(valid_scores[i], 5)}, Estimators Trained: {model.best_iteration_}')
 feature_importances = pd.DataFrame({'feature': feature_names,
'importance': importances})
 valid_scores = np.array(valid_scores)
 display(f'{nfolds} cross validation score: {round(valid_scores.mean() , 5)} with std: {round(valid_scores.std() , 5)}.')
 if return_preds:
predictions['Target'] = predictions[[1, 2, 3, 4]].idxmax(axis = 1)
 predictions['confidence'] = predictions[[1, 2, 3, 4]].max(axis = 1)
 return predictions, feature_importances
predictions = predictions.groupby('idhogar', as_index = False ).mean()
predictions['Target'] = predictions[[1, 2, 3, 4]].idxmax(axis = 1)
 predictions['confidence'] = predictions[[1, 2, 3, 4]].max(axis = 1)
 predictions = predictions.drop(columns = ['fold'])
 submission = submission_base.merge(predictions[['idhogar', 'Target']], on = 'idhogar', how = 'left' ).drop(columns = ['idhogar'])
 submission['Target'] = submission['Target'].fillna(4 ).astype(np.int8)
 return submission, feature_importances, valid_scores
pred = model.predict(clearTestImg)
 predNum = np.argmax(pred, axis=1)
 predStr = le.classes_[predNum]
res = {'file': testId, 'species': predStr}
res = pd.DataFrame(res)
 res.to_csv("res.csv", index=False )<import_modules>
hyp1 = {
'colsample_bytree': 0.88,
'learning_rate': 0.08,
'min_child_samples': 90, 'num_leaves': 34, 'subsample': 0.94, 'reg_lambda': 0.5,
'class_weight': 'balanced'}
hyp2 = {
'colsample_bytree': 0.88,
'learning_rate': 0.08,
'min_child_samples': 90, 'num_leaves': 14, 'subsample': 0.94, 'reg_lambda': 0.5,
'class_weight': 'balanced'}
hyp3 = {
'colsample_bytree': 0.78,
'learning_rate': 0.08,
'min_child_samples': 45, 'num_leaves': 14, 'subsample': 0.64, 'reg_lambda': 0.1,
'class_weight': 'balanced'}
hyp4 = {
'colsample_bytree': 0.72,
'learning_rate': 0.08,
'min_child_samples': 30, 'num_leaves': 18, 'subsample': 0.64, 'reg_lambda': 0.1,
'class_weight': 'balanced'}
scale = 70
seed = 7
subscale = 299<create_dataframe>
%%capture --no-display
submission1, gbm_fi_selected, valid_scores_selected = model_gbm(train_set, train_labels, test_set, test_ids,hyp = hyp1,random_state_int=103,early_stopping=300)
 submission2, gbm_fi_selected, valid_scores_selected = model_gbm(train_set, train_labels, test_set, test_ids,hyp = hyp1,random_state_int=103,early_stopping=300)
 submission3, gbm_fi_selected, valid_scores_selected = model_gbm(train_selected, train_labels, test_selected, test_ids)
subtrainingset = np.asarray(subtrainingset)
 subtraininglabels = pd.DataFrame(subtraininglabels )<categorify>
def model_gbm_2(features, labels, test_features, test_ids,
nfolds = 5, return_preds = False, hyp = None,random_state_int=101,early_stopping=300):

 feature_names = list(features.columns)
 if hyp is not None:
if 'n_estimators' in hyp:
del hyp['n_estimators']
params = hyp
else:
params = {
'colsample_bytree': 0.88,
'learning_rate': 0.028,
'min_child_samples': 10,
'num_leaves': 36,
'subsample': 0.54,
'class_weight': 'balanced'}
model = lgb.LGBMClassifier(**params, objective = 'multiclass',
n_jobs = 4, n_estimators = 10000,
random_state = 10)
 strkfold = StratifiedKFold(n_splits = nfolds, shuffle = True, random_state= random_state_int)
 predictions = pd.DataFrame()
importances = np.zeros(len(feature_names))
features = np.array(features)
 test_features = np.array(test_features)
 labels = np.array(labels ).reshape(( -1))
valid_scores = []
for i,(train_indices, valid_indices)in enumerate(strkfold.split(features, labels)) :
fold_predictions = pd.DataFrame()
X_train = features[train_indices]
X_valid = features[valid_indices]
y_train = labels[train_indices]
y_valid = labels[valid_indices]
model.fit(X_train, y_train, early_stopping_rounds =early_stopping,
eval_metric = macro_f1_score,
eval_set = [(X_train, y_train),(X_valid, y_valid)],
eval_names = ['train', 'valid'],
verbose = 200)
 valid_scores.append(model.best_score_['valid']['macro_f1'])
 fold_probabilitites = model.predict_proba(test_features)
 for j in range(4):
fold_predictions[(j + 1)] = fold_probabilitites[:, j]
fold_predictions['idhogar'] = test_ids
fold_predictions['fold'] =(i+1)
 predictions = predictions.append(fold_predictions)
 importances += model.feature_importances_ / nfolds
display(f'Fold {i + 1}, Validation Score: {round(valid_scores[i], 5)}, Estimators Trained: {model.best_iteration_}')
 feature_importances = pd.DataFrame({'feature': feature_names,
'importance': importances})
 valid_scores = np.array(valid_scores)
 display(f'{nfolds} cross validation score: {round(valid_scores.mean() , 5)} with std: {round(valid_scores.std() , 5)}.')
 if return_preds:
predictions['Target'] = predictions[[1, 2, 3, 4]].idxmax(axis = 1)
 predictions['confidence'] = predictions[[1, 2, 3, 4]].max(axis = 1)
 return predictions, feature_importances
predictions = predictions.groupby('idhogar', as_index = False ).mean()
predictions['Target'] = predictions[[1, 2, 3, 4]].idxmax(axis = 1)
 predictions['confidence'] = predictions[[1, 2, 3, 4]].max(axis = 1)
 predictions = predictions.drop(columns = ['fold'])
 submission = submission_base.merge(predictions[['idhogar', 'Target']], on = 'idhogar', how = 'left' ).drop(columns = ['idhogar'])
 submission['Target'] = submission['Target'].fillna(4 ).astype(np.int8)
 return submission, feature_importances, valid_scores
labels = preprocessing.LabelEncoder()
labels.fit(traininglabels[0])
 print('Classes'+str(labels.classes_))
encodedlabels = labels.transform(traininglabels[0])
 clearalllabels = np_utils.to_categorical(encodedlabels)
 classes = clearalllabels.shape[1]
print(str(classes))
traininglabels[0].value_counts().plot(kind='pie' )<split>
%%capture --no-display
submission4, gbm_fi_selected, valid_scores_selected = model_gbm_2(train_set, train_labels, test_set, test_ids,hyp = hyp3,random_state_int=103,early_stopping=300 )
new_train = new_train/255
x_train,x_test,y_train,y_test = train_test_split(new_train,clearalllabels,test_size=0.1,random_state=seed,stratify=clearalllabels )<train_model>
suball=submission1.merge(submission2, on='Id')
 suball=suball.merge(submission4, on='Id')
 suball=suball.merge(submission3, on='Id')
 suball_2=suball.merge(submission4, on='Id' )
generator = ImageDataGenerator(rotation_range = 180,zoom_range = 0.1,width_shift_range = 0.1,height_shift_range = 0.1,horizontal_flip = True,vertical_flip = True)
 generator.fit(x_train )<choose_model_class>
suball_2['index']=suball_2.index
np.random.seed(seed)
 model = Sequential()
model.add(Conv2D(filters=64, kernel_size=(5, 5), input_shape=(scale, scale, 3), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=128, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(BatchNormalization(axis=3))
model.add(Conv2D(filters=256, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(BatchNormalization(axis=3))
model.add(Dropout(0.1))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model.summary() 
<predict_on_test>
modle_13= pd.DataFrame(suball_2.mode(axis=1)[0] )
y_pred = model.predict(x_test)
 y_class = np.argmax(y_pred, axis = 1)
 y_check = np.argmax(y_test, axis = 1)
 cmatrix = confusion_matrix(y_check, y_class)
 print(cmatrix )<define_search_space>
modle_13['index']=modle_13.index
new_sub_train = []
sets = []; getEx = True
for i in subtrainingset:
blurr = cv2.GaussianBlur(i,(5,5),0)
 hsv = cv2.cvtColor(blurr,cv2.COLOR_BGR2HSV)
 lower =(25,40,50)
 upper =(75,255,255)
 mask = cv2.inRange(hsv,lower,upper)
 struc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(21,21))
mask = cv2.morphologyEx(mask,cv2.MORPH_CLOSE,struc)
 boolean = mask>0
new = np.zeros_like(i,np.uint8)
 new[boolean] = i[boolean]
new_sub_train.append(new)
 if getEx:
plt.subplot(2,3,1);plt.imshow(i)
 plt.subplot(2,3,2);plt.imshow(blurr)
 plt.subplot(2,3,3);plt.imshow(hsv)
 plt.subplot(2,3,4);plt.imshow(mask)
 plt.subplot(2,3,5);plt.imshow(boolean)
 plt.subplot(2,3,6);plt.imshow(new)
 plt.show()
getEx = False
new_sub_train = np.asarray(new_sub_train)
 print(len(new_sub_train))<categorify>
final = suball_2.merge(modle_13, on ='index' )
sublabels = preprocessing.LabelEncoder()
sublabels.fit(subtraininglabels[0])
 print('Classes'+str(sublabels.classes_))
subencodedlabels = sublabels.transform(subtraininglabels[0])
 clearalllabels = np_utils.to_categorical(subencodedlabels)
 subclasses = clearalllabels.shape[1]
print(str(subclasses))
subtraininglabels[0].value_counts().plot(kind='pie' )<split>
final_res=final[['Id',0]]
new_sub_train = new_sub_train/255
x_train,x_test,y_train,y_test = train_test_split(new_sub_train,clearalllabels,test_size=0.2,random_state=seed,stratify=clearalllabels )<train_model>
final_res.columns=['Id','Target']
<choose_model_class><EOS> 
final_res.to_csv('4_model_vote.csv',index=False )