Kaggle competition: Bike Sharing Demand
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text )<categorify>
df = pd.read_csv('.. /input/train.csv', parse_dates=[0])
 test = pd.read_csv('.. /input/test.csv', parse_dates=[0] )
def remove_punctuations(text):
punctuations = '@
for p in punctuations:
text = text.replace(p, f' {p} ')
 text = text.replace('...', '...')
 if '...' not in text:
text = text.replace('.. ', '...')
 return text<define_variables>
df.rename(columns={'count':'rentals'},inplace=True )
abbreviations = {
"$" : " dollar ",
"€" : " euro ",
"4ao" : "for adults only",
"a.m" : "before midday",
"a3" : "anytime anywhere anyplace",
"aamof" : "as a matter of fact",
"acct" : "account",
"adih" : "another day in hell",
"afaic" : "as far as i am concerned",
"afaict" : "as far as i can tell",
"afaik" : "as far as i know",
"afair" : "as far as i remember",
"afk" : "away from keyboard",
"app" : "application",
"approx" : "approximately",
"apps" : "applications",
"asap" : "as soon as possible",
"asl" : "age, sex, location",
"atk" : "at the keyboard",
"ave." : "avenue",
"aymm" : "are you my mother",
"ayor" : "at your own risk",
"b&b" : "bed and breakfast",
"b+b" : "bed and breakfast",
"b.c" : "before christ",
"b2b" : "business to business",
"b2c" : "business to customer",
"b4" : "before",
"b4n" : "bye for now",
"b@u" : "back at you",
"bae" : "before anyone else",
"bak" : "back at keyboard",
"bbbg" : "bye bye be good",
"bbc" : "british broadcasting corporation",
"bbias" : "be back in a second",
"bbl" : "be back later",
"bbs" : "be back soon",
"be4" : "before",
"bfn" : "bye for now",
"blvd" : "boulevard",
"bout" : "about",
"brb" : "be right back",
"bros" : "brothers",
"brt" : "be right there",
"bsaaw" : "big smile and a wink",
"btw" : "by the way",
"bwl" : "bursting with laughter",
"c/o" : "care of",
"cet" : "central european time",
"cf" : "compare",
"cia" : "central intelligence agency",
"csl" : "can not stop laughing",
"cu" : "see you",
"cul8r" : "see you later",
"cv" : "curriculum vitae",
"cwot" : "complete waste of time",
"cya" : "see you",
"cyt" : "see you tomorrow",
"dae" : "does anyone else",
"dbmib" : "do not bother me i am busy",
"diy" : "do it yourself",
"dm" : "direct message",
"dwh" : "during work hours",
"e123" : "easy as one two three",
"eet" : "eastern european time",
"eg" : "example",
"embm" : "early morning business meeting",
"encl" : "enclosed",
"encl." : "enclosed",
"etc" : "and so on",
"faq" : "frequently asked questions",
"fawc" : "for anyone who cares",
"fb" : "facebook",
"fc" : "fingers crossed",
"fig" : "figure",
"fimh" : "forever in my heart",
"ft." : "feet",
"ft" : "featuring",
"ftl" : "for the loss",
"ftw" : "for the win",
"fwiw" : "for what it is worth",
"fyi" : "for your information",
"g9" : "genius",
"gahoy" : "get a hold of yourself",
"gal" : "get a life",
"gcse" : "general certificate of secondary education",
"gfn" : "gone for now",
"gg" : "good game",
"gl" : "good luck",
"glhf" : "good luck have fun",
"gmt" : "greenwich mean time",
"gmta" : "great minds think alike",
"gn" : "good night",
"g.o.a.t" : "greatest of all time",
"goat" : "greatest of all time",
"goi" : "get over it",
"gps" : "global positioning system",
"gr8" : "great",
"gratz" : "congratulations",
"gyal" : "girl",
"h&c" : "hot and cold",
"hp" : "horsepower",
"hr" : "hour",
"hrh" : "his royal highness",
"ht" : "height",
"ibrb" : "i will be right back",
"ic" : "i see",
"icq" : "i seek you",
"icymi" : "in case you missed it",
"idc" : "i do not care",
"idgadf" : "i do not give a damn fuck",
"idgaf" : "i do not give a fuck",
"idk" : "i do not know",
"ie" : "that is",
"i.e" : "that is",
"ifyp" : "i feel your pain",
"IG" : "instagram",
"iirc" : "if i remember correctly",
"ilu" : "i love you",
"ily" : "i love you",
"imho" : "in my humble opinion",
"imo" : "in my opinion",
"imu" : "i miss you",
"iow" : "in other words",
"irl" : "in real life",
"j4f" : "just for fun",
"jic" : "just in case",
"jk" : "just kidding",
"jsyk" : "just so you know",
"l8r" : "later",
"lb" : "pound",
"lbs" : "pounds",
"ldr" : "long distance relationship",
"lmao" : "laugh my ass off",
"lmfao" : "laugh my fucking ass off",
"lol" : "laughing out loud",
"ltd" : "limited",
"ltns" : "long time no see",
"m8" : "mate",
"mf" : "motherfucker",
"mfs" : "motherfuckers",
"mfw" : "my face when",
"mofo" : "motherfucker",
"mph" : "miles per hour",
"mr" : "mister",
"mrw" : "my reaction when",
"ms" : "miss",
"mte" : "my thoughts exactly",
"nagi" : "not a good idea",
"nbc" : "national broadcasting company",
"nbd" : "not big deal",
"nfs" : "not for sale",
"ngl" : "not going to lie",
"nhs" : "national health service",
"nrn" : "no reply necessary",
"nsfl" : "not safe for life",
"nsfw" : "not safe for work",
"nth" : "nice to have",
"nvr" : "never",
"nyc" : "new york city",
"oc" : "original content",
"og" : "original",
"ohp" : "overhead projector",
"oic" : "oh i see",
"omdb" : "over my dead body",
"omg" : "oh my god",
"omw" : "on my way",
"p.a" : "per annum",
"p.m" : "after midday",
"pm" : "prime minister",
"poc" : "people of color",
"pov" : "point of view",
"pp" : "pages",
"ppl" : "people",
"prw" : "parents are watching",
"ps" : "postscript",
"pt" : "point",
"ptb" : "please text back",
"pto" : "please turn over",
"qpsa" : "what happens",
"ratchet" : "rude",
"rbtl" : "read between the lines",
"rlrt" : "real life retweet",
"rofl" : "rolling on the floor laughing",
"roflol" : "rolling on the floor laughing out loud",
"rotflmao" : "rolling on the floor laughing my ass off",
"rt" : "retweet",
"ruok" : "are you ok",
"sfw" : "safe for work",
"sk8" : "skate",
"smh" : "shake my head",
"sq" : "square",
"srsly" : "seriously",
"ssdd" : "same stuff different day",
"tbh" : "to be honest",
"tbs" : "tablespooful",
"tbsp" : "tablespooful",
"tfw" : "that feeling when",
"thks" : "thank you",
"tho" : "though",
"thx" : "thank you",
"tia" : "thanks in advance",
"til" : "today i learned",
"tl;dr" : "too long i did not read",
"tldr" : "too long i did not read",
"tmb" : "tweet me back",
"tntl" : "trying not to laugh",
"ttyl" : "talk to you later",
"u" : "you",
"u2" : "you too",
"u4e" : "yours for ever",
"utc" : "coordinated universal time",
"w/" : "with",
"w/o" : "without",
"w8" : "wait",
"wassup" : "what is up",
"wb" : "welcome back",
"wtf" : "what the fuck",
"wtg" : "way to go",
"wtpa" : "where the party at",
"wuf" : "where are you from",
"wuzup" : "what is up",
"wywh" : "wish you were here",
"yd" : "yard",
"ygtr" : "you got that right",
"ynk" : "you never know",
"zzz" : "sleeping bored and tired"
}<categorify>
df = df.append(test, sort=False )
def convert_abbrev(word):
return abbreviations[word.lower() ] if word.lower() in abbreviations.keys() else word<categorify>
for col in ['rentals','registered','casual']:
df[col] = np.log(df[col]+1 )
def convert_abbrev_in_text(text):
tokens = word_tokenize(text)
 tokens = [convert_abbrev(word)for word in tokens]
text = ' '.join(tokens)
 return text<choose_model_class>
df['year'] = df.datetime.dt.year
df['month'] = df.datetime.dt.month
df['day'] = df.datetime.dt.day
df['dayofweek'] = df.datetime.dt.dayofweek
df['hour'] = df['datetime'].dt.hour
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )<load_from_csv>
df.set_index('datetime', inplace=True )
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv" )<feature_engineering>
df.sort_index(inplace=True )
if target_big_corrected:
train["text"] = train["text"].apply(lambda x: clean_tweets(x))
test["text"] = test["text"].apply(lambda x: clean_tweets(x))
train["text"] = train["text"].apply(lambda x: remove_emoji(x))
test["text"] = test["text"].apply(lambda x: remove_emoji(x))
train["text"] = train["text"].apply(lambda x: remove_punctuations(x))
test["text"] = test["text"].apply(lambda x: remove_punctuations(x))
train["text"] = train["text"].apply(lambda x: convert_abbrev_in_text(x))
test["text"] = test["text"].apply(lambda x: convert_abbrev_in_text(x))<feature_engineering>
df['rolling_temp'] = df['temp'].rolling(4, min_periods=1 ).mean()
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[df['rentals'].isnull() ]
df = df[~df['rentals'].isnull() ]
train_input = bert_encode(train.text.values, tokenizer, max_len=160)
 test_input = bert_encode(test.text.values, tokenizer, max_len=160)
 train_labels = train.target.values<define_variables>
removed_cols = ['rentals', 'casual','registered','datetime']
feats = [c for c in df.columns if c not in removed_cols]
random_state_split = 7
Dropout_num = 0
learning_rate = 3e-5
valid = 0.2
epochs_num = 3
batch_size_num = 16
target_corrected = True
target_big_corrected = True<define_variables>
train, valid = train_test_split(df, random_state=42 )
random_state_split = 7
Dropout_num = 0.3
learning_rate = 4e-5
validation_split = 0.3
epochs_num = 3
batch_size_num = 16
target_corrected = True
target_big_corrected = True<train_model>
rf = RandomForestRegressor(random_state=42, n_estimators=100,n_jobs=-1 )
checkpoint = ModelCheckpoint('model_BERT.h5', monitor='val_loss', save_best_only=False)
 train_history = model_BERT.fit(
 train_input, train_labels,
validation_split = valid,
epochs = epochs_num,
callbacks=[checkpoint],
batch_size = batch_size_num
)<predict_on_test>
rf.fit(train[feats],train['rentals'] )
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>
preds = rf.predict(valid[feats] )
train_pred_BERT = model_BERT.predict(train_input)
 train_pred_BERT_int = train_pred_BERT.round().astype('int' )<create_dataframe>
mean_squared_error(valid['rentals'],preds)**(1/2 )
pred = pd.DataFrame(test_pred_BERT, columns=['preds'])
 pred.plot.hist()<prepare_output>
preds_test = rf.predict(test[feats] )
submission['target'] = test_pred_BERT_int
submission.head(10 )<save_to_csv>
test['count'] = np.exp(preds_test)-1
submission.to_csv("submission.csv", index=False, header=True )<import_modules>
test[['datetime','count']].to_csv('rf1.csv', index=False)
print(keras.__version__)
 print(tensorflow.__version__)
 EMBEDDING_FILES = [
'.. /input/gensim-embeddings-dataset/crawl-300d-2M.gensim',
'.. /input/gensim-embeddings-dataset/glove.840B.300d.gensim'
]
BATCH_SIZE = 512
LSTM_UNITS = 128
DENSE_HIDDEN_UNITS = 4 * LSTM_UNITS
EPOCHS = 20
MAX_LEN = 220
TEXT_COLUMN = 'text'
TARGET_COLUMN = 'target'
CHARS_TO_REMOVE = '!"
“”’'∞θ÷α•à−β∅³π‘₹´°£€\×™√²—'<load_from_csv>
train, valid = df[df['day'] <= 15], df.query('day > 15' )
print("Read Data")
 train_df = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test_df = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 train_df.head(5 )<feature_engineering>
rf = RandomForestRegressor(random_state=42, n_estimators=100,n_jobs=-1 )
def remove_urls(text):
url = re.compile(r'https?://\S+|www\.\S+')
 return url.sub(r'',text)
 def remove_html(text):
html=re.compile(r'<.*?>')
 return html.sub(r'',text)
 def remove_punct(text):
table=str.maketrans('','',string.punctuation)
 return text.translate(table)
 def process_tweets(tweets):
tweets = remove_urls(tweets)
 tweets = remove_html(tweets)
 tweets = remove_punct(tweets)
 return tweets
train_df["text"] = train_df["text"].map(process_tweets)
 test_df["text"] = test_df["text"].map(process_tweets )<prepare_x_and_y>
rf.fit(train[feats],train['rentals'] )
x_train = train_df[TEXT_COLUMN].astype(str)
 y_train = train_df[TARGET_COLUMN].values
x_test = test_df[TEXT_COLUMN].astype(str)
 tokenizer = text.Tokenizer(filters=CHARS_TO_REMOVE, lower=False)
 tokenizer.fit_on_texts(list(x_train)+ list(x_test))
x_train = tokenizer.texts_to_sequences(x_train)
 x_test = tokenizer.texts_to_sequences(x_test)
 x_train = sequence.pad_sequences(x_train, maxlen=MAX_LEN)
 x_test = sequence.pad_sequences(x_test, maxlen=MAX_LEN)
 checkpoint_predictions = []
weights = []
def build_matrix(word_index, path):
embedding_index = KeyedVectors.load(path, mmap='r')
 embedding_matrix = np.zeros(( len(word_index)+ 1, 300))
for word, i in word_index.items() :
for candidate in [word, word.lower() ]:
if candidate in embedding_index:
embedding_matrix[i] = embedding_index[candidate]
break
return embedding_matrix
embedding_matrix = np.concatenate(
 [build_matrix(tokenizer.word_index, f)for f in EMBEDDING_FILES], axis=-1)
 def build_model(embedding_matrix):
words = Input(shape=(None,))
x = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False )(words)
 x = SpatialDropout1D(0.5 )(x)
 x = Bidirectional(LSTM(LSTM_UNITS, return_sequences=True, dropout=0.3, recurrent_dropout=0.3))(x)
 x = Bidirectional(LSTM(LSTM_UNITS, return_sequences=True, dropout=0.3, recurrent_dropout=0.3))(x)
 hidden = concatenate([
GlobalMaxPooling1D()(x),
GlobalAveragePooling1D()(x)
 ])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 result = Dense(1, activation='sigmoid' )(hidden)
 model = Model(inputs=words, outputs=result)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model<train_model>
preds = rf.predict(valid[feats] )
print("Start Modeling")
 model = build_model(embedding_matrix)
 model.fit(
 x_train,
y_train,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
validation_split=0.2,
verbose=1 
)
 print("Modeling Complete" )<save_to_csv>
mean_squared_error(valid['rentals'],preds)**(1/2 )
predictions = model.predict(x_test)
 submission = pd.DataFrame.from_dict({
'id': test_df.id,
TARGET_COLUMN: np.round(predictions ).astype(int ).reshape(x_test.shape[0])
 })
 submission.to_csv('lstm_submission.csv', index=False)
 submission.head()<define_variables>
preds_test = rf.predict(test[feats] )
start_time = time.time()<set_options>
test['count'] = np.exp(preds_test)-1
%matplotlib inline 
<install_modules>
test[['datetime','count']].to_csv('rf2.csv', index=False)
!pip install tensorflow-text==2.0.0 --user<import_modules>
rf = RandomForestRegressor(random_state=42, n_estimators=100,n_jobs=-1,oob_score=True )
import tensorflow as tf
import tensorflow_hub as hub<import_modules>
rf.fit(df[feats],df['rentals'])
 rf.oob_score_
import tensorflow_text as text<load_from_csv>
preds = rf.oob_prediction_
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv" )<categorify>
mean_squared_error(df['rentals'],preds)**(1/2 )
def clean(text):
text = re.sub(r"http\S+", " ", text)
 text = re.sub(r"RT ", " ", text)
 text = re.sub(r"[^a-zA-Z
text = re.sub(r"[0-9]", " ", text)
 text = re.sub(r"\s+", " ", text)
 text = text.strip()
return text<feature_engineering>
preds_test = rf.predict(test[feats] )
train.text = train.text.apply(clean)
 test.text = test.text.apply(clean )<load_pretrained>
test['count'] = np.exp(preds_test)-1
use = hub.load("https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3" )<categorify>
test[['datetime','count']].to_csv('rf3.csv', index=False)
X_train = []
for r in tqdm(train.text.values):
emb = use(r)
 review_emb = tf.reshape(emb, [-1] ).numpy()
X_train.append(review_emb)
 X_train = np.array(X_train)
 y_train = train.target.values
X_test = []
for r in tqdm(test.text.values):
emb = use(r)
 review_emb = tf.reshape(emb, [-1] ).numpy()
X_test.append(review_emb)
 X_test = np.array(X_test )<split>
def cv(df, test, feats, y_name, k=5):
score, preds, fis = [], [], []
chunk = df.shape[0] // k
for i in range(k):
if i+1 < k:
valid = df.iloc[i*chunk:(i+1)*chunk]
train = df.iloc[:i*chunk].append(df.iloc[(i+1)*chunk:])
 else:
valid: df.iloc[i*chunk:]
train: df.iloc[:i*chunk]
rf = RandomForestRegressor(random_state=42, n_jobs=-1, n_estimators=100)
 rf.fit(train[feats],train[y_name])
 score.append(mean_squared_error(valid[y_name],rf.predict(valid[feats])) **(1/2))
preds.append(rf.predict(test[feats]))
fis.append(rf.feature_importances_)
 print(i, 'OK')
 return score, preds, fis
train_arrays, test_arrays, train_labels, test_labels = train_test_split(X_train,
y_train,
random_state =42,
test_size=0.05 )<import_modules>
score, preds, fis = cv(df, test, feats, 'rentals' )
import xgboost as xgb
from xgboost import XGBClassifier<train_on_grid>
pd.Series(score ).mean()
def xgboost_param_selection(X, y, nfolds):
depth_m=[2,3,5,7,9]
base_learners=[2,5,50,70,100]
parameters=dict(n_estimators=base_learners , max_depth=depth_m)
 clf=RandomizedSearchCV(XGBClassifier(n_jobs=-1, class_weight='balanced'),parameters, scoring='roc_auc', refit=True, cv=3)
 clf.fit(X, y)
 clf.best_params_
return clf 
<train_on_grid>
test['count'] = np.exp(pd.DataFrame(preds ).mean())-1
test[['datetime','count']].to_csv('rf4.csv', index=False)