我是机器学习的新手,正在从事使用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]
如Traceback中所指出的,它会为
y_pred = clf.predict(X)
行引发此错误。我是机器学习的新手,对**污染**不太了解,所以我在哪里做错了什么?
请帮帮我!
提前致谢!
最佳答案
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行。
关于python - Python SKlearn污染必须为(0,0.5]错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49663796/