Kaggle competition: Bike Sharing Demand
test.k_t = text_reconstruct_test
test_word = test.k_t.apply(lambda x: ' '.join(x))<load_pretrained>
train = pd.read_csv("/kaggle/input/bike-sharing-demand/train.csv",parse_dates=["datetime"])
 train.head()
path_for_word2vec = ".. /input/googlenewsvectorsnegative300/GoogleNews-vectors-negative300.bin"
word2vec = gensim.models.KeyedVectors.load_word2vec_format(path_for_word2vec, binary = True )<statistical_test>
test = pd.read_csv("/kaggle/input/bike-sharing-demand/test.csv",parse_dates=["datetime"])
 test.head()
def average_w2v(list_of_tokens, vct, generate_missing = False, dimentions = 300):
if len(list_of_tokens)< 1:
return np.zeros(dimentions)
 if generate_missing:
vector = [vct[item] if item in vct else np.random.rand(dimentions)for item in list_of_tokens]
else:
vector = [vct[item] if item in vct else np.zeros(dimentions)for item in list_of_tokens]
total_length = len(vector)
 sum_of_vectors = np.sum(vector, axis=0)
 average = np.divide(sum_of_vectors, total_length)
 return average 
<categorify>
train["year"] = train["datetime"].dt.year
train["month"] = train["datetime"].dt.month
train["hour"] = train["datetime"].dt.hour
train["dayofweek"] = train["datetime"].dt.dayofweek
train.shape
def word2vec_mapping(vect, our_word, generate_missing = False):
mapping = our_word.apply(lambda x: average_w2v(x, vect, generate_missing = generate_missing))
return list(mapping )<string_transform>
test["year"] = test["datetime"].dt.year
test["month"] = test["datetime"].dt.month
test["hour"] = test["datetime"].dt.hour
test["dayofweek"] = test["datetime"].dt.dayofweek
test.shape
our_tokenizer = RegexpTokenizer(r'\w+')
 tokenized_input_train = train_word.apply(our_tokenizer.tokenize)
 tokenized_input_test = test_word.apply(our_tokenizer.tokenize )<categorify>
categorical_feature = ["season","holiday","workingday","weather","dayofweek","month","year","hour"]
mapped_train = word2vec_mapping(word2vec, train.text)
 mapped_test = word2vec_mapping(word2vec,test.text )<predict_on_test>
for var in categorical_feature:
train[var] = train[var].astype("category")
 test[var] = test[var].astype("category")
 train.info()
classifier_SVM = SVC(C = 2,probability = True)
 classifier_SVM.fit(mapped_train, train.target)
 y_predicted_SVM = classifier_SVM.predict(mapped_test )<train_model>
feature = ["season","holiday","workingday","weather","dayofweek","year","hour","temp","atemp","humidity"]
classifier_LogisticRegression = LogisticRegression(C=30.0, class_weight = 'balanced', solver = 'newton-cg',
multi_class = 'multinomial', n_jobs = -1, random_state = 42)
 classifier_LogisticRegression.fit(mapped_train, train.target)
 y_predicted_LogisticRegression = classifier_LogisticRegression.predict(mapped_test )<groupby>
X_train = train[feature]
X_test = test[feature]
X_train.head()
train = train.fillna('Zilch')
 important_key_words = train.groupby('keyword' ).agg({'text':np.size, 'target':np.mean} ).rename(columns={'text':'count', 'target':'frequency'} )<define_variables>
Y_train = train["count"]
Y_train.head()
additional_list = ['bushfires','evacuated','forestfire','hostages','rescuers','sinkhole','thunderstorm']<filter>
model = RandomForestRegressor(n_estimators=500)
 Y_train_log = np.log1p(Y_train)
 model.fit(X_train,Y_train_log)
 result = model.predict(X_test )
prob_disaster = 0.95
keyword_list_disaster95 = list(important_key_words[important_key_words['frequency']>prob_disaster].index)+ additional_list<filter>
np.exp(result )
numbers_of_95certain_disasters = test['id'][test.keyword.isin(keyword_list_disaster95)]<feature_engineering>
sub = pd.read_csv("/kaggle/input/bike-sharing-demand/sampleSubmission.csv")
 sub.head()
y_predicted = np.zeros(len(y_predicted_SVM))
for i in range(0,len(y_predicted_SVM)) :
if i in numbers_of_95certain_disasters:
y_predicted[i] = 1
else:
y_predicted[i] = y_predicted_SVM[i] 
<feature_engineering>
sub["count"] = np.exp(result)
 sub.head()
<save_to_csv><EOS> 
sub.to_csv("20_03_29sub.csv",index=False )