Kaggle competition: Bike Sharing Demand
train_pred_BERT = model_BERT.predict(train_input)
 train_pred_BERT_int = train_pred_BERT.round().astype('int' )<create_dataframe>
warnings.filterwarnings("ignore", category=DeprecationWarning)
 %matplotlib inline
plt.style.use('ggplot')
 mpl.rcParams['axes.unicode_minus'] = False
pred = pd.DataFrame(test_pred_BERT, columns=['preds'])
 pred.plot.hist()<feature_engineering>
test = pd.read_csv('.. /input/bike-sharing-demand/test.csv', parse_dates=["datetime"])
 train = pd.read_csv('.. /input/bike-sharing-demand/train.csv', parse_dates=["datetime"] )
sub_sample['target'] = test_pred_BERT_int<save_to_csv>
train['humidity'].value_counts().head(10 )
sub_sample.to_csv("submission.csv", index=False, header=True )<load_from_csv>
data = train.append(test)
 data.reset_index(inplace=True)
 data.drop('index',inplace=True,axis=1 )
df_train = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 df_test = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 df_submission = pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')
<set_options>
train["year"] = train["datetime"].dt.year
train["month"] = train["datetime"].dt.month
train["day"] = train["datetime"].dt.day
train["hour"] = train["datetime"].dt.hour
train["dayofweek"] = train["datetime"].dt.dayofweek
train.shape
if torch.cuda.is_available() :
device = torch.device("cuda")
 print('There are %d GPU(s)available.' % torch.cuda.device_count())
 print('We will use the GPU:', torch.cuda.get_device_name(0))
else:
print('No GPU available, using the CPU instead.')
 device = torch.device("cpu" )<import_modules>
test["year"] = test["datetime"].dt.year
test["month"] = test["datetime"].dt.month
test["day"] = test["datetime"].dt.day
test["hour"] = test["datetime"].dt.hour
test["dayofweek"] = test["datetime"].dt.dayofweek
test.shape
import matplotlib.pyplot as plt
import nltk
import numpy as np
import re
import string
import seaborn as sns
from nltk.corpus import stopwords
from nltk.stem.lancaster import LancasterStemmer
from sklearn.metrics import accuracy_score
from sklearn.metrics import matthews_corrcoef 
<load_pretrained>
def predict_windspeed(data):
dataWind0 = data.loc[data['windspeed'] == 0]
dataWindNot0 = data.loc[data['windspeed'] != 0]
wCol = ["season", "weather", "humidity", "month", "temp", "year", "atemp"]
dataWindNot0["windspeed"] = dataWindNot0["windspeed"].astype("str")
 rfModel_wind = RandomForestClassifier()
rfModel_wind.fit(dataWindNot0[wCol], dataWindNot0["windspeed"])
 wind0Values = rfModel_wind.predict(X = dataWind0[wCol])
 predictWind0 = dataWind0
predictWindNot0 = dataWindNot0
predictWind0["windspeed"] = wind0Values
data = predictWindNot0.append(predictWind0)
 data["windspeed"] = data["windspeed"].astype("float")
 data.reset_index(inplace=True)
 data.drop('index', inplace=True, axis=1)
 return data
nltk.download('stopwords' )<count_values>
train = predict_windspeed(train)
 test = predict_windspeed(test )
df_train.target.value_counts(normalize=True )<feature_engineering>
categoricalFeatureNames = ["season","holiday","workingday","weather","dayofweek","month","year","hour"]
numericalFeatureNames = ["temp","humidity","windspeed","atemp"]
dropFeatures = ['casual',"count","datetime","day","registered"]
alphanumeric = lambda x: re.sub('\w*\d\w*', ' ', x)
 punc_lower = lambda x: re.sub('[%s]' % re.escape(string.punctuation), ' ', x.lower())
 ' in the string and replace it with a space
remove_n = lambda x: re.sub("
", " ", x)
 remove_non_ascii = lambda x: re.sub(r'[^\x00-\x7f]',r' ', x)
 df_train['text'] = df_train['text'].map(alphanumeric ).map(punc_lower ).map(remove_n ).map(remove_non_ascii)
 df_test['text'] = df_test['text'].map(alphanumeric ).map(punc_lower ).map(remove_n ).map(remove_non_ascii)
 df_train['text'][3]<install_modules>
for var in categoricalFeatureNames:
train[var] = train[var].astype("category")
 test[var] = test[var].astype("category" )
!pip install transformers<define_variables>
datetimecol = test["datetime"]
yLabels = train["count"]
yLablesRegistered = train["registered"]
yLablesCasual = train["casual"]
sentences = df_train.text.values
sentences_test = df_test.text.values
labels = df_train.target.values<load_pretrained>
train = train.drop(dropFeatures,axis=1)
 test = test.drop(['datetime','day'],axis=1 )
print('Loading BERT tokenizer...')
 tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True )<string_transform>
def rmsle(y, y_,convertExp=True):
if convertExp:
y = np.exp(y),
y_ = np.exp(y_)
 log1 = np.nan_to_num(np.array([np.log(v + 1)for v in y]))
log2 = np.nan_to_num(np.array([np.log(v + 1)for v in y_]))
calc =(log1 - log2)** 2
return np.sqrt(np.mean(calc))
print(' Original: ', sentences[100])
 print('Tokenized: ', tokenizer.tokenize(sentences[100]))
print('Token IDs: ', tokenizer.convert_tokens_to_ids(tokenizer.tokenize(sentences[100])) )<categorify>
pd.options.mode.chained_assignment = None
warnings.filterwarnings("ignore", category=DeprecationWarning)
 lModel = LinearRegression()
yLabelsLog = np.log1p(yLabels)
 lModel.fit(X = train,y = yLabelsLog)
 preds = lModel.predict(X= train)
 print("RMSLE Value For Linear Regression: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
def tocknize(text_input):
input_ids = []
for sent in text_input:
encoded_sent = tokenizer.encode(
 sent,
add_special_tokens = True, 
)
 input_ids.append(encoded_sent)
 return input_ids<define_variables>
ridge_m_ = Ridge()
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)
 grid_ridge_m = GridSearchCV(ridge_m_,
ridge_params_,
scoring = rmsle_scorer,
cv=5)
 yLabelsLog = np.log1p(yLabels)
 grid_ridge_m.fit(train, yLabelsLog)
 preds = grid_ridge_m.predict(X= train)
 print(grid_ridge_m.best_params_)
 print("RMSLE Value For Ridge Regression: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
fig,ax= plt.subplots()
fig.set_size_inches(12,5)
 df = pd.DataFrame(grid_ridge_m.cv_results_)
 df["alpha"] = df["params"].apply(lambda x:x["alpha"])
 df["rmsle"] = df["mean_test_score"].apply(lambda x:-x)
 sns.pointplot(data=df,x="alpha",y="rmsle",ax=ax )
input_ids = tocknize(sentences )<string_transform>
lasso_m_ = 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':alpha}
grid_lasso_m = GridSearchCV(lasso_m_,lasso_params_,scoring = rmsle_scorer,cv=5)
 yLabelsLog = np.log1p(yLabels)
 grid_lasso_m.fit(train, yLabelsLog)
 preds = grid_lasso_m.predict(X= train)
 print(grid_lasso_m.best_params_)
 print("RMSLE Value For Lasso Regression: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
fig,ax= plt.subplots()
fig.set_size_inches(12,5)
 df = pd.DataFrame(grid_lasso_m.cv_results_)
 df["alpha"] = df["params"].apply(lambda x:x["alpha"])
 df["rmsle"] = df["mean_test_score"].apply(lambda x:-x)
 sns.pointplot(data=df,x="alpha",y="rmsle",ax=ax)
MAX_LEN = 64
print('
Padding/truncating all sentences to %d values...' % MAX_LEN)
 print('
Padding token: "{:}", ID: {:}'.format(tokenizer.pad_token, tokenizer.pad_token_id))
input_ids = pad_sequences(input_ids, maxlen=MAX_LEN, dtype="long",
value=0, truncating="post", padding="post")
 print('
Done.' )<define_variables>
rfModel = RandomForestRegressor(n_estimators=100)
 yLabelsLog = np.log1p(yLabels)
 rfModel.fit(train,yLabelsLog)
 preds = rfModel.predict(X= train)
 print("RMSLE Value For Random Forest: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
attention_masks = []
for sent in input_ids:
att_mask = [int(token_id > 0)for token_id in sent]
attention_masks.append(att_mask )<split>
gbm = GradientBoostingRegressor(n_estimators=4000,alpha=0.01)
 yLabelsLog = np.log1p(yLabels)
 gbm.fit(train,yLabelsLog)
 preds = gbm.predict(X= train)
 print("RMSLE Value For Gradient Boost: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
train_inputs, validation_inputs, train_labels, validation_labels = train_test_split(input_ids, labels,
random_state=2018, test_size=0.1)
 train_masks, validation_masks, _, _ = train_test_split(attention_masks, labels,
random_state=2018, test_size=0.1 )<data_type_conversions>
for var in categoricalFeatureNames:
train[var] = train[var].astype("float")
 test[var] = test[var].astype("float" )
train_inputs = torch.tensor(train_inputs)
 validation_inputs = torch.tensor(validation_inputs)
 train_labels = torch.tensor(train_labels)
 validation_labels = torch.tensor(validation_labels)
 train_masks = torch.tensor(train_masks)
 validation_masks = torch.tensor(validation_masks )<create_dataframe>
xgb = XGBRegressor(n_estimators = 500, learning_rate = 0.1, max_depth = 4)
 yLabelsLog = np.log1p(yLabels)
 xgb.fit(train,yLabelsLog)
 preds = xgb.predict(train)
 print("RMSLE Value For XGBboost: ",rmsle(np.exp(yLabelsLog),np.exp(preds),False))
batch_size = 32
train_data = TensorDataset(train_inputs, train_masks, train_labels)
 train_sampler = RandomSampler(train_data)
 train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size)
 validation_data = TensorDataset(validation_inputs, validation_masks, validation_labels)
 validation_sampler = SequentialSampler(validation_data)
 validation_dataloader = DataLoader(validation_data, sampler=validation_sampler, batch_size=batch_size)
<load_pretrained>
submission = pd.DataFrame({
"datetime": datetimecol,
"count": [max(0, x)for x in np.exp(predsTest)]
})
 submission.to_csv('bike_predictions_gbm_separate_without_fe.csv', index=False )