Kaggle competition: Santander Value Prediction Challenge
<SOS> metric: RMSLE Kaggle data source: santander-value-prediction-challenge<compute_train_metric>
import gc
import numpy as np
import pandas as pd
import lightgbm as lgbm
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
def test_naive_bayes(test_x, test_y, logprior, loglikelihood):

 accuracy = 0
y_hats = []
for tweet in test_x:
if naive_bayes_predict(tweet, logprior, loglikelihood)> 0:
y_hat_i = 1
else:
y_hat_i = 0
y_hats.append(y_hat_i)
 error = np.mean(np.abs(y_hats - test_y))
accuracy = 1 - error
return accuracy<prepare_x_and_y>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train['target'] = np.log1p(train['target'])
 test.insert(1,'target',-1.)
freqs = count_tweets({}, df_train['text'], df_train['target'])
 logprior, loglikelihood = train_naive_bayes(freqs, df_train['text'], df_train['target'])
 print(logprior)
 print(len(loglikelihood))<compute_test_metric>
floatcolumns = []
intcolumns = []
for c in train.columns[2:]:
s = train[c].dtype
if(s=='float64'):
floatcolumns.append(c)
 else:
intcolumns.append(c )
print("Naive Bayes accuracy:", test_naive_bayes(df_train['text'], df_train['target'], logprior, loglikelihood)* 100, "%" )<predict_on_test>
alldata = pd.concat([train,test])
 del train,test
gc.collect()
print('Truth Predicted Tweet')
 for x, y in zip(df_train['text'], df_train['target']):
y_hat = naive_bayes_predict(x, logprior, loglikelihood)
 if y !=(np.sign(y_hat)> 0):
print('%d\t%0.2f\t%s' %(y, np.sign(y_hat)> 0, ' '.join(
 process_tweet(x)).encode('ascii', 'ignore')) )<load_from_csv>
alldata['sumofzeros'] =(alldata[intcolumns]==0 ).sum(axis=1)
 train = alldata[alldata.target!=-1]
test = alldata[alldata.target==-1]
del alldata
gc.collect()
df_test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv" )<predict_on_test>
lgbm_params = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'binary',
'metric': 'binary_logloss',
"learning_rate": 0.01,
"num_leaves": 100,
"feature_fraction":.5,
"bagging_fraction":.5,
"max_depth": -1,
"reg_alpha": 0.3,
"reg_lambda": 0.1,
"min_child_weight":10,
'zero_as_missing':True
}
df_test['target'] = df_test['text'].apply(lambda tweet : int(np.sign(naive_bayes_predict(tweet, logprior, loglikelihood)) > 0))<save_to_csv>
bestparams = {0:(387, 0.32549223081879874),
1:(363, 0.48964347828583543),
2:(389, 0.5646998803516646),
3:(412, 0.5738770523457457),
4:(371, 0.5016286734255448),
5:(433, 0.3353076747094824)}
submission = df_test[['id', 'target']]
submission.to_csv('nb_submission.csv', index=False )<load_from_url>
folds = KFold(n_splits=5, shuffle=True, random_state=42)
 oof_preds = np.zeros(( train.shape[0],6))
sub_preds = np.zeros(( test.shape[0],6))
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
for i in [0, 1, 2, 3, 4, 5]:
optimal_rounds, best_cv_score = bestparams[i]
print(i, optimal_rounds, best_cv_score)
 for n_fold,(trn_idx, val_idx)in enumerate(folds.split(train)) :
print(n_fold)
 trn_x, trn_y = train[feats].iloc[trn_idx], x[trn_idx]>i
val_x, val_y = train[feats].iloc[val_idx], x[val_idx]>i
clf = lgbm.train(lgbm_params,
lgbm.Dataset(trn_x,trn_y),
num_boost_round = optimal_rounds + 1,
verbose_eval=200)
 oof_preds[val_idx,i] = clf.predict(val_x, num_iteration=optimal_rounds + 1)
 sub_preds[:,i] += clf.predict(test[feats], num_iteration=optimal_rounds + 1)/ folds.n_splits
del clf
del trn_x, trn_y, val_x, val_y
gc.collect()
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import ModelCheckpoint
import tensorflow_hub as hub
import tokenization<choose_model_class>
off_preds_withbias = np.hstack([oof_preds,np.ones(shape=(oof_preds.shape[0],1)) ])
 sub_preds_withbias = np.hstack([sub_preds,np.ones(shape=(sub_preds.shape[0],1)) ] )
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )<feature_engineering>
params = np.linalg.lstsq(off_preds_withbias, train.target)[0]
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )<drop_column>
trainpreds = np.dot(off_preds_withbias,params)
 print(np.sqrt(mean_squared_error(train.target,trainpreds)) )
<categorify><EOS> 
testpreds = np.dot(sub_preds_withbias,params)
 sub = pd.DataFrame({'ID':test.ID, 'target':np.expm1(testpreds)})
 sub.to_csv('cut.csv',index=False )