Kaggle competition: ASHRAE - Great Energy Predictor III
from nltk.corpus import stopwords
import re
import string<categorify>
%matplotlib inline
path_data = "/kaggle/input/ashrae-energy-prediction/"
path_train = path_data + "train.csv"
path_test = path_data + "test.csv"
path_building = path_data + "building_metadata.csv"
path_weather_train = path_data + "weather_train.csv"
path_weather_test = path_data + "weather_test.csv"
def change_contraction_verb(text):
text = re.sub(r"n't", " not", text)
 text = re.sub(r"'re", " are", text)
 text = re.sub(r"'s", " is", text)
 text = re.sub(r"'d", " would", text)
 text = re.sub(r"'ll", " will", text)
 text = re.sub(r"'t", " not", text)
 text = re.sub(r"'ve", " have", text)
 text = re.sub(r"'m", " am", text)
 text = re.sub(r"won't", "will not", text)
 text = re.sub(r"can't", "can not", text)
 return text
train['text'] = train['text'].apply(lambda x : change_contraction_verb(x))
test['text'] = test['text'].apply(lambda x : change_contraction_verb(x))
train['text'].head(10 )<categorify>
train_df = pd.read_csv(path_train)
 building_df = pd.read_csv(path_building)
 le = LabelEncoder()
building_df.primary_use = le.fit_transform(building_df.primary_use)
 weather_train_df = pd.read_csv(path_weather_train )
def custom_preprocessor(text):

 text = text.lower()
text = re.sub('\[.*?\]', '', text)
 text = re.sub("\\W"," ",text)
 text = re.sub('https?://\S+|www\.\S+', '', text)
 text = re.sub('<.*?>+', '', text)
 text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
 text = re.sub('
', '', text)
 text = re.sub('\w*\d\w*', '', text)
 return text
train['text'] = train['text'].apply(lambda x : custom_preprocessor(x))
test['text'] = test['text'].apply(lambda x : custom_preprocessor(x))
train['text'].head(10 )<drop_column>
def preparing_data(X, bld_data, wth_data, test=False):
X = X.merge(bld_data, on = "building_id", how = "left")
 X = X.merge(wth_data, on = ["site_id", "timestamp"], how = "left")
 X["timestamp"] = pd.to_datetime(X["timestamp"], format="%Y-%m-%d %H:%M:%S")
 X["square_feet"] = np.log1p(X["square_feet"])
 if not test:
X.sort_values("timestamp", inplace = True)
 X.reset_index(drop=True, inplace = True)
 X["hour"] = X.timestamp.dt.hour
X["weekday"] = X.timestamp.dt.weekday
holidays = ["2016-01-01", "2016-01-18", "2016-02-15", "2016-05-30", "2016-07-04","2016-09-05", "2016-10-10",
"2016-11-11", "2016-11-24", "2016-12-26", "2017-01-01", "2017-01-16", "2017-02-20", "2017-05-29",
"2017-07-04", "2017-09-04", "2017-10-09", "2017-11-10", "2017-11-23", "2017-12-25", "2018-01-01",
"2018-01-15", "2018-02-19", "2018-05-28", "2018-07-04", "2018-09-03", "2018-10-08", "2018-11-12",
"2018-11-22", "2018-12-25", "2019-01-01"]
X["isholiday"] =(X.timestamp.dt.date.astype("str" ).isin(holidays)).astype(int)
 X.drop("timestamp", axis=1, inplace=True)
 if test:
row_ids = X.row_id
X.drop("row_id", axis=1, inplace=True)
 return X, row_ids
else:
y = np.log1p(X.meter_reading)
 X.drop("meter_reading", axis=1, inplace=True)
 return X, y
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 train['text'] = train['text'].apply(lambda x : remove_emoji(x))
test['text'] = test['text'].apply(lambda x : remove_emoji(x))<string_transform>
X_full, y_full = preparing_data(train_df, building_df, weather_train_df)
 del train_df, weather_train_df
gc.collect()
stopwords = stopwords.words('english')
 print(stopwords )<feature_engineering>
categorical_features = ["building_id", "site_id", "meter", "primary_use", "hour", "weekday", "isholiday"]
X_half1 = X_full.iloc[:int(len(X_full)/2)]
X_half2 = X_full[int(len(X_full)/2):]
y_half1 = y_full[:int(len(X_full)/2)]
y_half2 = y_full[int(len(X_full)/2):]
lgb_train1_data = lgb.Dataset(X_half1, label=y_half1, categorical_feature=categorical_features, free_raw_data=False)
 lgb_train2_data = lgb.Dataset(X_half2, label=y_half2, categorical_feature=categorical_features, free_raw_data=False)
 parameters = {
"objective": "regression",
"boosting": "gbdt",
"num_leaves": 100,
"learning_rate": 0.05,
"feature_fraction": 0.80,
"reg_lambda": 2,
"metric": "rmse",
"n_jobs": 4
}
print("start building model1_1 : ")
 model1_1 = lgb.train(parameters, train_set=lgb_train1_data, num_boost_round=1000, valid_sets = [lgb_train1_data, lgb_train2_data],
verbose_eval=200, early_stopping_rounds=200)
 print("start building model1_2 : ")
 model1_2 = lgb.train(parameters, train_set=lgb_train2_data, num_boost_round=1000, valid_sets=[lgb_train2_data, lgb_train1_data],
verbose_eval=200, early_stopping_rounds=200 )
count_vectorizer = CountVectorizer(token_pattern=r'\w{1,}', ngram_range=(1, 2), stop_words = stopwords)
 train_vector = count_vectorizer.fit_transform(train['text'])
 test_vector = count_vectorizer.transform(test['text'] )<categorify>
del X_half1, X_half2, y_half1, y_half2, lgb_train1_data, lgb_train2_data, categorical_features
gc.collect()
train_vector.toarray()<import_modules>
test_full = pd.read_csv(path_test)
 weather_test = pd.read_csv(path_weather_test)
 test_full = reduce_mem_usage(test_full)
 weather_test = reduce_mem_usage(weather_test)
 X_test, row_ids = preparing_data(test_full, building_df, weather_test, test=True )
clf = LogisticRegression()
scores = model_selection.cross_val_score(clf, train_vector, train["target"], cv=5, scoring="accuracy")
 print(scores )<compute_test_metric>
del test_full, building_df, weather_test
gc.collect()
print("Accuracy of Model with Cross Validation is: ",scores.mean() * 100 )<train_model>
predict = np.expm1(model1_1.predict(X_test, num_iteration = model1_1.best_iteration)) / 2
del model1_1
gc.collect()
predict += np.expm1(model1_2.predict(X_test, num_iteration = model1_2.best_iteration)) / 2
del model1_2
gc.collect()
<save_to_csv><EOS> 
submission = pd.DataFrame({"row_id": row_ids, "meter_reading": np.clip(predict, 0, a_max=None)})
 submission.to_csv("submission.csv", index=False)