【发布时间】:2018-09-13 20:32:43
【问题描述】:
我正在使用 Python(3.6) 和 Sklearn 开展一个项目。我已经完成了分类,但是当我尝试将其应用于重塑以便将其与 sklearn 的 fit 方法一起使用时,它会返回错误。
这是我尝试过的:
# 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(
n_neighbors = 20,
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: could not convert string to float: '301.48 Change: $0.00'
and it's pointed to `clf.fit(X)` line.
我配置错了什么?
【问题讨论】:
-
你传入X的数据有误,包含这个短语。
-
嗨@VivekKumar,我有一个大数据框,我能以某种方式忽略这种值吗?
-
没有。唯一的选择是要么不传递这样的整列,要么预先转换为数字。无论如何
'301.48 Change: $0.00'代表什么?你确定它对于单列而不是两列混合是正确的吗? -
是的,它是正确的,但我们可以将它转换为第一个 float/isn't 值。例如,在这种情况下,我们可以使用 301.48 并忽略字符串的其余部分。
-
@AbdulRehman 如果您只想提取浮点/整数部分,请从列中解析出来并使用它。当您使用
fit时,它只接受floats.. 所以如果字符串(包括数字)确实表示某些东西,您可能想使用 TF-ID 或 BOW
标签: python machine-learning scikit-learn