Kaggle competition: Bike Sharing Demand
optimizer = AdamW(model.parameters() ,
lr = 2e-5,
eps = 1e-8 
)
<train_model>
data_path = '/kaggle/input/bike-sharing-demand/'
train = pd.read_csv(data_path + 'train.csv')
 test = pd.read_csv(data_path + 'test.csv')
 submission = pd.read_csv(data_path + 'sampleSubmission.csv' )
total_steps = len(train_dataloader)* epochs
scheduler = get_linear_schedule_with_warmup(optimizer,
num_warmup_steps = 0,
num_training_steps = total_steps )<compute_test_metric>
train = train[train['weather'] != 4]
<predict_on_test>
all_data = pd.concat([train, test], ignore_index=True)
 all_data
print('Predicting labels for {:,} test sentences...'.format(len(tensor_input)))
 model.eval()
predictions , true_labels = [], []
for batch in prediction_dataloader:
b_input_ids = batch[0].to(device)
 b_input_mask = batch[1].to(device)
 b_labels = batch[2].to(device)
 with torch.no_grad() :
outputs = model(b_input_ids, token_type_ids=None,
attention_mask=b_input_mask)
 logits = outputs[0]
logits = logits.detach().cpu().numpy()
label_ids = b_labels.to('cpu' ).numpy()
predictions.append(logits)
 true_labels.append(label_ids)
 print('DONE.')
<prepare_output>
all_data['date'] = all_data['datetime'].apply(lambda x: x.split() [0])
 all_data['year'] = all_data['datetime'].apply(lambda x: x.split() [0].split('-')[0])
 all_data['month'] = all_data['datetime'].apply(lambda x: x.split() [0].split('-')[1])
 all_data['hour'] = all_data['datetime'].apply(lambda x: x.split() [1].split(':')[0])
 all_data["weekday"] = all_data['date'].apply(lambda dateString : datetime.strptime(dateString,"%Y-%m-%d" ).weekday() )
flat_predictions = [item for sublist in predictions for item in sublist]
flat_predictions = np.argmax(flat_predictions, axis=1 ).flatten() 
<save_to_csv>
drop_features = ['casual', 'registered', 'datetime', 'date', 'windspeed', 'month']
all_data = all_data.drop(drop_features, axis=1 )
df_submission['target'] = flat_predictions
df_submission.to_csv('submission.csv', index = False)
<set_options>
X_train = all_data[~pd.isnull(all_data['count'])]
X_test = all_data[pd.isnull(all_data['count'])]
X_train = X_train.drop(['count'], axis=1)
 X_test = X_test.drop(['count'], axis=1)
 y = train['count']
plt.style.use('ggplot')
 stop = set(stopwords.words('english')) 
<load_from_csv>
def rmsle(y_true, y_pred, convertExp=True):
if convertExp:
y_true = np.exp(y_true)
 y_pred = np.exp(y_pred)
 log_true = np.nan_to_num(np.array([np.log(y+1)for y in y_true]))
log_pred = np.nan_to_num(np.array([np.log(y+1)for y in y_pred]))
output = np.sqrt(np.mean(( log_true - log_pred)**2))
return output
tweet = 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")
 tweet.head()<concatenate>
ridge_model = Ridge()
df = pd.concat([tweet,test])
 df.shape<categorify>
ridge_params = {'max_iter':[3000], 'alpha':[0.1, 1, 2, 3, 4, 10, 30, 100, 200, 300, 400, 800, 900, 1000]}
rmsle_scorer = metrics.make_scorer(rmsle, greater_is_better=False)
 gridsearch_ridge_model = GridSearchCV(estimator=ridge_model,
param_grid=ridge_params,
scoring=rmsle_scorer,
cv=5 )
def remove_URL(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_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)
 def remove_punct(text):
table=str.maketrans('','',string.punctuation)
 return text.translate(table )<feature_engineering>
log_y = np.log1p(y)
 gridsearch_ridge_model.fit(X_train, log_y )
df['text'] = df['text'].apply(lambda x : remove_URL(x))
df['text'] = df['text'].apply(lambda x : remove_html(x))
df['text'] = df['text'].apply(lambda x : remove_emoji(x))
df['text'] = df['text'].apply(lambda x : remove_punct(x))<prepare_x_and_y>
print('최적 파라미터 :', gridsearch_ridge_model.best_params_ )
train = df[:tweet.shape[0]]
test = df[tweet.shape[0]:]<load_from_url>
preds = gridsearch_ridge_model.best_estimator_.predict(X_train)
 print(f'릿지 회귀 RMSLE 값 : {rmsle(log_y, preds, True):.4f}' )
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
lasso_model = Lasso()
lasso_alpha = 1/np.array([0.1, 1, 2, 3, 4, 10, 30, 100, 200, 300, 400, 800, 900, 1000])
 lasso_params = {'max_iter':[3000], 'alpha':lasso_alpha}
gridsearch_lasso_model = GridSearchCV(estimator=lasso_model,
param_grid=lasso_params,
scoring=rmsle_scorer,
cv=5)
 log_y = np.log1p(y)
 gridsearch_lasso_model.fit(X_train, log_y)
 print('최적 파라미터 :', gridsearch_lasso_model.best_params_ )
import tokenization<categorify>
preds = gridsearch_lasso_model.best_estimator_.predict(X_train)
 print(f'라쏘 회귀 RMSLE 값 : {rmsle(log_y, preds, True):.4f}' )
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>
randomforest_model = RandomForestRegressor()
rf_params = {'random_state':[42], 'n_estimators':[100, 120, 140]}
gridsearch_random_forest_model = GridSearchCV(estimator=randomforest_model,
param_grid=rf_params,
scoring=rmsle_scorer,
cv=5)
 log_y = np.log1p(y)
 gridsearch_random_forest_model.fit(X_train, log_y)
 print('최적 파라미터 :', gridsearch_random_forest_model.best_params_ )
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, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=2e-6), loss='binary_crossentropy', metrics=['accuracy'])
 return model<choose_model_class>
preds = gridsearch_random_forest_model.best_estimator_.predict(X_train)
 print(f'랜덤 포레스트 회귀 RMSLE 값 : {rmsle(log_y, preds, True):.4f}' )
<feature_engineering><EOS> 
submission['count'] = np.exp(randomforest_preds)
 submission.to_csv('submission.csv', index=False )