Kaggle competition: Bike Sharing Demand for Education()
df_union=pd.concat([df_train,df_test],sort=False ).copy()
df_union.fillna('ND', inplace = True )<feature_engineering>
import lightgbm as lgb
from sklearn.metrics import mean_squared_log_error
import matplotlib.pyplot as plt
import seaborn as sns
df_union['Month']=df_union['Date'].apply(lambda s : int(s.replace('-','')[4:6]))
df_union['Day']=df_union['Date'].apply(lambda s : int(s.replace('-','')[6:9]))
df_union['Date']=df_union['Date'].apply(lambda s : datetime.datetime.strptime(s, '%Y-%m-%d'))
df_union['DateOrd']=df_union['Date'].apply(lambda s : s.toordinal())
 df_union['Province_Norm']=df_union['Country_Region']+'-'+df_union['Province_State']
df_union.tail()<categorify>
train = pd.read_csv('/kaggle/input/bike-sharing-demand-for-education/train.csv')
 test = pd.read_csv('/kaggle/input/bike-sharing-demand-for-education/test.csv')
 sample_submission = pd.read_csv('/kaggle/input/bike-sharing-demand-for-education/sample_submission.csv' )
le1 = preprocessing.LabelEncoder()
le1.fit(df_union['Country_Region'])
 df_union['Country'] = le1.transform(df_union['Country_Region'] )<categorify>
X = train.drop(['datetime','casual','registered','cnt'],axis = 1)
 X_test = test.drop(['datetime'],axis = 1)
 y = train[['year','cnt']]
le2 = preprocessing.LabelEncoder()
le2.fit(df_union['Province_Norm'])
 df_union['Province'] = le2.transform(df_union['Province_Norm'])
 df_union.drop('Province_Norm',axis=1,inplace=True )<define_variables>
%%time
columns = X.columns
y_preds = np.zeros(X_test.shape[0])
 feature_importances = pd.DataFrame()
feature_importances['feature'] = columns
X_train = X.query("year == 2011")
 X_valid = X.query("year == 2012")
 y_train = y.query("year == 2011")
 y_valid = y.query("year == 2012")
 y_train.drop('year',axis=1, inplace=True)
 y_valid.drop('year',axis=1, inplace=True)
 dtrain = lgb.Dataset(X_train, label=y_train)
 params = {
'objective': 'mean_squared_error',
'metric': 'rmse'
}
LGBM = lgb.train(params, dtrain, 1000, verbose_eval=100)
 feature_importances['importance'] = LGBM.feature_importance(importance_type='gain')
 y_pred_valid = LGBM.predict(X_valid)
 y_pred_valid = np.where(y_pred_valid < 0, 0, y_pred_valid)
 print("RMSLE:", mean_squared_log_error(y_valid, y_pred_valid))
print('******************************************************')
 y_preds = LGBM.predict(X_test)
 y_preds = np.where(y_preds < 0, 0, y_preds )
t_max_tr = max(df_union['Date'][df_union['DsType']=='train'])
 t_min_te = min(df_union['Date'][df_union['DsType']=='test'])
 Noverlap=(t_max_tr-t_min_te ).days+1
Noverlap<concatenate>
submission = pd.concat([test['datetime'], pd.Series(y_preds)], axis=1)
 submission = submission.rename(columns={0:'cnt'} )
def cut_neg(y):
m = 0
for i in range(0,len(y)) :
if y[i]<m : y[i]=m
return y<find_best_model_class>
submission.to_csv('base_submission.csv',header=True, index=False )