1

我正在尝试使用几种不同的 SKLearn、HDBScan 和自定义异常值检测类来查看综合异常值信息。但是,由于某种原因,我一直遇到一个错误,即无法迭代任何使用 HDBScan 的类。所有其他 Sklearn 和自定义类都可以。我遇到的问题似乎一直发生在 HDBScan 类的第二遍,并立即发生在 algorithm.fit(tmp) 上。在调试脚本时,看起来甚至在到达类的第一行之前就抛出了错误。

有什么帮助吗?以下是最小可行复制:

import numpy as np
import pandas as pd
import hdbscan
from sklearn.datasets import make_blobs
from sklearn.svm import OneClassSVM
from sklearn.ensemble import IsolationForest
from sklearn.covariance import EllipticEnvelope

class DBClass():

    def __init__(self, random = None):
        self.random = random

    def fit(self, data):

        self.train_data = data

        cluster = hdbscan.HDBSCAN()
        cluster.fit(self.train_data)
        self.fit = cluster

    def predict(self, data):

        self.predict_data = data

        if self.train_data.equals(self.predict_data):
            return self.fit.probabilities_  


def OutlierEnsemble(df, anomaly_algorithms = None, num_slices = 5, num_columns = 7, outliers_fraction = 0.05):

    if isinstance(df, np.ndarray):
        df = pd.DataFrame(df)

    assert isinstance(df, pd.DataFrame)

    if not anomaly_algorithms: 
        anomaly_algorithms = [
            ("Robust covariance",
                EllipticEnvelope(contamination=outliers_fraction)),
            ("One-Class SVM",
                OneClassSVM(nu=outliers_fraction,
                                kernel="rbf")),
            ("Isolation Forest",
                IsolationForest(contamination=outliers_fraction)),
            ("HDBScan LOF",
                DBClass()),
        ]

    data = []
    for i in range(1, num_slices + 1):
        data.append(df.sample(n = num_columns, axis = 1, replace = False))

    predictions = []
    names = []

    for tmp in data:
        counter = 0
        for name, algorithm in anomaly_algorithms:
            algorithm.fit(tmp)
            predictions.append(algorithm.predict(tmp))
            counter += 1
            names.append(f"{name}{counter}")


    return predictions

blobs, labels = make_blobs(n_samples=3000, n_features=12)
OutlierEnsemble(blobs)

提供的错误不是最有用的。

Traceback (most recent call last):

  File "<ipython-input-4-e1d4b63cfccd>", line 75, in <module>
    OutlierEnsemble(blobs)

  File "<ipython-input-4-e1d4b63cfccd>", line 66, in OutlierEnsemble
    algorithm.fit(tmp)

TypeError: 'HDBSCAN' object is not callable
4

1 回答 1

1

在您的DBClass.fit,DBClass.fit无意中重新定义。

你也许可以使用类似的东西,

class DBClass():

    def __init__(self, random = None):
        self.random = random

    def fit(self, data):

        self.train_data = data

        cluster = hdbscan.HDBSCAN()
        cluster.fit(self.train_data)
        self.myfit = cluster   # save calculated cluster

    def predict(self, data):

        self.predict_data = data

        if self.train_data.equals(self.predict_data):
            return self.myfit.probabilities_  # use calculated cluster
于 2020-06-21T21:51:02.427 回答