Kaggle competition: Santander Customer Transaction Prediction
model = train_model()
opt = keras.optimizers.RMSprop(lr=0.0005, decay=1e-5)
 model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
 cbs = [ReduceLROnPlateau(monitor='loss', factor=0.5, patience=1, min_lr=1e-5, verbose=1)]
model.fit_generator(trainGen, steps_per_epoch=4922, epochs=4, validation_data=valGen,
validation_steps=493, shuffle=True, callbacks=cbs )<save_to_csv>
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv" )
test_set = pd.read_csv('.. /input/sample_submission.csv')
 pred = np.empty(( test_set.shape[0],))
for n in tqdm(range(test_set.shape[0])) :
data = np.array(Image.open('.. /input/test/test/'+test_set.id[n]))
data = data.astype(np.float32)/ 255.
 pred[n] = model.predict(data.reshape(( 1, 32, 32, 3)))[0][1]
test_set['has_cactus'] = pred
test_set.to_csv('sample_submission.csv', index=False )<load_from_csv>
train.pop("ID_code")
 test.pop("ID_code" )
def load_data(dataframe=None, batch_size=16, mode='categorical'):
if dataframe is None:
dataframe = pd.read_csv('.. /input/train.csv')
 dataframe['has_cactus'] = dataframe['has_cactus'].apply(str)
 gen = ImageDataGenerator(rescale=1./255., validation_split=0.1, horizontal_flip=True, vertical_flip=True)
 trainGen = gen.flow_from_dataframe(dataframe, directory='.. /input/train/train', x_col='id', y_col='has_cactus', has_ext=True, target_size=(32, 32),
class_mode=mode, batch_size=batch_size, shuffle=True, subset='training')
 testGen = gen.flow_from_dataframe(dataframe, directory='.. /input/train/train', x_col='id', y_col='has_cactus', has_ext=True, target_size=(32, 32),
class_mode=mode, batch_size=batch_size, shuffle=True, subset='validation')
 return trainGen, testGen<choose_model_class>
train["target"].value_counts()
def baseline_model() :
model = Sequential()
model.add(Conv2D(32,(3, 3), input_shape=(32, 32, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(32,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(64,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Conv2D(128,(3, 3), padding='same', use_bias=False, kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPool2D())
 model.add(GlobalAveragePooling2D())
 model.add(Dense(2, activation='softmax'))
return model<train_model>
y=train["target"]
def train_baseline() :
batch_size = 32
trainGen, valGen = load_data(batch_size=batch_size)
 model = baseline_model()
opt = Adam(1e-3)
 model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
 cbs = [ReduceLROnPlateau(monitor='loss', factor=0.5, patience=1, min_lr=1e-5, verbose=1)]
model.fit_generator(trainGen, steps_per_epoch=4922, epochs=3, validation_data=valGen,
validation_steps=493, shuffle=True, callbacks=cbs)
 return model
def predict_baseline(model):
testdf = pd.read_csv('.. /input/sample_submission.csv')
 pred = np.empty(( testdf.shape[0],))
for n in tqdm(range(testdf.shape[0])) :
data = np.array(Image.open('.. /input/test/test/'+testdf.id[n]))
data = data.astype(np.float32)/ 255.
 pred[n] = model.predict(data.reshape(( 1, 32, 32, 3)))[0][1]
testdf['has_cactus'] = pred
testdf.to_csv('sample_submission.csv', index=False )<predict_on_test>
train.pop("target" )
model = train_baseline()
predict_baseline(model )<set_options>
from sklearn.model_selection import StratifiedKFold,KFold
import lightgbm as lgb
warnings.filterwarnings("ignore" )<load_from_csv>
n_fold = 5
folds = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=42 )
original_train_data = pd.read_csv('.. /input/train.csv', nrows=6000000)
 train_data = original_train_data.sample(n=100000)
 train_data.info()<load_from_csv>
params = {'num_leaves': 8,
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'objective': 'binary',
'max_depth': 16,
'num_leaves': 13,
'learning_rate': 0.0085,
'boosting': 'gbdt',
'bagging_freq': 5,
'bagging_fraction': 0.38,
'feature_fraction': 0.04,
'bagging_seed': 11,
'reg_alpha': 0.1302650970728192,
'reg_lambda': 0.3603427518866501,
'random_state': 42,
'metric': 'auc',
'verbosity': -1,
'subsample': 0.81,
'min_gain_to_split': 0.01077313523861969,
'min_child_weight': 19.428902804238373,
'num_threads': 8}
test_data = pd.read_csv('.. /input/test.csv')
 test_data.info()<count_missing_values>
prediction = np.zeros(len(test))
for fold_n,(train_index, valid_index)in enumerate(folds.split(train,y)) :
print('Fold', fold_n)
 X_train, X_valid = train.iloc[train_index], train.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_data = lgb.Dataset(X_train, label=y_train)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 model = lgb.train(params,train_data,num_boost_round=20000,
valid_sets = [train_data, valid_data],verbose_eval=300,early_stopping_rounds = 200)
 prediction += model.predict(test, num_iteration=model.best_iteration)/5
train_data.isnull().sum()<drop_column>
sub1=pd.read_csv(".. /input/sample_submission.csv" )
train_data.dropna(axis=0,inplace=True)
<filter>
sub1["target"]=prediction
<train_model><EOS> 
sub1.to_csv("submissionlgb.csv",index=False )