资源简介
使用python编写了基于PCA的故障检测程序,输入训练数据和测试数据即可。代码中的数据是自行构造的测试数据,可导入自己需要的数据。
亲自编写,可以运行。
代码片段和文件信息
import numpy as np
import pandas as pd
from scipy import linalgstats
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
# 产生训练数据
num_sample = 100
a = 10*np.random.randn(num_sample1)
x1 = a+np.random.randn(num_sample1)
x2 = 1*np.sin(a)+np.random.randn(num_sample1)
x3 = 5*np.cos(5*a)+np.random.randn(num_sample1)
x4 = 0.8*x2+0.1*x3+np.random.randn(num_sample1)
x = np.hstack((x1x2x3x4))
xx_train = x
# 产生测试数据
a = 10*np.random.randn(num_sample1)
x1 = a+np.random.randn(num_sample1)
x2 = 1*np.sin(a)+np.random.randn(num_sample1)
x3 = 5*np.cos(5*a)+np.random.randn(num_sample1)
x4 = 0.8*x2+0.1*x3+np.random.randn(num_sample1)
xx_test = np.hstack((x1x2x3x4))
xx_test[50:1] = xx_test[50:1]+15*np.ones(50)
Xtrain = xx_train
Xtest = xx_test
# 标准化处理
X_mean = np.mean(Xtrain axis=0) #按列求Xtrain平均值
X_std = np.std(Xtrain axis=0) #求标准差
X_rowX_col = Xtrain.shape #求Xtrain行、列数
Xtrain = (Xtrain-np.tile(X_mean(X_row1)))/np.tile(X_std(X_row1))
# 求协方差矩阵
sigmaXtrain = np.cov(Xtrain rowvar=False)
#对协方差矩阵进行特征分解,lamda为特征值构成的对角阵,T的列为单位特征向量,且与lamda中的特征值一一对应:
lamdaT = linalg.eig(sigmaXtrain)
#取对角元素(结果为一列向量),即lamda值,并上下反转使其从大到小排列,主元个数初值为1,若累计贡献率小于90%则增加主元个数
# D = flipud(diag(lamda))
T = T[:lamda.argsort()] #将T的列按照lamda升序排列
lamda.sort() #将lamda按升序排列
D = -np.sort(-np.real(lamda)) #提取实数部分,按降序排列
num_pc = 1
while sum(D[0:num_pc])/sum(D) < 0.9:
num_pc += 1
#取与lamda相对应的特征向量
P = T[:X_col-num_pc:X_col]
TT = Xtrain.dot(T)
TT1 = Xtrain.dot(P)
# 求置信度为95%时的T2统计控制限
T2UCL1 = num_pc*(X_row-1)*(X_row+1)*stats.f.ppf(0.95num_pcX_row-num_pc)/(X_row*(X_row-num_pc))
# 置信度为95%的Q统计控制限
theta = []
for i in range(1 num_pc+1):
theta.append(sum((D[num_pc:])**i))
h0 = 1 - 2*theta[0]*theta[2]/(3*theta[1]**2)
ca = stats.norm.ppf(0.9501)
QUCL = theta[0]*(h0*ca*np.sqrt(2*theta[1])/theta[0] + 1 + theta[1]*h0*(h0 - 1)/theta[0]**2)**(1/h0)
# 在线监测:
# 标准化处理
# X_mean = np.mean(Xtest
- 上一篇:根据epsg代号进行坐标的批量投影转换
- 下一篇:Python 数据库编程入门教程
相关资源
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
- 量化交易(附python常见函数的使用方
- python 名字用字排行
评论
共有 条评论