Data Processing
from lightgbm import LGBMRegressor
from DataProcessor import DataProcessor
from Trainer import Trainer, get_MAPE, get_SMAPE
from AnomalyDetector import AnomalyDetector
# choose ML model for time series
MODEL = LGBMRegressor(random_state=42)
PARAM_GRID = {
'num_leaves': [16, 24, 31],
'learning_rate': [0.005, 0.01, 0.05],
'n_estimators': [50, 100, 150],
'subsample': [0.8, 1.0],
'colsample_bytree': [0.8, 1.0],
}
Turbine = DataProcessor('Turbine1.csv', 'Turbine2.csv')
# Clean and reorder data for Turbine1 and Turbine2
Turbine.df1 = Turbine.clean(Turbine.df1)
Turbine.df2 = Turbine.clean(Turbine.df2)
# Create additional time series features
Turbine.df1 = Turbine.add_features(Turbine.df1)
Turbine.df2 = Turbine.add_features(Turbine.df2)
# Combine and aggregate the data
combined_data = Turbine.aggregate()
combined_data.describe()
Prediction
One Feature
# initiate Trainer class
trainer = Trainer(combined_data)
# Single feature selection
trainer.features = ["Wind"]
# Split data into train and test sets
x_train, x_test, y_train, y_test = trainer.data_splitter()
# fine tune via GridSearch
MODEL = trainer.tune(PARAM_GRID, MODEL, x_train, y_train)
# # Train and evaluate the model
lgb_model, MAE_scores = trainer.train(MODEL, x_train, y_train)
sampe, mape, mae = trainer.evaluate(lgb_model, x_test, y_test)
print('cross validated MAE: %.3f ' % (MAE_scores.mean()))
print('-' * 50)
print("Test set MAPE:", mape)
print("Test set SMAPE:", sampe)
print("Test set MAE:", mae)
[LightGBM] [Info] Auto-choosing row-wise multi-threading, the overhead of testing was 0.000044 seconds.
You can set `force_row_wise=true` to remove the overhead.
And if memory is not enough, you can set `force_col_wise=true`.
[LightGBM] [Info] Total Bins 255
[LightGBM] [Info] Number of data points in the train set: 10482, number of used features: 1
[LightGBM] [Info] Start training from score 444.713280
cross validated MAE: 30.749
--------------------------------------------------
Test set MAPE: 3.442851448511671
Test set SMAPE: 0.30933055946116356
Test set MAE: 27.995101200373472
Multi-Features
# initiate Trainer class
trainer = Trainer(combined_data)
# Auto feature selection
trainer.features = trainer.select_features(n= 12)
# fine tune via GridSearch
tuned_model = trainer.tune(PARAM_GRID, MODEL, x_train, y_train)
Fitting 5 folds for each of 108 candidates, totalling 540 fits
[LightGBM] [Warning] Found whitespace in feature_names, replace with underlines
[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.000826 seconds.
You can set `force_col_wise=true` to remove the overhead.
[LightGBM] [Info] Total Bins 2964
[LightGBM] [Info] Number of data points in the train set: 10482, number of used features: 12
[LightGBM] [Info] Start training from score 444.713280
tuned_model
LGBMRegressor(colsample_bytree=0.8, learning_rate=0.05, n_estimators=150,
random_state=42, subsample=0.8)
# Train and evaluate the model
lgb_model, MAE_scores = trainer.train(MODEL, x_train, y_train)
sampe, mape, mae = trainer.evaluate(lgb_model, x_test, y_test)
print('cross validated MAE: %.3f ' % (MAE_scores.mean()))
print('-' * 50)
print("Test set MAPE:", mape)
print("Test set SMAPE:", sampe)
print("Test set MAE:", mae)
[LightGBM] [Warning] Found whitespace in feature_names, replace with underlines
[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.000976 seconds.
You can set `force_col_wise=true` to remove the overhead.
[LightGBM] [Info] Total Bins 2964
[LightGBM] [Info] Number of data points in the train set: 10482, number of used features: 12
[LightGBM] [Info] Start training from score 444.713280
cross validated MAE: 7.913
--------------------------------------------------
Test set MAPE: 0.2291028562043878
Test set SMAPE: 0.1711896467697045
Test set MAE: 4.787762103651617
Anomaly Detection
from AnomalyDetector import AnomalyDetector
# Instantiate AnomalyDetector
anomaly_detector = AnomalyDetector(Turbine.df1)
# Fit the Isolation Forest model
anomaly_detector.fit_model()
# Detect anomalies
anomaly_table = anomaly_detector.detect_anomalies()
anomaly_detector.data[['Wind', 'anomaly']][anomaly_detector.data['anomaly']==-1].head()
Wind | anomaly | |
Dat/Zeit | ||
2016-01-01 00:10:00 | 5.8 | -1 |
2016-01-01 00:20:00 | 5.8 | -1 |
2016-01-01 00:30:00 | 5.8 | -1 |
2016-01-01 00:40:00 | 6.4 | -1 |
2016-01-01 00:50:00 | 6.9 | -1 |
# Visualize anomalies
anomaly_detector.visualize_anomalies(until_date= '2016-3-31')
t-SNE Feature Visualization
# show t-SNE
anomaly_detector.visualize_tsne()
知乎学术咨询:https://www.zhihu.com/consult/people/792359672131756032?isMe=1
担任《Mechanical System and Signal Processing》等审稿专家,擅长领域:信号滤波/降噪,机器学习/深度学习,时间序列预分析/预测,设备故障诊断/缺陷检测/异常检测。