Kaggle competition: Don't Overfit! II
valid_generator=datagen.flow_from_dataframe(
 dataframe=traindf,
directory=".. /input/dog-breed-identification/train/",
x_col="id",
y_col="breed",
has_ext=False,
subset="validation",
batch_size=1,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(image_size, image_size))<create_dataframe>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train.shape
test_datagen=ImageDataGenerator(preprocessing_function=preprocess_input,rescale=1./255.)
test_generator=test_datagen.flow_from_dataframe(
 dataframe=testdf,
directory=".. /input/dog-breed-identification/test/",
x_col="id",
y_col=None,
has_ext=False,
batch_size=1,
seed=42,
shuffle=False,
class_mode=None,
target_size=(image_size, image_size))<train_model>
X = train.drop(['id', 'target'], axis=1 ).values
y = train['target']
STEP_SIZE_TRAIN=train_generator.n
STEP_SIZE_VALID=valid_generator.n
print(STEP_SIZE_TRAIN)
 print(STEP_SIZE_VALID)
 my_new_model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=5
)<prepare_output>
n_fold = 5
kf = KFold(n_splits=n_fold, shuffle=True, random_state=2)
 scores = []
for train_index, test_index in kf.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]
clf = linear_model.LogisticRegression(class_weight='balanced', penalty='l1', C=.1, solver='liblinear')
 model = BaggingClassifier(clf, n_estimators=300, max_samples=.8, max_features=.8, random_state=0)
 model.fit(X_train, y_train)
 y_pred = model.predict_proba(X_test)[:, 1]
scores.append(roc_auc_score(y_test, y_pred))
print(f'CV mean score: {np.mean(scores):.4f}' )
my_new_model.evaluate_generator(generator=valid_generator )<predict_on_test>
X_test = test.drop(['id'], axis=1 ).values
log_reg = linear_model.LogisticRegression(class_weight='balanced', penalty='l1', C=.1, solver='liblinear')
 model = BaggingClassifier(log_reg, n_estimators=300, max_samples=.8, max_features=.8, random_state=0)
 model.fit(X, y)
 y_pred = model.predict_proba(X_test)[:, 1]
test_generator.reset()
pred=my_new_model.predict_generator(test_generator,verbose=1 )<save_to_csv>
sub = pd.read_csv(".. /input/sample_submission.csv")
 sub['target'] = y_pred
labels =(train_generator.class_indices)
 labels = list(labels.keys())
 df = pd.DataFrame(data=pred,
columns=labels)
 columns = list(df)
 columns.sort()
df = df.reindex(columns=columns)
 filenames = testdf["id"]
df["id"] = filenames
cols = df.columns.tolist()
cols = cols[-1:] + cols[:-1]
df = df[cols]
df.head(5)
 df.to_csv("results_tl_01.csv",index=False )<load_from_csv>
sub.to_csv('submission.csv',index=False )