基于机器学习的风力涡轮机输出功率异常检测(Python)

194 篇文章 1 订阅
155 篇文章 5 订阅

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()

Windanomaly
Dat/Zeit
2016-01-01 00:10:005.8-1
2016-01-01 00:20:005.8-1
2016-01-01 00:30:005.8-1
2016-01-01 00:40:006.4-1
2016-01-01 00:50:006.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》等审稿专家,擅长领域:信号滤波/降噪,机器学习/深度学习,时间序列预分析/预测,设备故障诊断/缺陷检测/异常检测。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哥廷根数学学派

码字不易,且行且珍惜

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值