基于決策樹的旋轉機械故障診斷(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
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn import tree
import joblib 
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
max_depth_values = range(1, 20)
scores_train = []
scores_test = []
for m in max_depth_values:treeModel = tree.DecisionTreeClassifier(random_state = 0, max_depth = m)treeModel.fit(X_train, y_train)scores_train.append(treeModel.score(X_train, y_train))scores_test.append(treeModel.score(X_test, y_test))
plt.figure()
plt.xlabel('max_depth')
plt.ylabel('Accuracy')
plt.plot(max_depth_values, scores_train, label = 'Train')
plt.plot(max_depth_values, scores_test, label = 'Test')
plt.legend()

treeModel = tree.DecisionTreeClassifier(random_state = 0, max_depth = 7)
treeModel.fit(X_train, y_train)
DecisionTreeClassifier(max_depth=7, random_state=0)
tree.plot_tree(treeModel) 
[Text(200.88000000000002, 199.32, 'X[6] <= 0.14\ngini = 0.667\nsamples = 270\nvalue = [90, 90, 90]'),Text(167.40000000000003, 163.07999999999998, 'X[0] <= 3.726\ngini = 0.5\nsamples = 180\nvalue = [0, 90, 90]'),Text(66.96000000000001, 126.83999999999999, 'X[4] <= 0.397\ngini = 0.12\nsamples = 94\nvalue = [0, 6, 88]'),Text(33.480000000000004, 90.6, 'gini = 0.0\nsamples = 87\nvalue = [0, 0, 87]'),Text(100.44000000000001, 90.6, 'X[6] <= 0.055\ngini = 0.245\nsamples = 7\nvalue = [0, 6, 1]'),Text(66.96000000000001, 54.359999999999985, 'gini = 0.0\nsamples = 6\nvalue = [0, 6, 0]'),Text(133.92000000000002, 54.359999999999985, 'gini = 0.0\nsamples = 1\nvalue = [0, 0, 1]'),Text(267.84000000000003, 126.83999999999999, 'X[2] <= 3.032\ngini = 0.045\nsamples = 86\nvalue = [0, 84, 2]'),Text(234.36, 90.6, 'X[5] <= 665.031\ngini = 0.023\nsamples = 85\nvalue = [0, 84, 1]'),Text(200.88000000000002, 54.359999999999985, 'X[6] <= 0.062\ngini = 0.245\nsamples = 7\nvalue = [0, 6, 1]'),Text(167.40000000000003, 18.119999999999976, 'gini = 0.0\nsamples = 6\nvalue = [0, 6, 0]'),Text(234.36, 18.119999999999976, 'gini = 0.0\nsamples = 1\nvalue = [0, 0, 1]'),Text(267.84000000000003, 54.359999999999985, 'gini = 0.0\nsamples = 78\nvalue = [0, 78, 0]'),Text(301.32000000000005, 90.6, 'gini = 0.0\nsamples = 1\nvalue = [0, 0, 1]'),Text(234.36, 163.07999999999998, 'gini = 0.0\nsamples = 90\nvalue = [90, 0, 0]')]

target_names = ['Inner', 'Outer', 'Healthy']
pred = treeModel.predict(X_test)
print(confusion_matrix(y_test, pred))
print(classification_report(y_test, pred, target_names = target_names))
[[28  1  1][ 0 30  0][ 0  3 27]]precision    recall  f1-score   supportInner       1.00      0.93      0.97        30Outer       0.88      1.00      0.94        30Healthy       0.96      0.90      0.93        30accuracy                           0.94        90macro avg       0.95      0.94      0.94        90
weighted avg       0.95      0.94      0.94        90
pred_train = treeModel.predict(X_train)
print(confusion_matrix(y_train, pred_train))
print(classification_report(y_train, pred_train, target_names = target_names))
[[90  0  0][ 0 90  0][ 0  0 90]]precision    recall  f1-score   supportInner       1.00      1.00      1.00        90Outer       1.00      1.00      1.00        90Healthy       1.00      1.00      1.00        90accuracy                           1.00       270macro avg       1.00      1.00      1.00       270
weighted avg       1.00      1.00      1.00       270
sns.set()
mat = confusion_matrix(y_test, pred)
fig, ax = plt.subplots(figsize=(7,6))
sns.set(font_scale=1.3)
sns.heatmap(mat.T, square=False, annot=True, fmt='d', cbar=False,xticklabels=['Fallo inner race', 'Fallo oute race', 'Healthy'],yticklabels=['Fallo inner race', 'Fallo oute race', 'Healthy'],cmap=sns.cubehelix_palette(light=1, as_cmap=True))plt.xlabel('true label');
plt.ylabel('predicted label');

from sklearn.tree import export_graphviz
from six import StringIO
from IPython.display import Image
import pydotplus
dot_data = StringIO()
estadisticos = ['Kurtosis', 'Impulse factor', 'RMS', 'Margin factor', 'Skewness', 'Shape factor', 'Peak to peak', 'Crest factor']
export_graphviz(treeModel, out_file=dot_data,  filled=True, rounded = True,special_characters = True, feature_names = estadisticos, class_names = ['Inner', 'Outer', 'Sano'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('tree_hamming.png')
Image(graph.create_png())

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

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

相關文章

菲爾茲獎得主測試GPT-4o,經典過河難題未能破解!最強Claude 3.5回答離譜!

目錄 01 大言模型能否解決「狼-山羊-卷心菜」經典過河難題&#xff1f; 02 加大難度&#xff1a;100只雞、1000只雞如何&#xff1f; 01 大言模型能否解決「狼-山羊-卷心菜」經典過河難題&#xff1f; 最近&#xff0c;菲爾茲獎得主Timothy Gowers分享了他測試GPT-4o的經歷&a…

游戲推薦: 植物大戰僵尸雜交版

下載地址網上一搜就有. 安裝就能玩. 2是顯血. 4顯示植物血, 5是加速. 都是左手主鍵盤的按鈕, 再按是取消. 比較刺激: ps: 設置里面還能打開自動收集陽光和金幣.

視頻融合共享平臺LntonCVS統一視頻接入平臺智慧安防應用方案

安防視頻監控平臺LntonCVS是一款擁有強大拓展性和靈活部署能力的綜合管理平臺。它支持多種主流標準協議&#xff0c;包括國標GB28181、RTSP/Onvif、RTMP等&#xff0c;同時兼容各廠家的私有協議和SDK&#xff0c;如海康Ehome、海大宇等。LntonCVS不僅具備傳統安防視頻監控功能&…

深入解析Tomcat:Java Web服務器(上)

深入解析Tomcat&#xff1a;Java Web服務器&#xff08;上&#xff09; Apache Tomcat是一個開源的Java Web服務器和Servlet容器&#xff0c;用于運行Java Servlets和JavaServer Pages (JSP)。Tomcat在Java Web應用開發中扮演著重要角色。本文將詳細介紹Tomcat的基本概念、安裝…

遙遠星辰中的覺醒:超大質量黑洞的蘇醒

遙遠星辰中的覺醒&#xff1a;超大質量黑洞的蘇醒 在浩渺無垠的宇宙中&#xff0c;星辰的閃爍仿佛是時間的漣漪&#xff0c;穿越億萬年的距離&#xff0c;抵達我們的眼眸。而在這片星辰大海的深處&#xff0c;一個驚人的現象正在悄然上演——距離地球3.6億光年之遙的星系中&am…

【C++】空指針訪問成員函數

空指針訪問成員函數 C中空指針也是可以調用成員函數的&#xff0c;但是也要注意有沒有用到this指針 如果用到this指針&#xff0c;需要加以判斷保證代碼的健壯性 class Animal { public:void fun1() {//正常的成員函數}void fun2() {if (this NULL) {return;//如果沒有這個…

Django 一對一關系

作用&#xff1a; 兩個數據庫表建立外鍵關系當外鍵表的數據被刪除時&#xff0c;主表的數據也會一并刪除。 1&#xff0c;添加表模型 Test/app8/views.pyfrom django.db import modelsclass User(models.Model):username models.CharField(max_length50, uniqueTrue)email …

【代碼隨想錄訓練營】【Day 65】【圖論-2】| 卡碼 99

【代碼隨想錄訓練營】【Day 65】【圖論-2】| 卡碼 99 需強化知識點 深度搜索和廣度搜索 題目 99. 島嶼數量 思想&#xff1a;遍歷到為1的節點&#xff0c;再搜索標記&#xff0c;每遇到新的陸地節點&#xff0c;增加計數 深度搜索廣度搜索&#xff1a;此處用 [] 作為待遍…

前端面試必備:深入解析Vue.js中v-if與v-show的原理與應用

前言 在Vue.js中&#xff0c;條件渲染是一個核心的概念&#xff0c;它允許我們根據數據的狀態來動態地顯示或隱藏元素。v-if和v-show是Vue.js提供的兩個最常用的條件渲染指令&#xff0c;它們在表面上看起來很相似&#xff0c;但實際上在背后的工作原理和適用場景上有著顯著的…

2024年度濰坊市職業技能大賽 —網絡搭建(網絡與信息安全管理員)職業技能競賽賽項規程

2024年度濰坊市職業技能大賽 —網絡搭建&#xff08;網絡與信息安全管理員&#xff09;職業技能競賽賽項技術文件................................ 一、賽項簡介...................................... 3 二、競賽規程...................................... 3 &#xff08…

【Linux系統】進程替換 自主實現shell(簡易版)

1.先看代碼 && 現象 我們用exec*函數執行新的程序&#xff0c; exec*系列的函數&#xff0c;執行完畢后&#xff0c;后續的代碼不見了&#xff0c;因為被替換了。 execl的返回值可以不關心了&#xff0c;只要替換成功&#xff0c;就不會向后繼續運行&#xff0c;只要…

第5講:建立自己的C函數庫,js調用自己寫的C/C++函數,并包含依賴C/C++第三方靜態庫。

在javascript中&#xff0c;Array有很多內置的功能&#xff0c;比如Array.map&#xff0c;Array.filter&#xff0c;Array.find等等&#xff0c;能用內置的功能就用內置的功能&#xff0c;最好不要自己實現一套&#xff0c;因為底層調用的可能壓根就不是js語言本身&#xff0c;…

[AIGC] awk 和 sed

在Unix系統中&#xff0c;有兩種強大的用于文本操作的命令工具&#xff0c;它們就是awk和sed。這兩個命令工具是每個Linux用戶必備的知識之一&#xff0c;尤其對于需要進行文本處理或數據抽取的開發者來說&#xff0c;更加重要。 在實際開發過程中&#xff0c;我們常常需要處理…

JavaScript中的hasOwnProperty方法詳解

JavaScript中的hasOwnProperty方法詳解 大家好&#xff0c;我是免費搭建查券返利機器人省錢賺傭金就用微賺淘客系統3.0的小編&#xff0c;也是冬天不穿秋褲&#xff0c;天冷也要風度的程序猿&#xff01; 什么是hasOwnProperty方法&#xff1f; 在JavaScript中&#xff0c;h…

Wails 安裝初體驗

文章目錄 Wails 安裝說明1. 系統要求2. 安裝步驟3. 構建應用 結論 Wails 安裝說明 Wails 是一個用于構建桌面應用的 Go 框架&#xff0c;結合了現代前端技術。以下是安裝步驟&#xff1a; 1. 系統要求 Go 1.16 或更高版本Node.js 和 npm可選&#xff1a;適用于 Windows、mac…

【機器學習】機器學習的重要方法——強化學習:理論,方法與實踐

目錄 一、強化學習的核心概念 二、強化學習算法的分類與示例代碼 三.強化學習的優勢 四.強化學習的應用與挑戰 五、總結與展望 強化學習&#xff1a;理論&#xff0c;方法和實踐 在人工智能的廣闊領域中&#xff0c;強化學習&#xff08;Reinforcement Learning, RL&…

轉自羅翔老師的畢業寄語(二)

其實我很想祝大家一帆風順&#xff0c;可是我覺得這不現實。 智者說人這一生至少有三件事是無法避免的&#xff0c;一個是苦難&#xff0c;一個是邪惡&#xff0c;還有一個是人生的終點。所以真的愿我們每時每刻都在當下存儲足夠美好的記憶去對抗人生不期而至的苦楚&#xff0c…

基于源碼詳解ThreadPoolExecutor實現原理

個人博客地址 基于源碼詳解ThreadPoolExecutor實現原理 | iwts’s blog 內容拆分 這里算是一個總集&#xff0c;內容太多&#xff0c;拆分成幾個比較重要的小的模塊&#xff1a; ThreadPoolExecutor基于ctl變量的聲明周期管理 | iwts’s blog ThreadPoolExecutor 工作線程…

模板方法模式在金融業務中的應用及其框架實現

引言 模板方法模式&#xff08;Template Method Pattern&#xff09;是一種行為設計模式&#xff0c;它在一個方法中定義一個算法的框架&#xff0c;而將一些步驟的實現延遲到子類中。模板方法允許子類在不改變算法結構的情況下重新定義算法的某些步驟。在金融業務中&#xff…

可信和可解釋的大語言模型推理-RoG

大型語言模型&#xff08;LLM&#xff09;在復雜任務中表現出令人印象深刻的推理能力。然而&#xff0c;LLM在推理過程中缺乏最新的知識和經驗&#xff0c;這可能導致不正確的推理過程&#xff0c;降低他們的表現和可信度。知識圖譜(Knowledge graphs, KGs)以結構化的形式存儲了…