我正在使用 ROCR 包,我想知道如何在 R 中为 knn 模型绘制 ROC 曲线?有没有办法用这个包来绘制这一切?
不知道怎么用knn的ROCR的预测功能。这是我的示例,我使用来自 UCI 存储库的 isolet 数据集,我将类属性重命名为 y:
cl<-factor(isolet_training$y)
knn_isolet<-knn(isolet_training, isolet_testing, cl, k=2, prob=TRUE)
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,传递给 ROC 预测函数的参数是什么。我尝试了以下两种不起作用的替代方法:
library(ROCR)
pred_knn<-prediction(knn_isolet$y, cl)
pred_knn<-prediction(knn_isolet$y, isolet_testing$y)
Run Code Online (Sandbox Code Playgroud) 我有10多个功能和十几个案例来训练逻辑回归以对人类进行分类.第一个例子是法语和非法语,第二个例子是英语和非英语.结果如下:
//////////////////////////////////////////////////////
1= fr
0= non-fr
Class count:
0 69109
1 30891
dtype: int64
Accuracy: 0.95126
Classification report:
precision recall f1-score support
0 0.97 0.96 0.96 34547
1 0.92 0.93 0.92 15453
avg / total 0.95 0.95 0.95 50000
Confusion matrix:
[[33229 1318]
[ 1119 14334]]
AUC= 0.944717975754
//////////////////////////////////////////////////////
1= en
0= non-en
Class count:
0 76125
1 23875
dtype: int64
Accuracy: 0.7675
Classification report:
precision recall f1-score support
0 0.91 0.78 0.84 38245
1 0.50 0.74 0.60 11755
avg …Run Code Online (Sandbox Code Playgroud) 有谁知道是否可以以平滑的方式增加 ggplot2 中的线宽而不添加随机突出的线?这是我原来的线图,尺寸增加到 5:
> ggplot(curve.df, aes(x=recall, y=precision, color=cutoff)) +
> geom_line(size=1)
Run Code Online (Sandbox Code Playgroud)

理想情况下,最终图像看起来类似于 PRROC 包中的以下绘图,但从那里绘图时还有另一个问题,即网格线和 ablines 与轴刻度线不对应。
这里我首先打电话
> grid()
Run Code Online (Sandbox Code Playgroud)
然后打电话
> abline(v=seq(0,1,.2), h=seq(0,1,.2))
Run Code Online (Sandbox Code Playgroud)
老实说,我希望能够以更宽的线绘制这条曲线,以看到清晰的颜色和与轴刻度线相对应的网格。谢谢!
以下是截止值 0.5 到 0.7 的数据示例:
> dput(output)
structure(list(recall = c(0.0237648530331457, 0.024390243902439,
0.0250156347717323, 0.0256410256410256, 0.0256410256410256, 0.0268918073796123,
0.0275171982489056, 0.0281425891181989, 0.0293933708567855, 0.0300187617260788,
0.0300187617260788, 0.0300187617260788, 0.0306441525953721, 0.0312695434646654,
0.0312695434646654, 0.0312695434646654, 0.0318949343339587, 0.0318949343339587,
0.0318949343339587, 0.032520325203252, 0.0331457160725453, 0.0331457160725453,
0.0337711069418387, 0.034396497811132, 0.034396497811132, 0.0350218886804253,
0.0356472795497186, 0.0356472795497186, 0.0362726704190119, 0.0362726704190119,
0.0362726704190119, 0.0387742338961851, 0.0387742338961851, 0.0387742338961851,
0.0393996247654784, 0.0400250156347717, 0.0400250156347717, 0.040650406504065,
0.040650406504065, 0.040650406504065, 0.0412757973733583, 0.0419011882426517,
0.042526579111945, 0.0431519699812383, 0.0431519699812383, 0.0437773608505316, …Run Code Online (Sandbox Code Playgroud) 所以我用 scikit-learns 支持向量分类器(svm.SVC)结合流水线和网格搜索构建了一个小例子。经过拟合和评估,我得到了一条看起来很有趣的 ROC 曲线:它只弯曲一次。
我想我会在这里得到更多的曲线形状。谁能解释这种行为?最小的工作示例代码:
# Imports
import sklearn as skl
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn import preprocessing
from sklearn import svm
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn import metrics
from tempfile import mkdtemp
from shutil import rmtree
from sklearn.externals.joblib import Memory
def plot_roc(y_test, y_pred):
fpr, tpr, thresholds = skl.metrics.roc_curve(y_test, y_pred, pos_label=1)
roc_auc = skl.metrics.auc(fpr, tpr)
plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange', lw=lw, label='ROC curve …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用分类的决策树根据某些参数来识别两个类(重命名为0和1)。我使用数据集训练它,然后在“测试数据集”上运行它。当我尝试计算测试数据集中每个数据点的概率时,它仅返回0或1。我想知道是什么问题。
这是示例代码:
clf=tree.DecisionTreeClassifier(random_state=0)
trained=clf.fit(data,identifier) # training data where identifier is 0 or 1
predict=trained.predict(test_data)
结果是:
In [9]: predict
Out[9]:
array([0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0,
0, 0, 1, 0, 0, 0])
In [10]: trained.predict_proba(test_data)[:,1]
Out[10]:
array([ 0., 0., 0., 0., …Run Code Online (Sandbox Code Playgroud) 我正在研究执行校准分类器的效果,我读到校准的目的是使分类器的预测更“可靠”。考虑到这一点,我认为校准后的分类器会有更高的分数 (roc_auc)
当用 sklearn 在 Python 中测试这个假设时,发现正好相反
你能解释一下吗:
校准会提高 roc 分数吗?(或任何指标)
如果不是真的。执行校准的优势是什么?
clf=SVC(probability=True).fit(X_train,y_train)
calibrated=CalibratedClassifierCV(clf,cv=5,method='sigmoid').fit(X_train,y_train)
probs=clf.predict_proba(X_test)[:,1]
cal_probs=calibrated.predict_proba(X_test)[:,1]
plt.figure(figsize=(12,7))
names=['non-calibrated SVM','calibrated SVM']
for i,p in enumerate([probs,cal_probs]):
plt.subplot(1,2,i+1)
fpr,tpr,threshold=roc_curve(y_test,p)
plt.plot(fpr,tpr,label=nombre[i],marker='o')
plt.title(names[i]+ '\n' + 'ROC: '+ str(round(roc_auc_score(y_test,p),4)))
plt.plot([0,1],[0,1],color='red',linestyle='--')
plt.grid()
plt.tight_layout()
plt.xlim([0,1])
plt.ylim([0,1])
Run Code Online (Sandbox Code Playgroud)
我正在使用scikit学习,我想绘制精度和召回曲线。我正在使用的分类器是RandomForestClassifier。scikit学习文档中的所有资源均使用二进制分类。另外,我可以为多类绘制ROC曲线吗?
另外,我只找到了支持向量机的多标签,它有一个decision_function它RandomForest没有
嗯,我一直在尝试以特定的方式用我的 ROCS 制作一个情节,以便它与我的同事正在做的出版物的风格相匹配。
但每次我做我的 ROCS 时,我什至无法设法减少我的轴(我在 xlim 中尝试了几次更改),也没有获得图形的“盒状”边框。我尝试按照这些教程进行操作
https://www.youtube.com/watch?v=qcvAqAH60Yw https://rdrr.io/cran/pROC/man/ggroc.html
但我没有得到任何东西,并且 ggroc 拒绝工作,说我的尺寸不正确(即使页面提供了示例)。
我刚刚用我的数据得到了这个:
但是如果我将 xlim 更改为 0,1
不起作用。我已经尝试了几种组合。
有任何想法吗?
为了提供一些代码,让我们使用 ASAH 数据。
如果我们这样做,我们也会在 pROC 中的绘图中遇到同样的问题。
你能帮我这个尊敬的堆栈社区吗?编辑:到目前为止,一切顺利。但我仍然有轴的问题,我希望它们作为我发布的第一张图像作为参考开始。
有了 ggplot2,我想我可以做到这一点,不过,感谢提供的答案。
我正在使用kerasSequential() API 为 5 类问题构建 CNN 模型。由于准确性对于多类问题来说不是一个好的指标,因此我必须评估其他指标来评估我的模型。目前,我使用sklearn'sconfusion_matrix和classification_report's ,但我想研究更多指标,因此我决定评估 ROC AUC,但我不确定这是如何使用 keras 完成的,我应该对我的代码进行哪些修改等。
目前,这就是我构建模型的方式:
model = Sequential()
activ = 'relu'
model.add(Conv2D(32, (1, 3), strides=(1, 1), padding='same', activation=activ, input_shape=(1, 100, 4)))
model.add(Conv2D(32, (1, 3), strides=(1, 1), padding='same', activation=activ ))
model.add(MaxPooling2D(pool_size=(1, 2) ))
model.add(Conv2D(64, (1, 3), strides=(1, 1), padding='same', activation=activ))
model.add(Conv2D(64, (1, 3), strides=(1, 1), padding='same', activation=activ))
model.add(MaxPooling2D(pool_size=(1, 2)))
model.add(Flatten())
A = model.output_shape
model.add(Dense(int(A[1] * 1/4.), activation=activ))
model.add(Dense(5, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5, …Run Code Online (Sandbox Code Playgroud) 我想我已经被派到这里接受培训了。
library(caret)
library(mlbench)
library(plotROC)
library(pROC)
data(Sonar)
ctrl <- trainControl(method="cv",
summaryFunction=twoClassSummary,
classProbs=T,
savePredictions = T)
rfFit <- train(Class ~ ., data=Sonar,
method="rf", preProc=c("center", "scale"),
trControl=ctrl)
# Select a parameter setting
selectedIndices <- rfFit$pred$mtry == 2
Run Code Online (Sandbox Code Playgroud)
我想绘制 ROC。
plot.roc(rfFit$pred$obs[selectedIndices],
rfFit$pred$M[selectedIndices])
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试 ggplot2 方法时,它给了我完全不同的东西。
g <- ggplot(rfFit$pred[selectedIndices, ], aes(m=M, d=factor(obs, levels = c("R", "M")))) +
geom_roc(n.cuts=0) +
coord_equal() +
style_roc()
g + annotate("text", x=0.75, y=0.25, label=paste("AUC =", round((calc_auc(g))$AUC, 4)))
Run Code Online (Sandbox Code Playgroud)
我在这里做了一些非常错误的事情,但我不知道它是什么。谢谢。
roc ×10
python ×5
r ×4
scikit-learn ×4
auc ×2
ggplot2 ×2
plot ×2
keras ×1
knn ×1
matplotlib ×1
r-caret ×1
tensorflow ×1