在R中F_meas函数报错’input data must have the same two levels’?

set.seed(1)library(caret)library(dslabs)library(dplyr)data("tissue_gene_expression")y_<-tissue_gene_expression$ytest_index <- createDataPartition(y_, times = 1, ,p=0.5, list = FALSE)x_train<-tissue_gene_expression$x[-test_index,]y_train<-tissue_gene_expression$y[-test_index]x_test<-tissue_gene_expression$x[test_index,]y_test<-tissue_gene_expression$y[test_index]fit<-knn3(x_train,y_train,k=1)y_test_hat<-predict(fit,x_test,type = 'class')F_meas(data = y_test_hat,reference = y_test)

以上是我的代码,但它总是返回以下错误:

Error in F_meas.default(data = y_test_hat, reference = y_test, ) : input data must have the same two levels

尽管我已经检查了这两个数据(y_test_haty_test_hat)的级别,它们都有相同的7个级别


回答:

在这种情况下,”two”指的是2,而不是7。因此,要正确使用F_meas,你需要重新排列你的数据,例如cerebellum, colon, endometrium, hippocampus, kidney, liver, placenta,并为F_meas提供两个级别,否则它将无法工作。

#' @rdname recall#' @importFrom stats complete.cases#' @exportrecall.default <- function(data, reference, relevant = levels(reference)[1],                        na.rm = TRUE, ...) {  if (!is.factor(reference) | !is.factor(data)) stop("input data must be a factor")  if (length(unique(c(levels(reference), levels(data)))) != 2) stop("input data must have the same two levels") # 这里我们看到two指的是2  if (na.rm) {cc <- complete.cases(data) & complete.cases(reference)    if (any(!cc)) {  data <- data[cc]  reference <- reference[cc]}}  xtab <- table(data, reference) recall.table(xtab, relevant = relevant)}  

希望这能澄清问题。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注