Python是一种广泛使用的解释型、高级和通用的编程语言,众多的开源科学计算软件包都提供了Python接口,如计算机视觉库OpenCV、可视化工具库VTK等。Python专用计算扩展库,如NumPy、SciPy、matplotlab、Pandas、scikit-learn等。
开发工具上可用Spyder,在安装Anaconda时已经安装好,可以从开始菜单或者使用命令行spyder启动。Spyder是一个免费、开源的集成开发环境,用Python编写,用于Python程序的开发,由科学家、工程师和数据分析师设计。它综合开发工具的高级编辑、分析、调试和分析功能与科学软件包的数据探索、交互执行、深入检查和漂亮的可视化功能独特地结合在一起。可以单步执行,查看运行后的各变量的值,查看序列数据线图等,非常方便。其他的集成开发工具(IDE)还包括:
VisualStudioCode(简称VSCode)是一款由微软开发且跨平台的免费源代码编辑器。该软件支持语法高亮、代码自动补全(又称IntelliSense)、代码重构功能,并且内置了命令行工具和Git版本控制系统。用户可以更改主题和键盘快捷方式实现个性化设置,也可以通过内置的扩展程序商店安装扩展以拓展软件功能。VisualStudioCode默认支持非常多的编程语言,要使用Python进行开发,需要先安装相应的Python扩展(PythonextensionforVisualStudioCode)。
PyCharm,主要用于Python语言开发,由捷克公司JetBrains开发,提供代码分析、图形化调试器,集成测试器、集成版本控制系统,并支持使用Django进行网页开发。PyCharm是一个跨平台开发环境,拥有MicrosoftWindows、macOS和Linux版本。社区版在Apache许可证下发布,另外还有专业版在专用许可证下发布,其拥有许多额外功能。
本项目采用Python语言进行简单的信号处理(包络谱,低通、高通、带通滤波,初级特征提取,机器学习,短时傅里叶变换)及轴承故障诊断探索。
n_win_numb = 128;
n_win = np.hamming(n_win_numb);
n_overlap = 127;
nfft = n_win_numb;
pad_to = nfft*10;
plt.figure(figsize=(8,6))
plt.specgram(y, NFFT = nfft, Fs = fs, window = n_win, noverlap = n_overlap, pad_to=pad_to);
plt.title(['Increasing Win Overlap: win=',n_win_numb,' / overlap=',n_overlap, ' / nfft=',nfft, 'pad_to=',pad_to]);
plt.xlabel('time(s)');plt.ylabel('freq(Hz)')
n_win_numb = 32;
n_win = np.hamming(n_win_numb);
n_overlap = 0;
nfft = n_win_numb;
plt.figure(figsize=(8,6))
plt.specgram(y, NFFT = nfft, Fs = fs, window = n_win, noverlap = n_overlap);
plt.title(['Increasing Win Overlap: win=',n_win_numb,' / overlap=',n_overlap, ' / nfft=',nfft]);
plt.xlabel('time(s)');plt.ylabel('freq(Hz)')
n_win_numb = 32;
n_win = np.hamming(n_win_numb);
n_overlap = 30;
nfft = n_win_numb;
pad_to = nfft*10;
plt.figure(figsize=(8,6))
plt.specgram(y, NFFT = nfft, Fs = fs, window = n_win, noverlap = n_overlap, pad_to=pad_to);
plt.title(['Increasing Win Overlap: win=',n_win_numb,' / overlap=',n_overlap, ' / nfft=',nfft, 'pad_to=',pad_to]);
plt.xlabel('time(s)');plt.ylabel('freq(Hz)')
n_win_numb = 256;
n_win = np.hamming(n_win_numb);
n_overlap = 250;
nfft = n_win_numb;
plt.figure(figsize=(8,6))
plt.specgram(y, NFFT = nfft, Fs = fs, window = n_win, noverlap = n_overlap);
plt.title(['Increasing Win Overlap: win=',n_win_numb,' / overlap=',n_overlap, ' / nfft=',nfft]);
plt.xlabel('time(s)');plt.ylabel('freq(Hz)')
fig = plt.figure(figsize=(12,8))
ax = Axes3D(fig,azim=-0.001,elev=80)
plt.ylabel('Frequency');plt.xlabel('Div #')
for x, y, z, c in zip(plot_x.T, plot_y.T, A_div.T, colors):
ax.plot3D(y,x,z,color=c, linewidth=0.5)
plt.gca().invert_xaxis()
colors = cm.rainbow(np.linspace(0, 1, len(plot_y)))
fig = plt.figure(figsize=(12,8))
ax = Axes3D(fig,azim=-0.001,elev=85)
plt.ylabel('Time (s)');plt.xlabel('Div #')
for x, y, z, c in zip(plot_x.T, plot_y.T, y_div.T, colors):
ax.plot3D(y,x,z,color=c, linewidth=0.7)
plt.gca().invert_xaxis()
feature_n_mean = np.mean(feature_n,axis=0)
feature_f_mean = np.mean(feature_f,axis=0
plt.figure(figsize=(8,6))
plt.plot(feature_n_mean,'-bo')
plt.plot(feature_f_mean,'-rx')
plt.title('Average of Features')
plt.xticks(np.arange(0,np.size(feature_name)),feature_name,rotation='vertical');
plt.show()
from lightgbm import LGBMClassifier, plot_importance
n_estimator = 100
colsample_bytree = 0.1
model = LGBMClassifier(n_estimator=n_estimator,colsample_bytree = colsample_bytree)
model.fit(x_train, y_train)
y_test_pred = model.predict(x_test)
importance = model.feature_importances_
plt.figure(figsize=(4,4))
cm = pd.DataFrame(confusion_matrix(y_test, y_test_pred))
sns.heatmap(cm, annot=True)
plt.figure(figsize=(8,6))
plt.plot(importance,'-ko')
plt.xticks(np.arange(0,np.size(feature_name)),feature_name,rotation='vertical');
fig,ax = plt.subplots()
plot_importance(model,ax=ax)
print(model.score(x_test, y_test))
# print(metrics(y_test,y_test_pred))
plt.figure(figsize=(8,6))
plt.figure(figsize=(8,6))
z_ML_utils.plot_decision_boundaries(x,y,XGBClassifier, idx1=idx1, idx2=idx2, n_estimator=n_estimator,colsample_bytree = colsample_bytree)
完整的项目代码:https://mbd.pub/o/bread/Y5yXmJtt
工学博士,担任《Mechanical System and Signal Processing》《中国电机工程学报》《控制与决策》等期刊审稿专家,擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。