Kaggle competition: Bike Sharing Demand
lr_clf = LogisticRegression(max_iter=150,
penalty='l2',
solver='lbfgs',
random_state=0)
 lr_clf.fit(X_train_onehot, y_train)
 lr_pred = lr_clf.predict(X_val_onehot)
 print('accuracy score: ',accuracy_score(lr_pred,y_val))
print(classification_report(y_val, lr_pred))<string_transform>
train=data=pd.read_csv('/kaggle/input/bike-sharing-demand/train.csv')
 test=pd.read_csv('/kaggle/input/bike-sharing-demand/test.csv')
 submission=pd.read_csv('/kaggle/input/bike-sharing-demand/sampleSubmission.csv')
 print(train.shape, test.shape, submission.shape )
def create_corpus_new(df):
corpus=[]
for tweet in tqdm(df['text']):
words=[word.lower() for word in word_tokenize(tweet)]
corpus.append(words)
 return corpus<statistical_test>
all_data.isnull().sum()
corpus=create_corpus_new(df )<define_variables>
all_data['datetime']=pd.to_datetime(all_data['datetime'])
 all_data['year']=all_data['datetime'].dt.year
all_data['month']=all_data['datetime'].dt.month
all_data['day']=all_data['datetime'].dt.day
all_data['hour']=all_data['datetime'].dt.hour
all_data['dayofweek']=all_data['datetime'].dt.dayofweek
all_data=all_data.drop(columns='datetime' )
embedding_dict={}
with open('.. /input/glove-global-vectors-for-word-representation/glove.6B.100d.txt','r')as f:
for line in f:
values=line.split()
word = values[0]
vectors=np.asarray(values[1:],'float32')
 embedding_dict[word]=vectors
f.close()<categorify>
all_data=all_data.drop(columns=['month', 'day'] )
MAX_LEN=50
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>
all_data['weekend']=(all_data['dayofweek']==5)|(all_data['dayofweek']==6 )
word_index=tokenizer_obj.word_index
print('Number of unique words:',len(word_index))<define_variables>
season_encoded=pd.get_dummies(all_data['season'], prefix='season_')
 all_data=pd.concat(( all_data, season_encoded), axis=1)
 all_data=all_data.drop(columns='season' )
num_words=len(word_index)+1
embedding_matrix=np.zeros(( num_words,100))
for word,i in tqdm(word_index.items()):
if i < num_words:
emb_vec=embedding_dict.get(word)
 if emb_vec is not None:
embedding_matrix[i]=emb_vec<choose_model_class>
weather_encoded=pd.get_dummies(all_data['weather'], prefix='weather_')
 all_data=pd.concat(( all_data, weather_encoded), axis=1)
 all_data=all_data.drop(columns='weather' )
model=Sequential()
embedding=Embedding(num_words,100,embeddings_initializer=Constant(embedding_matrix),
input_length=MAX_LEN,trainable=False)
 model.add(embedding)
 model.add(SpatialDropout1D(0.2))
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
optimzer=Adam(learning_rate=3e-4)
 model.compile(loss='binary_crossentropy',optimizer=optimzer,metrics=['accuracy'] )<prepare_x_and_y>
from sklearn.decomposition import PCA
train=tweet_pad[:tweet.shape[0]]
test=tweet_pad[tweet.shape[0]:]<split>
pca=PCA(n_components=1)
 pca.fit(all_data[['temp', 'atemp']])
 print(f" Variance explained after PCA : {pca.explained_variance_ratio_}" )
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 )<load_from_url>
all_data=all_data.drop(columns=['temp', 'atemp'] )
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
all_data.loc[all_data['windspeed']==0, 'windspeed']=all_data['windspeed'].mean()
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import ModelCheckpoint
import tensorflow_hub as hub
import tokenization<categorify>
Xtrain=all_data[:len(train)]
Xtest=all_data[len(train):]
Ytrain=np.log(Ytrain+1)
 print(Xtrain.shape, Ytrain.shape, Xtest.shape )
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>
from lightgbm import LGBMRegressor
from sklearn.model_selection import cross_val_score
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, :]
if Dropout_num == 0:
out = Dense(1, activation='sigmoid' )(clf_output)
 else:
x = Dropout(Dropout_num )(clf_output)
 out = Dense(1, activation='sigmoid' )(x)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=learning_rate), loss='binary_crossentropy', metrics=['accuracy'])
 return model<string_transform>
model=LGBMRegressor(boosting_type='gbdt', class_weight=None,
colsample_bytree=0.6746393485503049, importance_type='split',
learning_rate=0.03158974434726661, max_bin=55, max_depth=-1,
min_child_samples=159, min_child_weight=0.001, min_split_gain=0.0,
n_estimators=1458, n_jobs=-1, num_leaves=196, objective=None,
random_state=18, reg_alpha=0.23417614793823338,
reg_lambda=0.33890027779706655, silent=False,
subsample=0.5712459474269626, subsample_for_bin=200000,
subsample_freq=1 )
def clean_tweets(tweet):

 tweet = ''.join([x for x in tweet if x in string.printable])
 tweet = re.sub(r"http\S+", "", tweet)
 return tweet<define_variables>
model.fit(Xtrain, Ytrain)
 log_predictions=model.predict(Xtest)
 predictions=np.exp(log_predictions)-1
submission['count']=predictions
submission.to_csv('Result.csv', index=False)
 submission.head()