游侠预测R中的尺寸错误

问题描述

发布评估游侠。两者都无法对数据进行子集化(想要rf.trnprob的第一列)

rangermodel= ranger(outcome~.,data=traindata,num.trees=200,probability=TRUE)
rf.trnprob= predict(rangerModel,traindata,type='prob')


trainscore <- subset(traindata,select=c("outcome"))
trainscore$score<-rf.trnprob[,1]  

错误

尺寸错误

table(pred = rf.trbprob,true=traindata$outcome)

错误

所有参数的长度必须相同

解决方法

似乎错误地调用了class Solution { public static void main(String[] args) { /* * 5235 and 4068 is OK * 5235 and 5339 is OK * 5235 and 2553 is OK * 5235 and 5236 is NOT OK * 5325 and 5235 is NOT OK (permutation) */ // threshold is the maximum number of digits that can be the same // while the pin1,pin2 are distant enough; in this case 2 int threshold = 2; int pinLength = 4; System.out.println(areDistant(5235,4068,threshold,pinLength)); System.out.println(areDistant(5235,5339,2553,5236,pinLength)); System.out.println(areDistant(5325,5235,pinLength)); } public static boolean areDistant(int pin1,int pin2,int threshold,int pinLength) { if (isPermutation(pin1,pin2)) return false; int sameDigits = 0; int ithDigit1,ithDigit2; for (int i=0; i<pinLength; i++) { ithDigit1 = (int) (pin1 / Math.pow(10,i)) % 10; ithDigit2 = (int) (pin2 / Math.pow(10,i)) % 10; // System.out.println(ithDigit1); // System.out.println(ithDigit2); if ( ithDigit1 == ithDigit2) sameDigits += 1; } return sameDigits <= threshold; } private static boolean isPermutation(int pin1,int pin2) { return false; // fill the code here } } 函数,它应该是predict而不是response。使用示例数据集:

type

概率存储在此处,每个类的一列:

library(ranger)
traindata =iris
traindata$Species = factor(as.numeric(traindata$Species=="versicolor"))
rangerModel = ranger(Species~.,data=traindata,probability=TRUE)
rf.trnprob= predict(rangerModel,traindata,response='prob')

但是似乎您想做一个混淆矩阵,因此您可以通过以下方式获得预测:

head(rf.trnprob$predictions)
             0           1
[1,] 1.0000000 0.000000000
[2,] 0.9971786 0.002821429
[3,] 1.0000000 0.000000000
[4,] 1.0000000 0.000000000
[5,] 1.0000000 0.000000000
[6,] 1.0000000 0.000000000

然后:

pred = levels(traindata$Species)[max.col(rf.trnprob$predictions)]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...