Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<choose_model_class>
print('fast.ai version:{}'.format(fastai.__version__))
extraTrees = ExtraTreesClassifier(random_state=seed)
 gBoosting = GradientBoostingClassifier(random_state=seed)
 randomForest = RandomForestClassifier(random_state=seed)
 logReg = LogisticRegression(random_state=seed)
 xgbc = XGBClassifier(random_state=seed )<train_on_grid>
model = models.resnet152
WORK_DIR = os.getcwd()
IMAGE_DIR = Path('.. /input/')
 image_size=224
batch_size=32
param_grid = {"max_depth": [None],
"max_features": [1, 3, 10],
"min_samples_split": [2, 3, 10],
"min_samples_leaf": [1, 3, 10],
"bootstrap": [False],
"n_estimators" :[100,300],
"criterion": ["gini"]}
grid_result = GridSearchCV(extraTrees,
param_grid = param_grid,
cv=kfold,
scoring="accuracy",
n_jobs= -1,
verbose = 1)
 grid_result.fit(X_train,y_train)
 extraTrees_best_result = grid_result.best_estimator_
print('Best score:', np.round(grid_result.best_score_*100, 2))
print('Best estimator:', extraTrees_best_result )<train_on_grid>
fnames = get_image_files(IMAGE_DIR/'train')
 fnames[:5]
param_grid = {'learning_rate': [0.01, 0.02],
'max_depth': [4, 5, 6],
'max_features': [0.2, 0.3, 0.4],
'min_samples_split': [2, 3, 4],
'random_state':[seed]}
grid_result = GridSearchCV(gBoosting,
param_grid=param_grid,
cv=kfold,
scoring="accuracy",
n_jobs=-1,
verbose=1)
 grid_result.fit(X_train, y_train)
 gBoosting_best_result = grid_result.best_estimator_
print('Best score:', np.round(grid_result.best_score_*100, 2))
print('Best estimator:', gBoosting_best_result )<train_on_grid>
pattern_get_class = re.compile(r'/([^/]+)\.\d+\.jpg$')
 data = ImageDataBunch.from_name_re(path = IMAGE_DIR,
fnames = fnames,
pat = pattern_get_class,
ds_tfms=get_transforms() ,
test ='test',
size=image_size,
bs=batch_size,
num_workers=0 ).normalize(imagenet_stats)
 data
param_grid = {"max_depth": [None],
"max_features": [1, 2],
"min_samples_split": [2, 3, 10],
"min_samples_leaf": [1, 3, 10],
"bootstrap": [False],
"n_estimators" :[100,300],
"criterion": ["gini"]}
grid_result = GridSearchCV(randomForest,
param_grid=param_grid,
cv=kfold,
scoring="accuracy",
n_jobs= -1,
verbose = 1)
 grid_result.fit(X_train, y_train)
 randomForest_best_result = grid_result.best_estimator_
print('Best score:', np.round(grid_result.best_score_*100, 2))
print('Best estimator:', randomForest_best_result )<choose_model_class>
data.show_batch(rows=3 )
param_grid = {'penalty' : ['l1', 'l2'],
'C': np.logspace(0, 4, 10),
'solver' : ['liblinear', 'saga']
}
grid_result = GridSearchCV(logReg,
param_grid=param_grid,
cv=kfold,
scoring="accuracy",
n_jobs= -1,
verbose = 1)
 grid_result.fit(X_train, y_train)
 logReg_best_result = grid_result.best_estimator_
print('Best score:', np.round(grid_result.best_score_*100, 2))
print('Best estimator:', logReg_best_result )<define_search_space>
learn = create_cnn(data, model, metrics=accuracy, model_dir=WORK_DIR )
param_grid = {'n_estimators': [275, 280],
'learning_rate': [0.01, 0.03],
'subsample': [0.9, 1],
'max_depth': [3, 4],
'colsample_bytree': [0.8, 0.9],
'min_child_weight': [2, 3],
'random_state':[seed]}
grid_result = GridSearchCV(xgbc,
param_grid=param_grid,
cv=kfold,
scoring="accuracy",
n_jobs= -1,
verbose = 1)
 grid_result.fit(X_train, y_train)
 xgbc_best_result = grid_result.best_estimator_
print('Best score:', np.round(grid_result.best_score_*100, 2))
print('Best estimator:', xgbc_best_result )<predict_on_test>
show_doc(learn.fit_one_cycle )
survived_ET = pd.Series(extraTrees_best_result.predict(X_test), name="ET")
 survived_GB = pd.Series(gBoosting_best_result.predict(X_test), name="GB")
 survived_RF = pd.Series(randomForest_best_result.predict(X_test), name="RF")
 survived_LR = pd.Series(logReg_best_result.predict(X_test), name="LR")
 survived_XB = pd.Series(xgbc_best_result.predict(X_test), name="XB")
 ensemble_results = pd.concat([survived_ET,
survived_GB,
survived_RF,
survived_LR,
survived_XB],
axis=1)
 fig, ax = plt.subplots(figsize=(12,8))
g= sns.heatmap(ensemble_results.corr() ,
annot=True,
fmt = ".3f",
cmap = "Greens",
ax=ax )<choose_model_class>
learn.fit_one_cycle(1 )
voting = VotingClassifier(estimators=[('XB', xgbc_best_result), 
('GB', gBoosting_best_result), 
('RF', randomForest_best_result), 
('LR', logReg_best_result), 
('ET', extraTrees_best_result)],
voting='soft', n_jobs=-1)
 voting.fit(X_train, y_train )<compute_test_metric>
learn.save('stage-1' )
print("Score(Voting): " + str(voting.score(X_train, y_train)) )<predict_on_test>
learn.unfreeze()
learn.lr_find()
learn.recorder.plot()
y_predict = voting.predict(X_test )<save_to_csv>
learn.load('stage-1')
 learn.unfreeze()
learn.fit_one_cycle(4, max_lr=slice(1e-6,1e-4))
solution = pd.DataFrame({
"PassengerId": PassengerId,
"Survived": y_predict.astype(int)
 })
 solution.to_csv('solution_final_v1.csv', index=False)
 df_solution = pd.read_csv('solution_final_v1.csv' )<import_modules>
learn.save('stage-2' )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pathlib import Path
import os, re, string<import_modules>
interp = ClassificationInterpretation.from_learner(learn )
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score<load_from_csv>
if 'DatasetType' not in sys.modules:
train_df = pd.read_csv(path/'train.csv')
 test_df = pd.read_csv(path/'test.csv' )<train_model>
try:
preds, _ = learn.TTA(ds_type=DatasetType.Test)
 except:
preds, _ = learn.get_preds(DatasetType.Test)
 else:
print('Predict with TTA done.' )
print(f'Training set size: {len(train_df)}')
 print(f'Test set size: {len(test_df)}' )<count_missing_values>
print(data.classes)
 dict_label_order = {label:order for order,label in enumerate(data.classes)}
print(dict_label_order )
print(f'Missing values in training set: {train_df.text.isna().sum() }')
 print(f'Missing values in test set: {test_df.text.isna().sum() }' )<compute_test_metric>
n_dogs = dict_label_order['dog']
n_dogs
print(f'Average target in training set: {np.round(train_df.target.mean() ,2)}' )<filter>
prob_dogs = preds[:,n_dogs].numpy()
prob_dogs
diff = len(train_df[train_df.target==0])-len(train_df[train_df.target==1])
 pct_diff = np.round(diff/len(train_df),2)
 diff, pct_diff<string_transform>
ids = [int(file.stem)for file in data.test_ds.x.items]
ids[:10]
re_tok = re.compile(f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤€‘’])')
 def re_tokenizer(s): return re_tok.sub(r' \1 ', s ).lower().split()<string_transform>
submission = pd.DataFrame({'id':ids,'label':prob_dogs})
 submission = submission.sort_values(by=['id'] )
print(re_tokenizer(train_df.text[6]))<split>
submission.to_csv('submission.csv', index=False )
<train_model><EOS> 
submission.to_csv('submission.csv', index=False )