本文介绍了推荐实验室,asMethod(对象)中的错误:dup_mMatrix_as_geMatrix 类“NA"无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将矩阵更改为可以在Recommenderlab 包的函数中使用的结构.

I am trying to change matrix into a structure that I can use in functions of the recommenderlab package.

datafile1 <- as(datafile1,"matrix")
datafile1
     name1 name2 rating1 rating2 rating3 rating4 rating5 rating6
[1,] "1"   "a"   "0"     "0"     "1"     "0"     "0"     "0"    
[2,] "2"   "d"   "0"     "0"     "1"     "0"     "0"     "0"    
[3,] "3"   "x"   "1"     "0"     "1"     "0"     "0"     "0"    
[4,] "4"   "b"   "0"     "1"     "1"     "0"     "0"     "0"  

library(recommenderlab)
datafile1 <- as(datafile1, "realRatingMatrix")

结果如下:

asMethod(object) 中的错误:dup_mMatrix_as_geMatrix 类 'NA' 无效

有人知道这里出了什么问题吗?

Does anyone have an idea about what's going wrong here?

推荐答案

问题在于 RealRatingMatrix 类扩展了 MatrixMatrix尚未实现带有 character 的矩阵.先将 matrix 转换为 numeric,然后再转换.

The problem is that the RealRatingMatrix class extends Matrix, and Matrix has not implemented matrices with characters in them. Convert your matrix to a numeric first, then convert.

# Recreate data
datafile1<-read.table(textConnection('
name1 name2 rating1 rating2 rating3 rating4 rating5 rating6
"1"   "a"   "0"     "0"     "1"     "0"     "0"     "0"    
"2"   "d"   "0"     "0"     "1"     "0"     "0"     "0"    
"3"   "x"   "1"     "0"     "1"     "0"     "0"     "0"    
"4"   "b"   "0"     "1"     "1"     "0"     "0"     "0"  
'),header=TRUE)
datafile1<-as.matrix(datafile1)

# Convert to numeric (by arbitrarily map the characters to numbers.)
datafile1<-sapply(data.frame(datafile1),as.numeric)

# Create real rating matrix
as(datafile1, "realRatingMatrix")
# 4 x 8 rating matrix of class ‘realRatingMatrix’ with 32 ratings.

这篇关于推荐实验室,asMethod(对象)中的错误:dup_mMatrix_as_geMatrix 类“NA"无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 04:01