machine-learning - ValueError: y_true 在 {'True', 'False'} 中取值并且 pos_label 未在 ROC_curve 中指定
问题描述
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=2)
# generate a no skill prediction (majority class)
ns_probs = [0 for _ in range(len(y_test))]
# fit a model
model = KNeighborsClassifier(n_neighbors = 3)
model.fit(x_train, y_train)
# predict probabilities
lr_probs = model.predict_proba(x_test)
# keep probabilities for the positive outcome only
lr_probs = lr_probs[:, 1]
# calculate scores
ns_auc = roc_auc_score(y_test, ns_probs)
lr_auc = roc_auc_score(y_test, lr_probs)
# summarize scores
print('No Skill: ROC AUC=%.3f' % (ns_auc))
print('Logistic: ROC AUC=%.3f' % (lr_auc))
# calculate roc curves
ns_fpr, ns_tpr, _ = roc_curve(y_test, ns_probs) <-- Error Occurred
lr_fpr, lr_tpr, _ = roc_curve(y_test, lr_probs)
...
我正在尝试在 KNN 算法中使用 ROC 曲线。
ValueError: y_true takes value in {'True', 'False'} and pos_label is not specified:
either make y_true take value in {0, 1} or {-1, 1} or pass pos_label explicitly
但是,正如您在上面看到的,发生了错误。
from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
encoder.fit(data.Malware)
data['TrueorFalse'] = encoder.transform(data['TrueorFalse'])
data.value_counts(data['TrueorFalse'].values, sort=False)
data.head()
所以为了解决这个问题,我认为我写的“True”和“False”标签是有问题的,因为它们是字符串。因此,应用上面的代码将 True 或 Flase 分别切换为 0 和 1,但仍然会出现错误。我在列中使用True
和False
作为标签TrueorFalse
。有什么我想念的吗?
解决方案
y_test = y_test.map({'True': 1, 'False': 0}).astype(int)
添加此代码帮助我解决了我的问题。
推荐阅读
- java - 确定从 Eclipse workspace.xmi 将哪些透视图/视图加载到工作区中
- python - 从 numpy 数组中删除 NaN 填充边框
- java - Java进程下来自plink的不完整inputStream
- java - 使用paint(Graphics p)时在java中删除一行?
- r - 对数据框中具有相同值的列元素进行计数并以矩阵格式显示
- dapper - 多次映射到同一张表
- android - ExoPlayer 无法在某些设备上播放声音
- java - 如何使用java中的线程来实现这个文本文件读/写程序?
- python - 使用python删除冗余节点集
- clojure - 是否可以定义一个宏:在函数上调度其他任何东西?