基于lightgbm hyperopt的旋轉機械故障診斷(Python)

前置文章:

將一維機械振動信號構造為訓練集和測試集(Python)

https://mp.weixin.qq.com/s/DTKjBo6_WAQ7bUPZEdB1TA

旋轉機械振動信號特征提取(Python)

https://mp.weixin.qq.com/s/VwvzTzE-pacxqb9rs8hEVw

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.colors import ListedColormap
import matplotlib.patches as mpatches
import lightgbm as lgb
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
df_train = pd.read_csv("statistics_10_train.csv" , sep = ',')
df_test = pd.read_csv("statistics_10_test.csv" , sep = ',')
X_train = df_train[['Kurtosis', 'Impulse factor', 'RMS', 'Margin factor', 'Skewness','Shape factor', 'Peak to peak', 'Crest factor']].values
y_train = df_train['Tipo'].values
X_test = df_test[['Kurtosis', 'Impulse factor', 'RMS', 'Margin factor', 'Skewness','Shape factor', 'Peak to peak', 'Crest factor']].values
y_test = df_test['Tipo'].values
from hyperopt import fmin, atpe, tpe, STATUS_OK, STATUS_FAIL, Trials
from hyperopt import hp
from hyperopt import space_eval
class HPOpt(object):def __init__(self, x_train, x_test, y_train, y_test):self.x_train = x_trainself.x_test  = x_testself.y_train = y_trainself.y_test  = y_testdef process(self, fn_name, space, trials, algo, max_evals):fn = getattr(self, fn_name)try:result = fmin(fn=fn, space=space, algo=algo, max_evals=max_evals, trials=trials)except Exception as e:return {'status': STATUS_FAIL,'exception': str(e)}return result, trialsdef lgb_clas(self, para):clf = lgb.LGBMClassifier(**para['clas_params'])return self.train_clf(clf, para)def train_clf(self, clf, para):clf.fit(self.x_train, self.y_train,eval_set=[(self.x_train, self.y_train), (self.x_test, self.y_test)], verbose = False, early_stopping_rounds = 20)pred = clf.predict(self.x_test)loss = para['loss_func'](self.y_test, pred)return {'loss': loss, 'status': STATUS_OK}
from sklearn.metrics import accuracy_score
lgb_clas_params = {'learning_rate':    hp.choice('learning_rate',    np.arange(0.001, 0.5, 0.001)),'max_depth':        hp.choice('max_depth',        np.arange(5, 10, 1, dtype=int)),'min_child_weight': hp.choice('min_child_weight', np.arange(0, 10, 1)),'min_data_in_leaf': hp.choice('min_data_in_leaf', np.arange(0, 10, 1)),'subsample':        hp.choice('subsample',        np.arange(0.1, 1, 0.05)),'n_estimators':     hp.choice('n_estimators',     np.arange(10, 200, 10, dtype=int)),'num_leaves':       hp.choice('num_leaves',       np.arange(5, 51, 1, dtype=int)),}lgb_para = dict()
lgb_para['clas_params'] = lgb_clas_params
lgb_para['loss_func' ] = lambda y, pred: accuracy_score(y, pred)# squared = False)
lgb_para["max_evals"] = 100
# Optimización 
obj = HPOpt(X_train, X_test, y_train, y_test)lgb_opt = obj.process(fn_name='lgb_clas', space=lgb_para, trials=Trials(), algo=tpe.suggest, max_evals=lgb_para["max_evals"])
parametros = space_eval(lgb_clas_params, lgb_opt[0])
clf = lgb.LGBMClassifier()
clf.set_params(**parametros) 
clf.fit(X_train, y_train)
LGBMClassifier(learning_rate=0.342, max_depth=9, min_child_weight=0,min_data_in_leaf=7, n_estimators=90, num_leaves=33,subsample=0.15000000000000002)
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
pred = clf.predict(X_test)
print(confusion_matrix(y_test, pred))
print(classification_report(y_test, pred))
[[27  3  0][ 0 30  0][ 0  1 29]]precision    recall  f1-score   supportInner       1.00      0.90      0.95        30Outer       0.88      1.00      0.94        30Sano       1.00      0.97      0.98        30accuracy                           0.96        90macro avg       0.96      0.96      0.96        90
weighted avg       0.96      0.96      0.96        90
clf = lgb.LGBMClassifier(n_estimators = 100, learning_rate = 0.01, min_data_in_leaf = 0)
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
target_names = ['Inner', 'Outer', 'Healthy']
print(confusion_matrix(y_test, pred))
print(classification_report(y_test, pred, target_names = target_names))
[[29  1  0][ 0 30  0][ 0  3 27]]precision    recall  f1-score   supportInner       1.00      0.97      0.98        30Outer       0.88      1.00      0.94        30Healthy       1.00      0.90      0.95        30accuracy                           0.96        90macro avg       0.96      0.96      0.96        90
weighted avg       0.96      0.96      0.96        90
pred_train = clf.predict(X_train)
print(confusion_matrix(y_train, pred_train))
print(classification_report(y_train, pred_train, target_names = target_names))知乎學術咨詢:
https://www.zhihu.com/consult/people/792359672131756032?isMe=1

工學博士,擔任《Mechanical System and Signal Processing》《中國電機工程學報》《控制與決策》等期刊審稿專家,擅長領域:現代信號處理,機器學習,深度學習,數字孿生,時間序列分析,設備缺陷檢測、設備異常檢測、設備智能故障診斷與健康管理PHM等。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/37217.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/37217.shtml
英文地址,請注明出處:http://en.pswp.cn/web/37217.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python變量的命名規則與賦值方式

第二章:Python 基礎語法 第一節:變量的命名規則與賦值方式 2.1.1 引言 在編程中,變量是存儲數據的基本單元。變量的命名和賦值是編程語言中表達和操作數據的基礎。了解和遵循變量命名規則對于編寫清晰、可維護的代碼至關重要。 2.1.2 變量…

【linux】網絡基礎(1)

文章目錄 網絡基本概念網絡的定義網絡的類型局域網(LAN)廣域網(WAN) 網絡協議OSI七層模型TCP/IP模型TCP/IP模型的結構 網絡傳輸的基本流程計算機與計算機之間的通信計算機的信息處理封裝報頭 網絡基本概念 網絡的定義 1.網絡是指…

專題一: Spring生態初探

咱們先從整體脈絡上看下Spring有哪些模塊,重要的概念有個直觀印象。 從Spring框架的整體架構和組成對整體框架有個認知。 Spring框架基礎概念 Spring基礎 - Spring和Spring框架組成 上圖是從官網4.2.x獲取的原圖,目前我們使用最廣法的版本應該都是5.x&am…

GitHub每日最火火火項目(6.30)

項目名稱:modelscope / DiffSynth - Studio 項目介紹:該項目致力于讓用戶體驗擴散模型的神奇魅力。擴散模型是一種具有廣泛應用前景的技術,在圖像生成、音頻處理等領域展現出了強大的能力。通過DiffSynth - Studio,用戶可以深入探…

Arrays.asList 和 java.util.ArrayList 區別

理解 Java 中的 Arrays.asList 和 java.util.ArrayList 的區別 在 Java 編程中,Arrays.asList 方法和 java.util.ArrayList 是兩種常用的處理列表數據的方式。雖然它們在功能上看起來相似,但在內部實現和使用上有著本質的不同。本文將探討這兩種方式的區…

一區算法MPA|海洋捕食者算法原理及其代碼實現(Matlab/Python))

Matlab/Python: 本文KAU將介紹一個2020年發表在1區期刊ESWA上的優化算法——海洋捕食者算法 (Marine Predators Algorithm,MPA)[1] 該算法由Faramarzi等于2020年提出,其靈感來源于海洋捕食者之間不同的覓食策略、最佳相遇概率策略、海洋記…

【Linux】IO多路復用——select,poll,epoll的概念和使用,三種模型的特點和優缺點,epoll的工作模式

文章目錄 Linux多路復用1. select1.1 select的概念1.2 select的函數使用1.3 select的優缺點 2. poll2.1 poll的概念2.2 poll的函數使用2.3 poll的優缺點 3. epoll3.1 epoll的概念3.2 epoll的函數使用3.3 epoll的優點3.4 epoll工作模式 Linux多路復用 IO多路復用是一種操作系統的…

MCU復位時GPIO是什么狀態?

大家一定遇到過上電或者復位時外部的MOS電路或者芯片使能信號意外開啟,至此有經驗的工程師就會經常關心一個問題,MCU復位時GPIO是什么狀態?什么電路需要外部加上下拉? MCU從上電到啟動,實際可分為復位前和復位后、初始…

【WPF】Windows系統桌面應用程序編程開發新手入門-打造自己的小工具

電腦Windows系統上的桌面程序通常是用Visual Studio 開發工具編寫出來的,有兩種開發方式供選擇,一種是WindowForm,簡稱WinForm,另一種是Windows Presentation Foundation,簡稱WPF,這里將學習WPF項目。 文章…

大物3錯題整理

平衡位置:在O點上的位置 相位: 當N很大的時候,wxwywz。因此,平均平動動能除以3,就是能量均分定理。 W F在x上的積分 Π時無單位 180,就是單位 1rad,rad就是單位 左手定則、右手定則、安培定…

C++模板類與繼承

1)模板類繼承普通類(常見)。 2)普通類繼承模板類的實例化版本。 3)普通類繼承模板類。(常見) 4)模板類繼承模板類。 5)模板類繼承模板參數給出的基類(不能是模板類)。 示…

【抽代復習筆記】24-群(十八):循環群的兩道例題

例1:證明: (1)三次交錯群A3是循環群,它與(Z3,)同構,其中Z3 {[0],[1],[2]}; (2)G {1,i,-1,-i},G上的代數運算是數的乘法,則G是一個循環群&…

如何解決三菱軟件提示 起動MELSOFT Mediative Server失敗

前言: 注意,這篇文章僅針對如何解決 起動MELSOFT Mediative Server失敗 的問題。對于其他相關的問題,請搜索其他相應的解決辦法。 本人是在重裝三菱GX Works軟件時遇到此問題的。后來搜索發現無人能妥善的關閉這個提示。因此本文介紹如何關…

【Web3項目案例】Ethers.js極簡入門+實戰案例:實現ERC20協議代幣查詢、交易

蘇澤 大家好 這里是蘇澤 一個鐘愛區塊鏈技術的后端開發者 本篇專欄 ←持續記錄本人自學智能合約學習筆記和經驗總結 如果喜歡拜托三連支持~ 目錄 簡介 前景科普-ERC20 Ethers極簡入門教程:HelloVitalik(非小白可跳) 教程概覽 開發工具 V…

魔行觀察-烤匠麻辣烤魚-開關店監測-時間段:2011年1月 至 2024年6月

今日監測對象:烤匠麻辣烤魚,監測時間段:2011年1月 至 2024年6月 本文用到數據源獲取地址 魔行觀察http://www.wmomo.com/ 品牌介紹: 2013年,第一家烤匠在成都藍色加勒比廣場開業,隨后幾年成都國金中心店…

超詳細的tomcat安裝以及簡略項目的部署

一、安裝包 安裝路徑: 鏈接:https://pan.baidu.com/s/1JzPQQ2zUdnXi_FaTTG0pvg?pwdriht 提取碼:riht 安裝完之后我們打開,可看見以下目錄結構 二、環境變量配置 首先打開我們電腦的高級環境變量配置 我們先配置一個系統變量…

Variables Reference for vscode

Predefined variables Visual Studio Code 支持在調試、任務配置文件以及一些特定的設置中使用變量替換。這些變量可以使用 ${variableName} 語法在 launch.json 和 tasks.json 文件的某些鍵和值字符串中使用。 Predefined variables Visual Studio Code 支持以下預定義變量…

Zookeeper:Zookeeper JavaAPI操作與分布式鎖

文章目錄 一、Zookeeper JavaAPI操作1、Curator介紹2、創建、查詢、修改、刪除節點3、Watch事件監聽 二、Zookeeper分布式鎖原理 一、Zookeeper JavaAPI操作 1、Curator介紹 Curator是Apache Zookeeper的Java客戶端。常見的Zookeeper Java API: 原生Java API。ZkC…

天氣網站爬蟲及可視化

摘要:隨著互聯網的快速發展,人們對天氣信息的需求也越來越高。本論文基于Python語言,設計并實現了一個天氣網站爬蟲及可視化系統。該系統通過網絡爬蟲技術從多個天氣網站上獲取實時的天氣數據,并將數據進行清洗和存儲。同時&#…

數據倉庫面試題(二)

1. 簡述星型模型和雪花模型的區別?應用場景 ? 星型模型(Star Schema)和雪花模型(Snowflake Schema)是數據倉庫中常用的兩種維度建模方法,它們在數據組織和設計上有所不同。 星型模型&#xff…