Created
August 29, 2019 13:25
-
-
Save ground0state/881be629ab2ba6fc772b1f6d480973b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import lightgbm as lgb | |
from sklearn.model_selection import * | |
!wget "https://www.analyticsvidhya.com/wp-content/uploads/2016/02/AirPassengers.csv" | |
df = pd.read_csv('AirPassengers.csv', index_col=0) | |
f_ori = df.values | |
f = f_ori | |
length_of_sequences = len(df) | |
width = 12 | |
data = [] | |
target = [] | |
for i in range(0, length_of_sequences - width + 1): | |
data.append(f[i: i + width]) | |
target.append(f[i + width - 1]) | |
X = np.stack(data, axis=0).reshape(-1, width) | |
y = np.array(target).reshape(len(data), ) | |
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size=0.1) | |
lgb_train = lgb.Dataset(X_train, y_train) | |
lgb_eval = lgb.Dataset(X_validation, y_validation, reference=lgb_train) | |
# LightGBM parameters | |
params = { | |
'task' : 'train', | |
'boosting':'gbdt', | |
'objective' : 'regression', # 7.0713 | |
'metric' : {'mae'}, | |
'num_leaves':78, | |
'max_drop':20, # 5 | |
'drop_rate':0.05, # 0.01 | |
'learning_rate':0.01, | |
'feature_fraction':0.4, # 0.7 | |
'bagging_fraction':0.4, # 0.85 | |
'bagging_freq':30, | |
'lambda_l1':1, # 1 | |
'lambda_l2':0.01, # 0.01 | |
'seed':0, | |
'verbose':0, | |
'device': 'cpu' | |
} | |
evaluation_results = {} | |
gbm = lgb.train(params, | |
lgb_train, | |
num_boost_round=100000, | |
valid_sets=[lgb_train, lgb_eval], | |
valid_names=['Train', 'Test'], | |
evals_result=evaluation_results, | |
early_stopping_rounds=3000, | |
verbose_eval=100) | |
y_pred = gbm.predict(X_validation, num_iteration=gbm.best_iteration) | |
# widthブロックの1個目を取得 | |
Z = X[:1] # X[0]としないのはshapeを(1, width)とするため | |
original = [f[i] for i in range(width)] | |
predicted = [None for i in range(width)] | |
for i in range(length_of_sequences - width + 1): | |
z_ = Z[-1:] # shape=(1, width, 1) | |
y_ = gbm.predict(z_, num_iteration=gbm.best_iteration) | |
sequence_ = np.concatenate((z_.reshape(width)[1:], y_), axis=0).reshape(1, width) | |
Z = np.append(Z, sequence_, axis=0) | |
predicted.append(y_.reshape(-1)) | |
plt.plot(original, linestyle='dashed', color='black') | |
plt.plot(predicted, color='black') | |
# trainig dataに対する予測 | |
pred_train = np.array([None for i in range(width)]) | |
pred_train = np.append(pred_train, gbm.predict(X), axis=0) | |
plt.plot(f, linestyle='dashed', color='green') | |
plt.plot(original, linestyle='dashed', color='black') | |
plt.plot(pred_train, color='black') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment