在R

时间:2018-10-12 20:47:58

标签: r machine-learning neural-network rstudio fuzzy

我正在使用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()时出现错误:

  

矩阵错误(nrow = nrow(rule.data.num),ncol = 2 *   ncol(rule.data.num)-:无效的'ncol'值(<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

现在,我的模型没有得到训练,并且出现了以上错误。不知道怎么了:(

2 个答案:

答案 0 :(得分:1)

该错误可能是由于数据集中只有一个唯一值的列的存在。
在下面的代码中,删除这些列后,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"))

答案 1 :(得分:0)

另一个解决方案可能是手动输入您的最小/最大限制。这里的分钟数是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可以正常运行。