Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
train_term_doc_tfidf = vec_tfidf.fit_transform(X_train)
 valid_term_doc_tfidf = vec_tfidf.transform(X_valid )<compute_test_metric>
PATH = ".. /input/"
TMP_PATH = "/tmp/tmp"
MODEL_PATH = "/tmp/model/"
sz=224
def pr(y_i, y, x):
p = x[y==y_i].sum(0)
 return(p+1)/(( y==y_i ).sum() +1 )<normalization>
fnames = np.array([f'train/{f}' for f in sorted(os.listdir(f'{PATH}train')) ])
 labels = np.array([(0 if 'cat' in fname else 1)for fname in fnames] )
r = np.squeeze(np.asarray(np.log(pr(1, y=y_train, x=train_term_doc_tfidf)/pr(0, y=y_train, x=train_term_doc_tfidf))))<define_variables>
from fastai.imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
preds =(valid_term_doc_tfidf @ r + b)> 0<compute_test_metric>
arch=resnet50
print(f'Validation accuracy: {(preds == y_valid ).mean() }')
 print(f'Validation F1 score: {f1_score(y_valid, preds)}' )<count_duplicates>
data = ImageClassifierData.from_names_and_array(
 path=PATH,
fnames=fnames,
y=labels,
classes=['dogs', 'cats'],
test_name='test',
tfms= tfms_from_model(resnet34, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
)
 learn = ConvLearner.pretrained(arch, data, precompute=True, tmp_name=TMP_PATH, models_name=MODEL_PATH )
nb_train = train_term_doc_tfidf.multiply(r)
 nb_valid = valid_term_doc_tfidf.multiply(r)
 nb_train.shape, nb_valid.shape<train_model>
learn.fit(0.01,4 )
model = LogisticRegression(C=4, solver='liblinear')
 model.fit(nb_train, y_train)
 preds = model.predict(nb_valid )<compute_test_metric>
learn.precompute=False
learn.fit(1e-2, 3, cycle_len=2 )
print(f'Validation accuracy: {(preds == y_valid ).mean() }')
 print(f'Validation F1 score: {f1_score(y_valid, preds)}' )<prepare_x_and_y>
lr=np.array([1e-4,1e-3,1e-2] )
train_term_doc_tfidf = vec_tfidf.fit_transform(train_df.text)
 test_term_doc_tfidf = vec_tfidf.transform(test_df.text)
 y_train = train_df.target.values<normalization>
learn.fit(lr, 3, cycle_len=1, cycle_mult=2 )
r = np.squeeze(np.asarray(np.log(pr(1, y=y_train, x=train_term_doc_tfidf)/pr(0, y=y_train, x=train_term_doc_tfidf))))<predict_on_test>
??learn.TTA
model = LogisticRegression(C=4, solver='liblinear')
 model.fit(train_term_doc_tfidf.multiply(r), y_train)
 preds = model.predict(test_term_doc_tfidf.multiply(r))<load_from_csv>
log_predictions,y = learn.TTA(is_test=True)
 prob_predictions = np.mean(np.exp(log_predictions),0)
 probs = prob_predictions[:,1]
submit = pd.read_csv(path/'sample_submission.csv' )<define_variables>
valid_preds= np.argmax(prob_predictions, axis=1 )
assert all(submit.id == test_df.id)
 assert len(submit)== len(test_df)== len(preds )<prepare_output>
ids= fnames = np.array([f'{f}' for f in os.listdir(f'{PATH}test')] )
submit.target = preds<save_to_csv>
ids= [i.replace(".jpg","")for i in ids]
ids[0]
submit.to_csv('submission_060320.csv', index=False )<import_modules>
ans= pd.DataFrame({"id":ids,"label":probs})
 ans= ans.sort_values('id')
 ans.head()
<define_variables><EOS> 
ans.to_csv('submission.csv', index=False )