Kaggle competition: Bike Sharing Demand
test_pred = model.predict(test_input )<save_to_csv>
train = pd.read_csv(".. /input/train.csv", parse_dates = ["datetime"])
 test = pd.read_csv(".. /input/test.csv", parse_dates = ["datetime"] )
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )<set_options>
train["datetime"].dt.hour
%matplotlib inline
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
train["datetime"].dt.hour.value_counts()
sample_submission = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv")
 train = pd.read_csv(".. /input/nlp-getting-started/train.csv")
 test = pd.read_csv(".. /input/nlp-getting-started/test.csv" )<categorify>
train["year"] = train["datetime"].dt.year
train["hour"] = train["datetime"].dt.hour
train["dayofweek"] = train["datetime"].dt.dayofweek
def text_cleaning_hyperlink(text):
return re.sub(r"http\S+","",text )<categorify>
test["year"] = test["datetime"].dt.year
test["hour"] = test["datetime"].dt.hour
test["dayofweek"] = test["datetime"].dt.dayofweek
def text_cleaning_punctuation(text):
translator = str.maketrans('', '', string.punctuation)
 return text.translate(translator )<string_transform>
train["datetime"].dt.month.value_counts()
def text_cleaning_stopwords(text):
stop_words = set(stopwords.words('english'))
word_token = word_tokenize(text)
 filtered_sentence = [w for w in word_token if not w in stop_words]
return ' '.join(filtered_sentence)
<string_transform>
train["season"].value_counts()
def text_cleaning_lowercase(text):
return text.lower()<string_transform>
train["datetime"].dt.day.value_counts()
def text_extract(text_lst):
txt = []
for i,x in enumerate(text_lst):
for j,p in enumerate(x):
txt.append(p)
 return txt 
<string_transform>
test["datetime"].dt.day.value_counts()
def remove_digits(txt):
no_digits = ''.join(i for i in txt if not i.isdigit())
 return no_digits<feature_engineering>
y_casual = np.log1p(train.casual)
 y_registered = np.log1p(train.registered )
train.keyword = train.keyword.apply(lambda x: text_cleaning_stopwords(text_cleaning_punctuation(text_cleaning_hyperlink(remove_digits(x.lower())))) if type(x)== str else x )<feature_engineering>
train.drop(["datetime", "windspeed", "casual", "registered", "count"], 1)
 test.drop(["datetime", "windspeed"], 1 )
train.text = train.text.apply(lambda x: text_cleaning_stopwords(text_cleaning_punctuation(text_cleaning_hyperlink(remove_digits(x.lower())))))
 train.text = train.text.apply(lambda x: list(set(x.split(' '))))<feature_engineering>
from lightgbm import LGBMRegressor
<feature_engineering>
model = LGBMRegressor()
train['k_t'] = train.apply(lambda x : x['text'] + [x['keyword']] if type(x['keyword'])== str else x['text'], axis=1 )<string_transform>
train = pd.read_csv(".. /input/train.csv", parse_dates = ["datetime"])
 test = pd.read_csv(".. /input/test.csv", parse_dates = ["datetime"])
 train["year"] = train["datetime"].dt.year
train["hour"] = train["datetime"].dt.hour
train["dayofweek"] = train["datetime"].dt.dayofweek
test["year"] = test["datetime"].dt.year
test["hour"] = test["datetime"].dt.hour
test["dayofweek"] = test["datetime"].dt.dayofweek
y_casual = np.log1p(train.casual)
 y_registered = np.log1p(train.registered)
 train.drop(["datetime", "windspeed", "casual", "registered", "count"], 1, inplace=True)
 test.drop(["datetime", "windspeed", ], 1, inplace=True )
ps_1 = PorterStemmer()
wnl_1 = WordNetLemmatizer()
text_reconstruct = []
for i,x in enumerate(train.k_t.values):
try:
a = wnl_1.lemmatize(ps_1.stem(x))
except AttributeError:
a = list(set([wnl_1.lemmatize(ps_1.stem(word)) for j,word in enumerate(x)]))
text_reconstruct.append(a )<string_transform>
model.fit(train, y_casual )
train.k_t = text_reconstruct
train_word = train.k_t.apply(lambda x: ' '.join(x))<feature_engineering>
preds1 = model.predict(test )
test.keyword = test.keyword.apply(lambda x: text_cleaning_stopwords(text_cleaning_punctuation(text_cleaning_hyperlink(remove_digits(x.lower())))) if type(x)== str else x )<feature_engineering>
model.fit(train, y_registered)
 preds2 = model.predict(test )
test.text = test.text.apply(lambda x: text_cleaning_stopwords(text_cleaning_punctuation(text_cleaning_hyperlink(remove_digits(x.lower())))))
 test.text = test.text.apply(lambda x: list(set(x.split(' '))))<feature_engineering>
submission = pd.read_csv(".. /input/sampleSubmission.csv" )
test['k_t'] = test.apply(lambda x : x['text'] + [x['keyword']+x['keyword']+x['keyword']] if type(x['keyword'])== str else x['text'],axis=1 )<string_transform>
submission["count"] = np.expm1(preds1)+ np.expm1(preds2)
 submission.to_csv("result.csv", index=False )
ps_2 = PorterStemmer()
wnl_2 = WordNetLemmatizer()
text_reconstruct_test = []
for i,x in enumerate(test.k_t.values):
try:
a = wnl_2.lemmatize(ps_2.stem(x))
except AttributeError:
a = list(set([wnl_2.lemmatize(ps_2.stem(word)) for j,word in enumerate(x)]))
text_reconstruct_test.append(a )<string_transform>
train = pd.read_csv(".. /input/train.csv" )