Kaggle competition: ASHRAE - Great Energy Predictor III
<SOS> metric: RMSLE Kaggle data source: ashrae-great-energy-predictor-iii<compute_test_metric>
import os, gc
import datetime
import numpy as np
import pandas as pd
import category_encoders
from sklearn.impute import SimpleImputer
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import Lasso
from sklearn.linear_model import Ridge
from lightgbm import LGBMRegressor
from mlxtend.regressor import StackingRegressor
from pandas.api.types import is_categorical_dtype
from pandas.api.types import is_datetime64_any_dtype as is_datetime
score_metrics(y_test, y_predicted_dt )<install_modules>
PATH = '.. /input/ashrae-energy-prediction/'
!pip install gensim -i http://pypi.douban.com/simple --trusted-host pypi.douban.com<string_transform>
train = pd.read_csv(f'{PATH}train.csv')
 weather_train = pd.read_csv(f'{PATH}weather_train.csv')
 metadata = pd.read_csv(f'{PATH}building_metadata.csv' )
stop_words = stopwords.words('english')
 for word in ['us','no','yet']:
stop_words.append(word)
 data_list = []
text_series = data['text']
for i in range(len(text_series)) :
content = text_series.iloc[i]
cutwords = [word for word in content.split(' ')if word not in stop_words if len(word)!= 0]
data_list.append(cutwords )<define_variables>
train = train[train['building_id'] != 1099]
train = train.query('not(building_id <= 104 & meter == 0 & timestamp <= "2016-05-20")' )
for i in range(len(data_list)) :
content = data_list[i]
if len(content)<1:
print(i )<load_pretrained>
def weather_data_parser(weather_data)-> pd.DataFrame:
time_format = '%Y-%m-%d %H:%M:%S'
start_date = datetime.datetime.strptime(weather_data['timestamp'].min() , time_format)
 end_date = datetime.datetime.strptime(weather_data['timestamp'].max() , time_format)
 total_hours = int(((end_date - start_date ).total_seconds() + 3600)/ 3600)
 hours_list = [(end_date - datetime.timedelta(hours=x)).strftime(time_format)for x in range(total_hours)]
for site_id in range(16):
site_hours = np.array(weather_data[weather_data['site_id'] == site_id]['timestamp'])
 new_rows = pd.DataFrame(np.setdiff1d(hours_list, site_hours), columns=['timestamp'])
 new_rows['site_id'] = site_id
weather_data = pd.concat([weather_data, new_rows], sort=True)
 weather_data = weather_data.reset_index(drop=True)
 weather_data['datetime'] = pd.to_datetime(weather_data['timestamp'])
 weather_data['day'] = weather_data['datetime'].dt.day
weather_data['week'] = weather_data['datetime'].dt.week
weather_data['month'] = weather_data['datetime'].dt.month
weather_data = weather_data.set_index(['site_id', 'day', 'month'])
 air_temperature_filler = pd.DataFrame(weather_data.groupby(['site_id','day','month'])['air_temperature'].median() , columns=['air_temperature'])
 weather_data.update(air_temperature_filler, overwrite=False)
 cloud_coverage_filler = weather_data.groupby(['site_id', 'day', 'month'])['cloud_coverage'].median()
cloud_coverage_filler = pd.DataFrame(cloud_coverage_filler.fillna(method='ffill'), columns=['cloud_coverage'])
 weather_data.update(cloud_coverage_filler, overwrite=False)
 due_temperature_filler = pd.DataFrame(weather_data.groupby(['site_id','day','month'])['dew_temperature'].median() , columns=['dew_temperature'])
 weather_data.update(due_temperature_filler, overwrite=False)
 sea_level_filler = weather_data.groupby(['site_id','day','month'])['sea_level_pressure'].median()
sea_level_filler = pd.DataFrame(sea_level_filler.fillna(method='ffill'), columns=['sea_level_pressure'])
 weather_data.update(sea_level_filler, overwrite=False)
 wind_direction_filler = pd.DataFrame(weather_data.groupby(['site_id','day','month'])['wind_direction'].median() , columns=['wind_direction'])
 weather_data.update(wind_direction_filler, overwrite=False)
 wind_speed_filler = pd.DataFrame(weather_data.groupby(['site_id','day','month'])['wind_speed'].median() , columns=['wind_speed'])
 weather_data.update(wind_speed_filler, overwrite=False)
 precip_depth_filler = weather_data.groupby(['site_id','day','month'])['precip_depth_1_hr'].median()
precip_depth_filler = pd.DataFrame(precip_depth_filler.fillna(method='ffill'), columns=['precip_depth_1_hr'])
 weather_data.update(precip_depth_filler, overwrite=False)
 weather_data = weather_data.reset_index()
weather_data = weather_data.drop(['datetime','day','week','month'], axis=1)
 return weather_data
word2vec_path='./GoogleNews-vectors-negative300.bin.gz'
word2vec_model = gensim.models.KeyedVectors.load_word2vec_format(word2vec_path, binary=True )<feature_engineering>
weather_train = weather_data_parser(weather_train )
def get_textVector(data_list, word2vec, textsVectors_list):
for i in range(len(data_list)) :
words_perText = data_list[i]
if len(words_perText)< 1:
words_vector = [np.zeros(300)]
else:
words_vector = [word2vec.wv[k] if k in word2vec_model else np.zeros(300)for k in words_perText]
text_vector = np.array(words_vector ).mean(axis=0)
 textsVectors_list.append(text_vector)
 return textsVectors_list<prepare_x_and_y>
train = reduce_mem_usage(train, use_float16=True)
 weather_train = reduce_mem_usage(weather_train, use_float16=True)
 metadata = reduce_mem_usage(metadata, use_float16=True )
textsVectors_list = []
get_textVector(data_list, word2vec_model, textsVectors_list)
 X = np.array(textsVectors_list )<count_missing_values>
train = train.merge(metadata, on='building_id', how='left')
 train = train.merge(weather_train, on=['site_id', 'timestamp'], how='left')
 del weather_train; gc.collect()
pd.isnull(X ).any()<split>
def data_parser(data)-> pd.DataFrame:
data.sort_values('timestamp')
 data.reset_index(drop=True)
 data['timestamp'] = pd.to_datetime(data['timestamp'], format='%Y-%m-%d %H:%M:%S')
 data['weekday'] = data['timestamp'].dt.weekday
data['hour'] = data['timestamp'].dt.hour
data['square_feet'] = np.log1p(data['square_feet'])
 data = data.drop(['timestamp', 'sea_level_pressure',
'wind_direction', 'wind_speed', 'year_built', 'floor_count'], axis=1)
 gc.collect()
encoder = LabelEncoder()
data['primary_use'] = encoder.fit_transform(data['primary_use'])
 return data
word2vec_X = X[0:train_data.shape[0]]
y = data['target'][0:train_data.shape[0]]
word2vec_submit = X[train_data.shape[0]:-1]
X_train_word2vec, X_test_word2vec, y_train_word2vec, y_test_word2vec = train_test_split(word2vec_X, y,
test_size = 0.2, random_state = 4 )<train_model>
train = data_parser(train )
word2vec_lr = LogisticRegression(class_weight = 'balanced', solver = 'lbfgs', n_jobs = -1)
 word2vec_lr.fit(X_train_word2vec, y_train_word2vec)
 y_predicted_word2vec_lr = word2vec_lr.predict(X_test_word2vec )<compute_test_metric>
target = np.log1p(train['meter_reading'])
 features = train.drop(['meter_reading'], axis = 1)
 del train; gc.collect()
score_metrics(y_test_word2vec, y_predicted_word2vec_lr )<define_variables>
categorical_features = ['building_id', 'site_id', 'meter', 'primary_use']
encoder = category_encoders.CountEncoder(cols=categorical_features)
 encoder.fit(features)
 features = encoder.transform(features)
 features_size = features.shape[0]
for feature in categorical_features:
features[feature] = features[feature] / features_size
compare_list = []
for(i,j)in zip(y_test_word2vec, y_predicted_word2vec_lr):
k = i - j
compare_list.append(k)
 wrong_num = [i for i,j in enumerate(compare_list)if j != 0]
text_series[0:train_data.shape[0]][wrong_num]<define_variables>
imputer = SimpleImputer(missing_values=np.nan, strategy='median')
 imputer.fit(features)
 features = imputer.transform(features )
lenlen = []
for i in range(len(data_list)) :
content = data_list[i]
perlen = len(content)
 lenlen.append(perlen)
 print(max(lenlen))<define_variables>
lightgbm = LGBMRegressor(objective='regression', learning_rate=0.05, num_leaves=1024,
feature_fraction=0.8, bagging_fraction=0.9, bagging_freq=5)
 ridge = Ridge(alpha=0.3)
 lasso = Lasso(alpha=0.3 )
max_sequence_length = 26
embedding_dim = 300<train_model>
kfold = KFold(n_splits=2, shuffle=False)
 models = []
for idx,(train_idx, val_idx)in enumerate(kfold.split(features)) :
train_features, train_target = features[train_idx], target[train_idx]
val_features, val_target = features[val_idx], target[val_idx]
model = StackingRegressor(regressors=(lightgbm, ridge, lasso),
meta_regressor=lightgbm, use_features_in_secondary=True)
 model.fit(np.array(train_features), np.array(train_target))
models.append(model)
 print('RMSE: {:.4f} of fold: {}'.format(
 np.sqrt(mean_squared_error(val_target, model.predict(np.array(val_features)))) , idx))
del train_features, train_target, val_features, val_target; gc.collect()
del features, target; gc.collect()
tokenizer = Tokenizer()
tokenizer.fit_on_texts(data_list)
 sequences = tokenizer.texts_to_sequences(data_list)
 word_index = tokenizer.word_index
cnn_data = pad_sequences(sequences, maxlen = max_sequence_length)
 cnn_label = to_categorical(np.asarray(train_data['target']))
print('len of word_index:', len(word_index))
print('shape of data tensor:', cnn_data.shape)
 print('shape of label tensoe:', cnn_label.shape )<split>
test = pd.read_csv(f'{PATH}test.csv')
 weather_test = pd.read_csv(f'{PATH}weather_test.csv' )
trainCNN_data = cnn_data[0:train_data.shape[0]]
X_train_cnn, X_test_cnn, y_train_cnn, y_test_cnn = train_test_split(trainCNN_data, cnn_label,
test_size = 0.2, random_state = 4)
 X_cnn, X_val_cnn, y_cnn, y_val_cnn = train_test_split(X_train_cnn, y_train_cnn,
test_size = 0.2, random_state = 4 )<choose_model_class>
row_ids = test['row_id']
test.drop('row_id', axis=1, inplace=True )
CNNmodel = Sequential()
CNNmodel.add(Embedding(len(word_index)+1, embedding_dim, input_length = max_sequence_length))
CNNmodel.add(Conv1D(filters=250, kernel_size=3, strides=1, padding='valid', activation = 'relu'))
CNNmodel.add(MaxPooling1D(pool_size=3))
CNNmodel.add(Flatten())
 CNNmodel.add(Dense(embedding_dim, activation='relu'))
CNNmodel.add(Dropout(0.8))
CNNmodel.add(Dense(cnn_label.shape[1], activation='sigmoid'))
CNNmodel.summary()<train_model>
weather_test = weather_data_parser(weather_test )
CNNmodel.compile(optimizer='adam', loss=losses.binary_crossentropy, metrics=['accuracy'])
 history = CNNmodel.fit(X_cnn, y_cnn, epochs=3, validation_data=(X_val_cnn, y_val_cnn))<compute_test_metric>
test = reduce_mem_usage(test, use_float16=True)
 weather_test = reduce_mem_usage(weather_test, use_float16=True )
test_loss, test_acc = CNNmodel.evaluate(X_test_cnn, y_test_cnn, verbose=2)
 print('test loss:',test_loss)
 print('test acc:',test_acc )<prepare_x_and_y>
test = test.merge(metadata, on='building_id', how='left')
 test = test.merge(weather_test, on=['site_id', 'timestamp'], how='left')
 del metadata; gc.collect()
embedding_matrix = np.zeros(( len(word_index)+ 1, embedding_dim))
for word, i in word_index.items() :
if word in word2vec_model:
embedding_matrix[i] = np.asarray(word2vec_model.wv[word] )<choose_model_class>
test = data_parser(test)
 test = encoder.transform(test)
 for feature in categorical_features:
test[feature] = test[feature] / features_size
test = imputer.transform(test )
embedding_layer = Embedding(len(word_index)+1,
embedding_dim,
weights = [embedding_matrix],
input_length = max_sequence_length,
trainable = False )<choose_model_class>
predictions = 0
for model in models:
predictions += np.expm1(model.predict(np.array(test)))/ len(models)
 del model; gc.collect()
del test, models; gc.collect()
<train_model><EOS> 
submission = pd.DataFrame({
'row_id': row_ids,
'meter_reading': np.clip(predictions, 0, a_max=None)
 })
 submission.to_csv('submission.csv', index=False, float_format='%.4f' )