Kaggle competition: Bike Sharing Demand
<SOS> metric: RMSLE Kaggle data source: bike-sharing-demand<categorify>
import os
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_log_error
from sklearn.model_selection import GridSearchCV
from sklearn.experimental import enable_hist_gradient_boosting
from sklearn.ensemble import HistGradientBoostingRegressor
train_input = bert_encode(train.text.values, tokenizer, max_len=160)
 train_labels = train.target.values
test_input = bert_encode(test.text.values, tokenizer, max_len=160 )<train_model>
df=pd.read_csv('.. /input/bike-sharing-demand/train.csv')
 test=pd.read_csv('.. /input/bike-sharing-demand/test.csv')
train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=3,
batch_size=16 
)
 model.save('model.h5' )<predict_on_test>
df.isnull().sum()
test_pred = model.predict(test_input )<save_to_csv>
df['Date_time'] = df['datetime'].astype('datetime64[ns]')
 df['Year']=df.Date_time.dt.year
df['Month']=df.Date_time.dt.month
df['Day']=df.Date_time.dt.day
df['Hour']=df.Date_time.dt.hour
test['Date_time'] = test['datetime'].astype('datetime64[ns]')
 test['Year']=test.Date_time.dt.year
test['Month']=test.Date_time.dt.month
test['Day']=test.Date_time.dt.day
test['Hour']=test.Date_time.dt.hour
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )<load_from_csv>
for i in df.columns:
print(i)
 print(df[i].value_counts().sum)
 print("***********************************************" )
train_data = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test_data = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 train_data.head()<categorify>
df = df.drop(columns=['holiday'])
 test = test.drop(columns=['holiday'])
 df['windspeed']=df['windspeed'].replace(0,df['windspeed'].median())
 test['windspeed']=test['windspeed'].replace(0,test['windspeed'].median())
 df['weekday'] = 0
for ind in df.index:
df['weekday'][ind]= calendar.weekday(df['Year'][ind], df['Month'][ind] , df['Day'][ind])
 test['weekday'] = 0
for ind in test.index:
test['weekday'][ind]= calendar.weekday(test['Year'][ind], test['Month'][ind] , test['Day'][ind])
 df['is_night'] = 0
df.loc[(df['Hour'] < 6)|(df['Hour'] > 20), 'is_night'] = 1
test['is_night'] = 0
test.loc[(test['Hour'] < 6)|(test['Hour'] > 20), 'is_night'] = 1
df["weekend"] = df["weekday"].apply(lambda x: 1 if x == 5 or x == 6 else 0)
 test["weekend"] = test["weekday"].apply(lambda x: 1 if x == 5 or x == 6 else 0)
 df["day_cos"] = np.cos(( df['Hour'])*(2 * np.pi / 24))
df["day_sin"] = np.sin(( df['Hour'])*(2 * np.pi / 24))
df["month_cos"] = np.cos(( df['Month'])*(2 * np.pi / 12))
df["month_sin"] = np.sin(( df['Month'])*(2 * np.pi / 12))
df["weekday_sin"] = np.sin(( df['weekday'])*(2 * np.pi / 7))
df["weekday_cos"] = np.cos(( df['weekday'])*(2 * np.pi / 7))
test["day_cos"] = np.cos(( test['Hour'])*(2 * np.pi / 24))
test["day_sin"] = np.sin(( test['Hour'])*(2 * np.pi / 24))
test["month_cos"] = np.cos(( test['Month'])*(2 * np.pi / 12))
test["month_sin"] = np.sin(( test['Month'])*(2 * np.pi / 12))
test["weekday_sin"] = np.sin(( test['weekday'])*(2 * np.pi / 7))
test["weekday_cos"] = np.cos(( test['weekday'])*(2 * np.pi / 7))
Q1_windspeed = df['windspeed'].quantile(0.25)
 Q3_windspeed = df['windspeed'].quantile(0.75)
 IQR1 = Q3_windspeed - Q1_windspeed
Q1_causal = df['casual'].quantile(0.25)
 Q3_causal = df['casual'].quantile(0.75)
 IQR2 = Q3_causal - Q1_causal
Q1_registered = df['registered'].quantile(0.25)
 Q3_registered = df['registered'].quantile(0.75)
 IQR3 = Q3_registered - Q1_registered
df.loc[df.windspeed > 16.997900, 'windspeed'] = np.nan
df['windspeed'].fillna(1.5*IQR1,inplace=True)
 df.loc[df.casual > 49.000000, 'casual'] = np.nan
df['casual'].fillna(1.5*IQR2,inplace=True)
 df.loc[df.registered >222.000000, 'registered'] = np.nan
df['registered'].fillna(1.5*IQR3,inplace=True )
def decontracted(phrase):
phrase = re.sub(r"won't", "will not", phrase)
 phrase = re.sub(r"can't", "can not", phrase)
 phrase = re.sub(r"n't", " not", phrase)
 phrase = re.sub(r"'re", " are", phrase)
 phrase = re.sub(r"'s", " is", phrase)
 phrase = re.sub(r"'d", " would", phrase)
 phrase = re.sub(r"'ll", " will", phrase)
 phrase = re.sub(r"'t", " not", phrase)
 phrase = re.sub(r"'ve", " have", phrase)
 phrase = re.sub(r"'m", " am", phrase)
 return phrase<categorify>
train_df, val_df = train_test_split(df, test_size=0.2, random_state=42,shuffle=True)
 X_train = train_df.drop(columns=['count'])
 y_train = train_df['count']
X_val = val_df.drop(columns=['count'])
 y_val = val_df['count']
print(list(train_df.columns))
nlp = spacy.load('en')
 def preprocessing(text):
text = text.replace('
text = decontracted(text)
 text = re.sub('\S*@\S*\s?','',text)
 text = re.sub('http[s]?:(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F])) +','',text)
 token=[]
result=''
text = re.sub('[^A-z]', ' ',text.lower())
 text = nlp(text)
 for t in text:
if not t.is_stop and len(t)>2:
token.append(t.lemma_)
 result = ' '.join([i for i in token])
 return result.strip()<feature_engineering>
X_train = X_train [['weekend','is_night','weekday','temp','season', 'workingday', 'weather','humidity', 'windspeed','Year', 'Month', 'Day', 'Hour']]
X_val = X_val [list(X_train.columns)]
X_test = test[list(X_train.columns)]
data.text = data.text.apply(lambda x : preprocessing(x))<string_transform>
RegModel=XGBRegressor(learning_rate = 0.07,max_depth = 10, alpha = 6, n_estimators = 350)
 y_train = np.log(train_df['count']+1)
 RegModel.fit(X_train,y_train)
 train_predict=RegModel.predict(X_train)
 train_predict=np.exp(train_predict)
 Error1= np.sqrt(mean_squared_log_error(np.exp(y_train), train_predict))
print('RMSLE Training:' + str(Error1))
predict= RegModel.predict(X_val)
 predict=np.exp(predict)
 Error= np.sqrt(mean_squared_log_error(y_val , predict))
print('RMSLE Validation:' + str(Error))
stop=set(stopwords.words('english'))
def create_corpus(df):
corpus=[]
for tweet in tqdm(df['text']):
words=[word.lower() for word in word_tokenize(tweet)if(( word.isalpha() ==1)&(word not in stop)) ]
corpus.append(words)
 return corpus<statistical_test>
y_test_predicted = RegModel.predict(X_test)
 y_test_predicted=np.exp(y_test_predicted)
 test['count'] = y_test_predicted.astype(int)
 test[['datetime', 'count']].to_csv('/kaggle/working/submission.csv', index=False)