Python HDBScan class 在进入第一个函数之前,第二次迭代总是失败

Python HDBScan class always fails on second iteration before even entering first function

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

有什么帮助吗?以下是最小可行繁殖:

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

在您的 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