R有一个很好的库来检测时间序列中的异常,它的名字是tsoutliers,例如在这里讨论https://stats.stackexchange.com/questions/104882/detecting-outliers-in-time-series-ls-ao- TC-使用-tsoutliers封装功能于R-如何
python中有替代库吗?或者如何使用更多标准库(numpy,scipy,sklearn)找到时间序列异常值的简单方法?
Luminol图书馆的Github链接:https://github.com/linkedin/luminol
任何人都可以用示例代码向我解释,如何使用此模块来查找数据集中的异常.
我想使用这个模块来查找我的时间序列数据中的异常.
PS:我尝试了README.md中提供的示例1,但是收到错误,所以有人请给我一个查找异常的工作示例.
示例1将异常分数放入列表中.
from luminol.anomaly_detector import AnomalyDetector
my_detector = AnomalyDetector(ts)
score = my_detector.get_all_scores()
anom_score = list()
for (timestamp, value) in score.iteritems():
t_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
anom_score.append([t_str, value])
Run Code Online (Sandbox Code Playgroud)
获取值错误:(22,'无效参数')在行:t_str = time.strftime('%Y-%m-%d%H:%M%S',time.localtime(timestamp))
使用Python 2.7
谢谢 :)
Scikit-Learn 的IsolationForest类有一个方法decision_function可以返回输入样本的异常分数。但是,文档并没有说明这些分数的可能范围是多少,只说明“[分数]越低,越不正常”。
编辑:阅读 jmunsch 的评论后,我再次查看了源代码,这是我更新的猜测:如果分数公式中的指数始终为负,则分数将始终介于 0 和 1 之间,这意味着返回的范围是 [- 0.5, 0.5] 因为0.5 - scores由该方法返回。但我不确定指数是否总是负数。
在使用隔离森林进行数据异常检测时,我们应该仅使用正常数据还是混合使用正常数据和异常数据来训练模型?另外,多变量数据异常检测的最佳算法是什么?我想要最少的误报。
注意:我知道减少误报是调整模型的问题,但我想知道最有效的算法。从博客中我了解到 IsolationForest 是最新、最有效的无监督异常检测算法之一。
当某个主题的消息率高于或低于平时,我如何获得提醒?
我目前正在使用Python 中的Isolation Forest检测数据集中的异常值,但我没有完全理解 scikit-learn 文档中给出的示例和解释
是否可以使用隔离森林来检测具有 258 行和 10 列的数据集中的异常值?
我需要一个单独的数据集来训练模型吗?如果是,是否有必要让训练数据集没有异常值?
这是我的代码:
rng = np.random.RandomState(42)
X = 0.3*rng.randn(100,2)
X_train = np.r_[X+2,X-2]
clf = IsolationForest(max_samples=100, random_state=rng, contamination='auto'
clf.fit(X_train)
y_pred_train = clf.predict(x_train)
y_pred_test = clf.predict(x_test)
print(len(y_pred_train))
Run Code Online (Sandbox Code Playgroud)
我尝试将我的数据集加载到,X_train但这似乎不起作用。
因此,得出以下结论:实时异常检测的定义是什么?
我正在研究异常检测领域,在许多论文中,该方法被定义为实时,而在许多其他论文中,它被简称为异常检测。
我碰巧发现,纠正我是否我错了,大多数所谓的实时方法实际上是类似近实时的。具体来说,它们是时间序列上的某种无监督的基于上下文的异常检测,其中上下文几乎总是桶的大小。换句话说,算法处理微批次数据,因此从这里遵循近实时.
现在,我想知道这两种异常检测是否有区别。如果是这样,它们之间有何不同以及桶大小的阈值是多少(如果有)?
这组问题来自这样一个事实,即我正在进行一项关于异常检测不同框架的预测性能/质量的研究,我想知道这种差异是否很大,因为它意味着两个不同的评估指标。我想阅读一些关于此事的认证来源。
下面的数据显示了我的数据集的一部分,用于检测异常
describe_file data_numbers index
0 gkivdotqvj 7309.0 0
1 hpwgzodlky 2731.0 1
2 dgaecubawx 0.0 2
3 NaN 0.0 3
4 lnpeyxsrrc 0.0 4
Run Code Online (Sandbox Code Playgroud)
我使用了一类 SVM 算法来检测异常
from pyod.models.ocsvm import OCSVM
random_state = np.random.RandomState(42)
outliers_fraction = 0.05
classifiers = {
'One Classify SVM (SVM)':OCSVM(kernel='rbf', degree=3, gamma='auto', coef0=0.0, tol=0.001, nu=0.5, shrinking=True, cache_size=200, verbose=False, max_iter=-1, contamination=outliers_fraction)
}
X = data['data_numbers'].values.reshape(-1,1)
for i, (clf_name, clf) in enumerate(classifiers.items()):
clf.fit(X)
# predict raw anomaly score
scores_pred = clf.decision_function(X) * -1
# prediction of a datapoint …Run Code Online (Sandbox Code Playgroud) 我期待创建一个通用功能的输出转换decision_scores的sklearn's IsolationForest成真概率[0.0, 1.0]。
我知道并阅读了原始论文,并且我在数学上理解该函数的输出不是概率,而是每个基估计器构建的路径长度的平均值,以隔离异常。
我想将该输出转换为tuple (x,y)wherex=P(anomaly)和形式的概率y=1-x。
def convert_probabilities(predictions, scores):
from sklearn.preprocessing import MinMaxScaler
new_scores = [(1,1) for _ in range(len(scores))]
anomalous_idxs = [i for i in (range(len(predictions))) if predictions[i] == -1]
regular_idxs = [i for i in (range(len(predictions))) if predictions[i] == 1]
anomalous_scores = np.asarray(np.abs([scores[i] for i in anomalous_idxs]))
regular_scores = np.asarray(np.abs([scores[i] for i in regular_idxs]))
scaler = MinMaxScaler()
anomalous_scores_scaled = scaler.fit_transform(anomalous_scores.reshape(-1,1))
regular_scores_scaled = …Run Code Online (Sandbox Code Playgroud) python machine-learning probability scikit-learn anomaly-detection
我想检测包含趋势和季节性成分的“时间序列数据”中的异常值。我想忽略季节性的峰值,只考虑其他峰值并将它们标记为异常值。由于我是时间序列分析的新手,请帮助我解决这个时间序列问题。
使用的编码平台是 Python。
我已经训练了我的模型并预测了测试数据。然后能够计算预测结果与我的测试数据的实际值之间的差异,然后能够根据观察到的方差找出异常值。
!pip install pyramid-arima
from pyramid.arima import auto_arima
stepwise_model = auto_arima(train_log, start_p=1, start_q=1,max_p=3, max_q=3,m=7,start_P=0, seasonal=True,d=1, D=1, trace=True,error_action='ignore', suppress_warnings=True,stepwise=True)
Run Code Online (Sandbox Code Playgroud)
import math
import statsmodels.api as sm
import statsmodels.tsa.api as smt
from sklearn.metrics import mean_squared_error
Run Code Online (Sandbox Code Playgroud)
train, test = actual_vals[0:-70], actual_vals[-70:]
Run Code Online (Sandbox Code Playgroud)
train_log, test_log = np.log10(train), np.log10(test)
Run Code Online (Sandbox Code Playgroud)
history = [x for x in train_log]
predictions = list()
predict_log=list()
Run Code Online (Sandbox Code Playgroud)
for t in range(len(test_log)):
stepwise_model.fit(history)
output = stepwise_model.predict(n_periods=1)
predict_log.append(output[0])
yhat = 10**output[0] …Run Code Online (Sandbox Code Playgroud) python ×5
scikit-learn ×4
time-series ×3
apache-kafka ×1
monitoring ×1
outliers ×1
probability ×1
python-2.7 ×1
python-3.x ×1
r ×1
real-time ×1
svm ×1