Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<define_variables>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
stopwords = set(STOPWORDS)
<load_pretrained>
sample=pd.read_csv('.. /input/sampleSubmission.csv')
 train_df=pd.read_csv('.. /input/train.csv')
 test_df=pd.read_csv('.. /input/test.csv' )
nltk.download('punkt' )<install_modules>
train_df.nunique()
pip install category_encoders<categorify>
test_df.nunique()
features = ['keyword', 'location_clean']
encoder = ce.TargetEncoder(cols=features)
 encoder.fit(train[features],train['target'])
 train = train.join(encoder.transform(train[features] ).add_suffix('_target'))
test = test.join(encoder.transform(test[features] ).add_suffix('_target'))<prepare_x_and_y>
train_df.isnull().sum()
vec_links = CountVectorizer(min_df = 2, analyzer = 'word', token_pattern = r'https?://\S+')
 link_vec = vec_links.fit_transform(train['links'])
 link_vec_test = vec_links.transform(test['links'])
 X_train_link = pd.DataFrame(link_vec.toarray() , columns=vec_links.get_feature_names())
 X_test_link = pd.DataFrame(link_vec_test.toarray() , columns=vec_links.get_feature_names())
 vec_men = CountVectorizer(min_df = 2)
 men_vec = vec_men.fit_transform(train['mentions'])
 men_vec_test = vec_men.transform(test['mentions'])
 X_train_men = pd.DataFrame(men_vec.toarray() , columns=vec_men.get_feature_names())
 X_test_men = pd.DataFrame(men_vec_test.toarray() , columns=vec_men.get_feature_names())
 vec_hash = CountVectorizer(min_df = 2)
 hash_vec = vec_hash.fit_transform(train['hashtags'])
 hash_vec_test = vec_hash.transform(test['hashtags'])
 X_train_hash = pd.DataFrame(hash_vec.toarray() , columns=vec_hash.get_feature_names())
 X_test_hash = pd.DataFrame(hash_vec_test.toarray() , columns=vec_hash.get_feature_names())
 print(X_train_link.shape, X_train_men.shape, X_train_hash.shape )<sort_values>
season_df=train_df.groupby('season' )
hash_rank =(X_train_hash.transpose().dot(train['target'])/ X_train_hash.sum(axis=0)).sort_values(ascending=False)
 print('Hashtags with which 100% of Tweets are disasters: ')
 print(list(hash_rank[hash_rank==1].index))
print('Total: ' + str(len(hash_rank[hash_rank==1])))
 print('Hashtags with which 0% of Tweets are disasters: ')
 print(list(hash_rank[hash_rank==0].index))
print('Total: ' + str(len(hash_rank[hash_rank==0])) )<categorify>
cat_feat=['season','weather','holiday','workingday']
cont_feat=['temp','atemp','humidity','windspeed']
count_feats=['casual','registered','count']
vec_text = TfidfVectorizer(min_df = 8, ngram_range =(1,2), stop_words='english')
 text_vec = vec_text.fit_transform(train['text_clean'])
 text_vec_test = vec_text.transform(test['text_clean'])
 X_train_text = pd.DataFrame(text_vec.toarray() , columns=vec_text.get_feature_names())
 X_test_text = pd.DataFrame(text_vec_test.toarray() , columns=vec_text.get_feature_names())
 print(X_train_text.shape )<merge>
hour=[]
day=[]
month=[]
year=[]
for row in train_df['datetime']:
date_hour=row.split()
date=date_hour[0]
hour_row=date_hour[1]
hour.append(hour_row.split(':')[0])
 date=date.split('-')
 day.append(date[2])
 month.append(date[1])
 year.append(date[0] )
train = train.join(X_train_link, rsuffix='_link')
 train = train.join(X_train_men, rsuffix='_mention')
 train = train.join(X_train_hash, rsuffix='_hashtag')
 train = train.join(X_train_text, rsuffix='_text')
 test = test.join(X_test_link, rsuffix='_link')
 test = test.join(X_test_men, rsuffix='_mention')
 test = test.join(X_test_hash, rsuffix='_hashtag')
 test = test.join(X_test_text, rsuffix='_text')
 print(train.shape, test.shape )<define_variables>
train_df['hour']=hour
train_df['day']=day
train_df['month']=month
train_df['year']=year
text_clean_train=train['text_clean']
text_clean_test=test['text_clean']<normalization>
hour=[]
day=[]
month=[]
year=[]
for row in test_df['datetime']:
date_hour=row.split()
date=date_hour[0]
hour_row=date_hour[1]
hour.append(hour_row.split(':')[0])
 date=date.split('-')
 day.append(date[2])
 month.append(date[1])
 year.append(date[0])
 test_df['hour']=hour
test_df['day']=day
test_df['month']=month
test_df['year']=year
features_to_drop = ['id', 'keyword','location','text','location_clean','text_clean', 'hashtags', 'mentions','links']
scaler = MinMaxScaler()
Target = train.target
train = train.drop(columns = features_to_drop + ['target'])
 test = test.drop(columns = features_to_drop)
<import_modules>
datetime=['hour','day','month','year']
for time in datetime:
train_df[time]=train_df[time].astype(int)
 test_df[time]=test_df[time].astype(int )
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score<split>
train_df=pd.DataFrame(train_df )
X_train, val_X, Y_train, val_y = train_test_split(train, Target,test_size=0.1,random_state=1 )<load_from_url>
train_df.set_index('datetime',inplace=True )
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<install_modules>
test_df.set_index('datetime',inplace=True )
pip install sentencepiece<define_variables>
train_df.drop(columns=['casual','registered'],axis=1,inplace=True )
random_state_split = 21
Dropout_num = 0
learning_rate = 6e-6
valid = 0.2
epochs_num = 3
batch_size_num = 16
target_corrected = False
target_big_corrected = False<import_modules>
weather_df=pd.get_dummies(train_df['weather'],prefix='weather')
 yr_df=pd.get_dummies(train_df['year'],prefix='year')
 month_df=pd.get_dummies(train_df['month'],prefix='month')
 hour_df=pd.get_dummies(train_df['hour'],prefix='hour')
 season_df=pd.get_dummies(train_df['season'],prefix='season')
 train_df=train_df.join(weather_df)
 train_df=train_df.join(yr_df)
 train_df=train_df.join(month_df)
 train_df=train_df.join(hour_df)
 train_df=train_df.join(season_df)
 weather_df=pd.get_dummies(test_df['weather'],prefix='weather')
 yr_df=pd.get_dummies(test_df['year'],prefix='year')
 month_df=pd.get_dummies(test_df['month'],prefix='month')
 hour_df=pd.get_dummies(test_df['hour'],prefix='hour')
 season_df=pd.get_dummies(test_df['season'],prefix='season')
 test_df=test_df.join(weather_df)
 test_df=test_df.join(yr_df)
 test_df=test_df.join(month_df)
 test_df=test_df.join(hour_df)
 test_df=test_df.join(season_df )
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<categorify>
train_df.drop(columns=['season','hour','month','year','weather'],axis=1,inplace=True)
 test_df.drop(columns=['season','hour','month','year','weather'],axis=1,inplace=True )
def bert_encode(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<choose_model_class>
def rmlse(predicted,actual):
sum_val=0
for i in range(len(predicted)) :
sum_val+=(np.log(predicted[i]+1)-np.log(actual[i]+1)) **2
return(sum_val/len(predicted)) **(0.5 )
def build_model(bert_layer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
if Dropout_num == 0:
out = Dense(1, activation='sigmoid' )(clf_output)
 else:
x = Dropout(Dropout_num )(clf_output)
 out = Dense(1, activation='sigmoid' )(x)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=6e-6), loss='binary_crossentropy', metrics=['accuracy'])
 return model<choose_model_class>
X=train_df.drop(columns='count',axis=1)
 y=train_df['count']
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>
xgr=xgb.XGBRegressor(learning_rate =0.1, n_estimators=1000, max_depth=9,
min_child_weight=4, gamma=0.125, subsample=1, colsample_bytree=0.8)
 xgr.fit(train_df.drop(columns='count',axis=1),train_df['count'])
 y_predict=xgr.predict(test_df )
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 )<categorify>
test_df['count']=np.exp(y_predict )
train_input = bert_encode(text_clean_train, tokenizer, max_len=160)
 test_input = bert_encode(text_clean_test, tokenizer, max_len=160)
 train_labels =Target.values<train_model>
result=pd.DataFrame()
checkpoint = ModelCheckpoint('model_BERT.h5', monitor='val_loss', save_best_only=True)
 train_history = model_BERT.fit(
 train_input, train_labels,
validation_split = 0.2,
epochs = 3,
callbacks=[checkpoint],
batch_size = 16
)<predict_on_test>
result['datetime']=test_df['datetime']
result['count']=test_df['count']
model_BERT.load_weights('model_BERT.h5')
 test_pred_BERT = model_BERT.predict(test_input)
 test_pred_BERT_int = test_pred_BERT.round().astype('int' )<predict_on_test>
result.to_csv('output.csv',index=False )