有一组时序数据,是企业设备的振动能量大小,一般都稳定在一个范围内,如果能量值出现异常趋势,希望能识别出来,请用Python写算法实现,给出至少五个实现的代码
时间: 2023-10-05 14:09:05 浏览: 35
1. 使用统计方法:计算均值和标准差,判断是否超过一定的阈值
```python
import numpy as np
def detect_anomaly(data, threshold=3):
mean = np.mean(data)
std = np.std(data)
if np.abs(data[-1] - mean) > threshold * std:
return True
else:
return False
```
2. 使用滑动窗口:对一段时间内的数据进行平均,判断当前数据是否与平均值相差较大
```python
def detect_anomaly(data, window_size=5, threshold=3):
if len(data) < window_size:
return False
else:
window = data[-window_size:]
mean = np.mean(window)
std = np.std(window)
if np.abs(data[-1] - mean) > threshold * std:
return True
else:
return False
```
3. 使用聚类方法:将数据聚为正常和异常两类,使用新数据判断所属类别
```python
from sklearn.cluster import KMeans
def detect_anomaly(data, n_clusters=2):
kmeans = KMeans(n_clusters=n_clusters)
kmeans.fit(data.reshape(-1, 1))
label = kmeans.predict(data[-1].reshape(-1, 1))
if label == 1:
return True
else:
return False
```
4. 使用支持向量机(SVM)方法:将数据分为正常和异常两类,使用SVM分类器判断新数据所属类别
```python
from sklearn.svm import SVC
def detect_anomaly(data, C=1.0, kernel='rbf'):
X = np.arange(len(data)).reshape(-1, 1)
y = np.zeros(len(data))
for i in range(len(data)):
if data[i] > np.percentile(data, 95):
y[i] = 1
clf = SVC(C=C, kernel=kernel)
clf.fit(X, y)
label = clf.predict(len(data).reshape(-1, 1))
if label == 1:
return True
else:
return False
```
5. 使用深度学习方法:使用LSTM神经网络进行时序数据预测,判断当前数据是否与预测值相差较大
```python
from keras.models import Sequential
from keras.layers import LSTM, Dense
def detect_anomaly(data, look_back=1, threshold=3):
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
X, y = [], []
for i in range(len(data)-look_back):
X.append(data[i:i+look_back])
y.append(data[i+look_back])
X = np.array(X)
y = np.array(y)
model.fit(X, y, epochs=100, batch_size=1, verbose=0)
predict = model.predict(np.array(data[-look_back:]).reshape(1, 1, look_back))[0][0]
if np.abs(data[-1] - predict) > threshold:
return True
else:
return False
```