在 R 中使用 frbs.learn() 训练 ANFIS 模型时出错

Error in training an ANFIS model using frbs.learn() in R

我正在使用 R 中的 frbs.learn() 构建 ANFIS 模型。 这是我的代码:

library(readxl)
library(anfis)
library(parallel)
library(frbs)
Yamuna_final <- read_excel("F:/Downloads/Yamuna_final.xlsx",    
                         col_names = FALSE)
data.train <- as.matrix(Yamuna_final)

frbs_obj <- frbs.learn(data.train , range.data = NULL, method.type = 
    c("ANFIS"), list(num.labels = 13, max.iter= 10, step.size = 0.01, 
    type.tnorm = "MIN",                                                       
    type.implication.func = "ZADEH" , name = "Sim-0"))

test <- read_excel("F:/Downloads/test.xlsx",    
                       col_names = FALSE)
res <- predict(frbs_obj, test)

现在。当 frbs.learn() 执行时我得到错误:

Error in matrix(nrow = nrow(rule.data.num), ncol = 2 * ncol(rule.data.num) - : invalid 'ncol' value (< 0)

我的数据集(data.train)有 1539 行和 12 列。显示了它的几个实例:

         X__1 X__2 X__3      X__4      X__5      X__6       X__7     X__8        X__9    X__10      X__11        X__12
    [1,] 1999    1    1  7.720000  11.00000  1.000000  0.0500000  0.92000        85.0 14.00000        210   8.60000000
    [2,] 1999    1    2  7.700000  10.00000  1.000000  0.0500000  2.00000        50.0 14.50000       3700  10.80000000
    [3,] 1999    1    3  8.400000  10.00000  1.000000  0.0400000  0.92000       120.0 23.00000        400   8.60000000
    [4,] 1999    1    4  8.270000   6.00000  1.000000  0.0500000  0.56000        80.0 22.00000       4600  12.50000000
    [5,] 1999    1    5  8.180000   6.00000  1.000000  0.0500000  0.80000       140.0 22.00000      23000   8.70000000

现在,我的模型没有接受训练,并且出现上述错误。不知道出了什么问题。 :(

该错误可能是由于您的数据集中存在只有一个唯一值的列。
在下面的代码中,删除这些列后,frbs.learn 运行没有错误。

library(frbs)
data.train <- read.table(text="
         X__1 X__2 X__3      X__4      X__5      X__6       X__7     X__8        X__9    X__10      X__11        X__12
    [1,] 1999    1    1  7.720000  11.00000  1.000000  0.0500000  0.92000        85.0 14.00000        210   8.60000000
    [2,] 1999    1    2  7.700000  10.00000  1.000000  0.0500000  2.00000        50.0 14.50000       3700  10.80000000
    [3,] 1999    1    3  8.400000  10.00000  1.000000  0.0400000  0.92000       120.0 23.00000        400   8.60000000
    [4,] 1999    1    4  8.270000   6.00000  1.000000  0.0500000  0.56000        80.0 22.00000       4600  12.50000000
    [5,] 1999    1    5  8.180000   6.00000  1.000000  0.0500000  0.80000       140.0 22.00000      23000   8.70000000
", header=T)

# Find columns with only one unique value and delete them.
delete_cols <- apply(data.train, 2, function(x) length(unique(x))!=1)
data.train <- data.train[,delete_cols]

frbs_obj <- frbs.learn(data.train, range.data = NULL, method.type =c("ANFIS"), 
  list(num.labels = 13, max.iter= 10, step.size = 0.01, 
    type.tnorm = "MIN",                                                       
    type.implication.func = "ZADEH" , name = "Sim-0"))

否则,错误可能是由于数据集中存在NA
统计数据集列中缺失数据的数量,可以看到第二列有1个缺失值

apply(data.train,2,function(x) sum(is.na(x)))

# X1  X2  X3  X4  X5  X6  X7  X8  X9 X10 X11 X12 
#  0   1   0   0   0   0   0   0   0   0   0   0

第 277 行

posNA <- which(apply(data.train,1,function(x) any(is.na(x))))
data.train[posNA, ]

#       X1 X2 X3   X4 X5 X6    X7    X8   X9 X10     X11 X12
# 277 2000 NA  1 7.49 77 25 13.17 19.26 5000  20 2.1e+07   0

这是最终代码:

library(frbs)
data.train <- read_excel("F:/Downloads/Yamuna_final.xlsx", col_names=FALSE)

posNA <- which(apply(data.train,1,function(x) any(is.na(x))))
data.train <- data.train[-posNA, ]
data.train <- as.matrix(data.train)

frbs_obj <- frbs.learn(data.train , range.data = NULL, method.type = 
    c("ANFIS"), list(num.labels = 13, max.iter= 10, step.size = 0.01, 
    type.tnorm="MIN", type.implication.func="ZADEH" , name="Sim-0"))

另一个解决方案可能是手动输入您的 min/max 限制。分钟数为 0:

range.data=matrix(c(0,1999,0,1,0,5,0,8.4,0,11,0,1,0,0.05,0,2,0
   ,140,0,23,0,23000,0,12.5),ncol=12)

这也允许 frbs_learn 到 运行 而不会出错。