【问题标题】:Python SKlearn contamination must be in (0, 0.5] errorPython SKlearn 污染必须在 (0, 0.5] 错误
【发布时间】:2018-09-14 18:29:27
【问题描述】:

我是机器学习新手,正在使用 Python(3.6)、Pandas、Numpy 和 SKlearn 进行项目。我已经完成了分类和重塑,但是在预测时它会抛出一个错误,如contamination must be in (0, 0.5]

这是我尝试过的:

# Determine no of fraud cases in dataset
Fraud = data[data['Class'] == 1]
Valid = data[data['Class'] == 0]

# calculate percentages for Fraud & Valid 
outlier_fraction = len(Fraud) / float(len(Valid))
print(outlier_fraction)

print('Fraud Cases : {}'.format(len(Fraud)))
print('Valid Cases : {}'.format(len(Valid)))
# Get all the columns from dataframe
columns = data.columns.tolist()

# Filter the columns to remove data we don't want
columns = [c for c in columns if c not in ["Class"] ]

# store the variables we want to predicting on
target = "Class"
X = data.drop(target, 1)
Y = data[target]

# Print the shapes of X & Y
print(X.shape)
print(Y.shape)

# define a random state
state = 1

# define the outlier detection method
classifiers = {
    "Isolation Forest": IsolationForest(max_samples=len(X),
                                       contamination=outlier_fraction,
                                       random_state=state),
    "Local Outlier Factor": LocalOutlierFactor(
    contamination = outlier_fraction)
}
# fit the model
n_outliers = len(Fraud)

for i, (clf_name, clf) in enumerate(classifiers.items()):

    # fit te data and tag outliers
    if clf_name == "Local Outlier Factor":
        y_pred = clf.fit_predict(X)
        scores_pred = clf.negative_outlier_factor_
    else:
        clf.fit(X)
        scores_pred = clf.decision_function(X)
        y_pred = clf.predict(X)

    # Reshape the prediction values to 0 for valid and 1 for fraudulent
    y_pred[y_pred == 1] = 0
    y_pred[y_pred == -1] = 1

    n_errors = (y_pred != Y).sum()

    # run classification metrics 
    print('{}:{}'.format(clf_name, n_errors))
    print(accuracy_score(Y, y_pred ))
    print(classification_report(Y, y_pred ))

这是它返回的内容:

ValueError: 污染必须在 (0, 0.5]

它会为 y_pred = clf.predict(X) 行抛出此错误,正如 Traceback 中所指出的那样。

我是机器学习的新手,对**污染**不太了解,所以我哪里做错了?

请帮帮我!

提前致谢!

【问题讨论】:

  • 我无法从您发布的代码中重现错误。 print(outlier_fraction) 输出什么?
  • 嗨@versatileparsley,它的输出是0.0

标签: python machine-learning scikit-learn machine-learning-model


【解决方案1】:

LocalOutlierFactor 是一种无监督异常值检测算法,在this paper 中引入。每个算法都有自己的参数,这些参数真正改变了算法的行为。在应用该方法之前,您应该始终研究这些参数及其对算法的影响,否则您可能会迷失在海量参数选项的土地上。

对于LocalOutlierFactor,它假定您的异常值不超过数据集的一半。在实践中,我会说,即使 异常值 占据了您数据集的 30%,它们也不再是异常值。它们只是不同的 typeclass 数据。

另一方面,如果您告诉异常值检测算法您有 0 异常值,则您不能指望它会起作用,如果 outlier_fraction 实际上是 0,您可能就是这种情况。

【讨论】:

    【解决方案2】:

    ValueError: 污染必须在 (0, 0.5]

    这意味着contamination 必须严格大于 0.0 且小于或等于 0.5。 (What does this square bracket and parenthesis bracket notation mean [first1,last1)? 是一个关于括号符号的好问题)正如您所评论的,print(outlier_fraction) 输出 0.0,问题在于您发布的代码的前 6 行。

    【讨论】:

      猜你喜欢
      • 2021-09-11
      • 2020-12-17
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      相关资源
      最近更新 更多