Kaggle competition: DL in NLP Spring 2019. Classification
<SOS> metric: meanfscore Kaggle data source: dl-in-nlp-spring-2019.-classification<string_transform>
fastai.__version__
stop_words = set(stopwords.words('english'))
stop_words<feature_engineering>
train = pd.read_csv('.. /input/train.csv' ).fillna(' ')
 valid = pd.read_csv('.. /input/valid.csv' ).fillna(' ')
 test = pd.read_csv('.. /input/test.csv' ).fillna(' ' )
tq = tqdm_notebook(total=len(clean_ingredients_list))
for i, ingredients in enumerate(clean_ingredients_list):
cleaned_ingredients = [c for c in ingredients.split(' ')if c not in stop_words]
cleaned_ingredients =(' '.join(cleaned_ingredients)).strip(' ')
 clean_ingredients_list[i] = cleaned_ingredients
tq.update(1)
 tq.close()
clean_unique_ingredients_list = [c for c in list(np.unique(clean_ingredients_list)) if len(c)>0]<count_unique_values>
pd.concat([train['text'], valid['text'], test['text']] ).to_csv(
 'unlabeled_news.csv', index=None, header=True )
len(clean_unique_ingredients_list )<feature_engineering>
pd.concat([train[['text', 'label']],valid[['text', 'label']]] ).to_csv(
 'train_28k.csv', index=None, header=True)
 test[['text']].to_csv('test_5k.csv', index=None, header=True )
ingredients_encoded_test = np.zeros(( df_test.shape[0], len(clean_unique_ingredients_list)+2), dtype=np.uint8)
 tq = tqdm_notebook(total=df_test.shape[0])
 for i in range(df_test.shape[0]):
ingredients_encoded_test[i,0] = df_test['id'].values[i]
ingredients_encoded_test[i,1] = 0
clean_recipe = [re.sub(r'['+chars+']', '',
re.sub('[0-9]+','', c.replace("oz","")) ).strip(' ' ).lower()
for c in df_test['ingredients'][i]]
for k, ingredients in enumerate(clean_recipe):
cleaned_ingredients = [c for c in ingredients.split(' ')if c not in stop_words]
cleaned_ingredients =(' '.join(cleaned_ingredients)).strip(' ')
 clean_recipe[k] = cleaned_ingredients
clean_recipe = [c for c in clean_recipe if len(c)>0]
for ingredient in clean_recipe:
if ingredient not in clean_unique_ingredients_list:
continue
ingredients_encoded_test[i,clean_unique_ingredients_list.index(ingredient)+2]= 1
tq.update(1)
 tq.close()<create_dataframe>
folder = '.'
unlabeled_file = 'unlabeled_news.csv'
data_test = pd.DataFrame(data=ingredients_encoded_test[:,1:])
 data_test<create_dataframe>
%%time
data_lm = TextLMDataBunch.from_csv(folder, unlabeled_file, text_cols='text' )
data = pd.DataFrame(data=ingredients_encoded[:,1:])
 data<groupby>
%%time
learn = language_model_learner(data_lm, drop_mult=0.3, arch=AWD_LSTM )
data_gb = data.groupby([0] ).sum()<filter>
%%time
learn.lr_find(start_lr = slice(10e-7, 10e-5), end_lr=slice(0.1, 10))
data_gb.sum(0)[data_gb.sum(0)<2]<filter>
best_lm_lr = learn.recorder.min_grad_lr
best_lm_lr
len(data_test.sum(0)[data_test.sum(0)<2] )<filter>
%%time
learn.fit_one_cycle(1, best_lm_lr )
data = data.loc[:,data.sum(0)>1]<import_modules>
%%time
learn.fit_one_cycle(1, best_lm_lr )
from sklearn.model_selection import train_test_split<import_modules>
learn.predict('An italian man was found dead in his yard due to', n_words=200 )
from sklearn.model_selection import train_test_split<split>
learn.save_encoder('clickbait_news_enc' )
y = data[data.columns[0]]
X = data[data.columns[1:]]
X_train, X_test, y_train, y_test = train_test_split(X, y, shuffle = True, stratify = y, test_size=0.2 )<feature_engineering>
train_file, test_file = 'train_28k.csv', 'test_5k.csv'
def prtime(start, stop):
temp = stop - start
hours = temp//3600
temp = temp - 3600*hours
minutes = temp//60
seconds = temp - 60*minutes
print('%d:%d:%d' %(hours,minutes,seconds))<init_hyperparams>
data_clas = TextClasDataBunch.from_csv(path=folder,
csv_name=train_file,
test=test_file,
vocab=data_lm.train_ds.vocab,
bs=64,
text_cols='text',
label_cols='label' )
start = timeit.default_timer()
rf_best = RandomForestClassifier(bootstrap=True,
class_weight='balanced',
criterion='gini',
max_depth=100,
max_features='auto',
max_leaf_nodes=None,
min_impurity_decrease=0.0,
min_impurity_split=None,
min_samples_leaf=1,
min_samples_split=2,
min_weight_fraction_leaf=0.0,
n_estimators=400,
n_jobs=8,
oob_score=False,
random_state=None, verbose=0, warm_start=False)
 stop = timeit.default_timer()
prtime(start, stop )<import_modules>
data_clas.save('ulmfit_data_clas_clickbait_news' )
from sklearn.metrics import accuracy_score 
<set_options>
learn_clas = text_classifier_learner(data_clas, drop_mult=0.3, arch=AWD_LSTM)
 learn_clas.load_encoder('clickbait_news_enc' )
start = timeit.default_timer()
clf_best = MLPClassifier(solver='adam',
activation='relu',
early_stopping=True,
random_state=3,
max_iter=500,
verbose=True,
alpha=1e-08,
beta_1=0.05,
beta_2=0.4,
epsilon=1e-09)
 stop = timeit.default_timer()
prtime(start, stop )<import_modules>
learn_clas.lr_find(start_lr=slice(10e-7, 10e-5), end_lr=slice(0.1, 10))
from sklearn.metrics import accuracy_score 
<train_model>
best_clf_lr = learn_clas.recorder.min_grad_lr
best_clf_lr
start = timeit.default_timer()
clf_best = LogisticRegression(penalty='l2',
fit_intercept = True,
class_weight = 'balanced',
solver = 'newton-cg',
multi_class = 'multinomial' 
)
 clf_best.fit(X, y)
 stop = timeit.default_timer()
prtime(start, stop )<predict_on_test>
learn_clas.fit_one_cycle(1, best_clf_lr )
y_pred = clf_best.predict(X_test)
 accuracy_score(y_test,y_pred )<split>
learn_clas.freeze_to(-2 )
X_sub = data_test[data_test.columns[1:]]
X_sub = X_sub.loc[:, X.columns]<predict_on_test>
learn_clas.fit_one_cycle(1, best_clf_lr )
y_sub = clf_best.predict(X_sub)
 y_pred_str = [list(classes_dict.keys())[list(classes_dict.values() ).index(c)] for c in y_sub]<create_dataframe>
learn_clas.unfreeze()
subm = pd.DataFrame({'id':df_test['id'], 'cuisine':y_pred_str} )<count_unique_values>
learn_clas.fit_one_cycle(1, best_clf_lr )
len(np.unique(subm['cuisine']))<save_to_csv>
data_clas.add_test(test["text"] )
subm.to_csv('submission.csv', index=False )<set_options>
test_preds, _ = learn_clas.get_preds(DatasetType.Test,
ordered=True )
tqdm.pandas()
init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore' )<define_variables>
test_pred_df = pd.DataFrame(test_preds.data.cpu().numpy() ,
columns=['clickbait', 'news', 'other'])
 ulmfit_preds = pd.Series(np.argmax(test_pred_df.values, axis=1),
name='label' ).map({0: 'clickbait', 1: 'news', 2: 'other'})
def random_colours(number_of_colors):

 colors = []
for i in range(number_of_colors):
colors.append("
return colors<load_from_disk>
ulmfit_preds.to_csv('ulmfit_predictions.csv', index_label='id', header=True )