Kaggle competition: Bike Sharing Demand
corpus=create_corpus(data )<categorify>
%matplotlib inline
MAX_LEN=40
tokenizer_obj=Tokenizer()
tokenizer_obj.fit_on_texts(corpus)
 sequences=tokenizer_obj.texts_to_sequences(corpus)
 tweet_pad=pad_sequences(sequences,maxlen=MAX_LEN,truncating='post',padding='post' )<count_unique_values>
train = pd.read_csv('/kaggle/input/bike-sharing-demand/train.csv')
 test = pd.read_csv('/kaggle/input/bike-sharing-demand/test.csv' )
word_index=tokenizer_obj.word_index
print('Number of unique words:',len(word_index))<define_variables>
train.drop(['casual', 'registered'], axis=1, inplace=True )
num_words=len(word_index)+1
embedding_matrix=np.zeros(( num_words,200))
for word,i in tqdm(word_index.items()):
if i > num_words:
continue
emb_vec=embedding_dict.get(word)
 if emb_vec is not None:
embedding_matrix[i]=emb_vec<choose_model_class>
train['datetime'] = pd.to_datetime(train['datetime'], errors='coerce')
 test['datetime'] = pd.to_datetime(test['datetime'], errors='coerce')
model=Sequential()
embedding=Embedding(num_words,200,embeddings_initializer=Constant(embedding_matrix),
input_length=MAX_LEN,trainable=False)
 model.add(embedding)
 model.add(SpatialDropout1D(0.2))
model.add(LSTM(64,dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
optimzer=Adam(learning_rate=0.001)
 model.compile(loss='binary_crossentropy',optimizer=optimzer,metrics=['accuracy'])
 model.summary()<choose_model_class>
categorical_cols=['season','holiday','workingday','weather']
numerical_cols=['temp','atemp','humidity','windspeed']
label='count'
filepath="weights-improvement-{epoch:02d}-{val_accuracy:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
 es = EarlyStopping(monitor='val_accuracy', mode='max', verbose=1, patience=30)
 callbacks_list = [checkpoint,es]<split>
train['Month'] = train['datetime'].dt.month
test['Month'] = test['datetime'].dt.month
train['Year'] = train['datetime'].dt.year
test['Year'] = test['datetime'].dt.year
train['WeekDay'] = train['datetime'].dt.weekday
test['WeekDay'] = test['datetime'].dt.weekday
train['Hour'] = train['datetime'].dt.hour
test['Hour'] = test['datetime'].dt.hour
tweet =data.iloc[:7613,:]
test = data.iloc[7613:,:]<prepare_x_and_y>
def encodetime(train,test,col,label):
d3=train[[col,label]].groupby(col ).mean()
d3.sort_values(by='count',ascending=False)
 plt.scatter(x=d3.index,y=d3['count'])
 plt.xlabel(col)
 d3=d3.sort_values(by='count')
 d3['w']=np.arange(train[col].nunique())
 dic=dict(zip(d3.index,d3['w']))
train[col]=train[col].map(dic)
 test[col]=test[col].map(dic )
train=tweet_pad[:tweet.shape[0]]
test=tweet_pad[tweet.shape[0]:]<split>
encodetime(train,test,'Year',label )
X_train,X_test,y_train,y_test=train_test_split(train,tweet['target'].values,test_size=0.2)
 print('Shape of train',X_train.shape)
 print("Shape of Validation ",X_test.shape )<train_model>
encodetime(train,test,'Month',label)
history=model.fit(X_train,y_train,batch_size=100,epochs=20,callbacks=callbacks_list,validation_data=(X_test,y_test),verbose=1 )<load_from_csv>
encodetime(train,test,'Hour',label)
sample_sub=pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv' )<save_to_csv>
encodetime(train,test,'WeekDay',label)
y_pre = model.predict(test)
 y_pre=np.round(y_pre ).astype(int ).reshape(3263)
 sub=pd.DataFrame({'id':sample_sub['id'].values.tolist() ,'target':y_pre})
 sub.to_csv('submission.csv',index=False )<set_options>
features=['holiday', 'workingday', 'weather', 'temp', 'atemp', 'Hour']
def lag(data,features, shift):
for feature in features:
data['lag_'+str(shift)+'_'+feature] = data[feature].shift(shift )
%matplotlib inline 
<load_from_csv>
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 all_data = [train,test]<groupby>
train.isnull().sum()
print(train[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean())
<groupby>
train.isnull().sum()
print(train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean())
<groupby>
train.fillna(-1, inplace=True)
 test.fillna(-1, inplace=True )
for dataset in all_data:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
print(train[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean() )<drop_column>
X = train.drop(['count','datetime'],axis=1)
 xtest = test.drop(['datetime'],axis=1)
 y = train['count']
tss = TimeSeriesSplit(n_splits = 5)
 for train_index, test_index in tss.split(X):
X_train, X_valid = X.iloc[train_index, :], X.iloc[test_index,:]
y_train, y_valid = y.iloc[train_index], y.iloc[test_index]
train = train.drop(['Parch', 'SibSp'], axis=1)
 test = test.drop(['Parch', 'SibSp'], axis=1)
 all_data = [train, test]
train.head() 
<data_type_conversions>
xt,xv,yt,yv=train_test_split(X,y,test_size=0.2,random_state=0 )
for dataset in all_data:
dataset['Sex'] = dataset['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 train.head()<define_variables>
def mk_model_xgb(xt,xv,yt,yv,func1,func2,lr=1,min_child_weight =25,colsample_bytree = 0.8,md=None):
model =XGBRegressor(colsample_bytree = colsample_bytree, learning_rate = lr,min_child_weight =min_child_weight, max_depth=md)
 ytt=yt.apply(func1)
 model.fit(xt,ytt)
 ypt=np.apply_along_axis(func2,arr=model.predict(xt),axis=0)
 ypv=np.apply_along_axis(func2,arr=model.predict(xv),axis=0)
 print('training r2:',r2_score(yt,ypt))
print('Validation r2:',r2_score(yv,ypv))
print('training rmsle:',np.sqrt(msle(yt,ypt)))
 print('validation rmsle:',np.sqrt(msle(yv,ypv)))
 return model
guess_ages = np.zeros(( 2,3))
guess_ages 
<define_search_space>
_=mk_model_xgb(X_train,X_valid,y_train,y_valid,func1=trans,func2=rev_trans,lr=0.2,min_child_weight =20,colsample_bytree = 0.8,md=20 )
for dataset in all_data:
for i in range(0, 2):
for j in range(0, 3):
guess_data = dataset[(dataset['Sex'] == i)& \ 
(dataset['Pclass'] == j+1)]['Age'].dropna()
age_guess = guess_data.median()
guess_ages[i,j] = int(age_guess/0.5 + 0.5)* 0.5
for i in range(0, 2):
for j in range(0, 3):
dataset.loc[(dataset.Age.isnull())&(dataset.Sex == i)&(dataset.Pclass == j+1),\
'Age'] = guess_ages[i,j]
dataset['Age'] = dataset['Age'].astype(int)
 train.head() 
<sort_values>
_=mk_model_xgb(xt,xv,yt,yv,func1=trans,func2=rev_trans,lr=0.2,min_child_weight =20,colsample_bytree = 0.8,md=20 )
train['Age_group'] = pd.cut(train['Age'], 5)
 train[['Age_group', 'Survived']].groupby(['Age_group'], as_index=False ).mean().sort_values(by='Age_group', ascending=True)
<feature_engineering>
model=XGBRegressor(colsample_bytree = 0.8, learning_rate = 0.2,min_child_weight =20, max_depth=20 ).fit(X,y.apply(trans))
for dataset in all_data:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age']
train.head() 
<drop_column>
log_y_pred = model.predict(xtest )
train = train.drop(['Age_group'], axis=1)
 all_data = [train, test]
train.head() 
<groupby>
yp=np.apply_along_axis(rev_trans,arr=log_y_pred,axis=0)
 yp = np.round(yp )
for dataset in all_data:
dataset['Embarked'] = dataset['Embarked'].fillna('S')
 print(train[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean())
<data_type_conversions>
submission = test[['datetime']].copy()
submission['count'] = yp
submission.head()
for dataset in all_data:
dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train.head() 
<drop_column>
submission.to_csv('/kaggle/working/submission.csv', index=False )