【问题标题】:RStudio - object not found - kohonen packRStudio - 找不到对象 - kohonen pack
【发布时间】:2018-01-15 04:38:11
【问题描述】:

我正在尝试为som 地图编写脚本。它来自这个tutorial。我的问题是 Rstudio 不起作用。我有这个代码:

require(kohonen)

# Create a training data set (rows are samples, columns are variables
# Here I am selecting a subset of my variables available in "data"
data_train <- data[, c(2,4,5,8)]

# Change the data frame with training data to a matrix
# Also center and scale all variables to give them equal importance during
# the SOM training process. 
data_train_matrix <- as.matrix(scale(data_train))

# Create the SOM Grid - you generally have to specify the size of the 
# training grid prior to training the SOM. Hexagonal and Circular 
# topologies are possible
som_grid <- somgrid(xdim = 20, ydim=20, topo="hexagonal")

# Finally, train the SOM, options for the number of iterations,
# the learning rates, and the neighbourhood are available
som_model <- som(data_train_matrix, 
                 grid=som_grid, 
                 rlen=500, 
                 alpha=c(0.05,0.01), 
                 keep.data = TRUE )
plot(som_model, type="changes")

如果我尝试运行这个脚本,它会写出这个错误:

Error in supersom(list(X), ...) : object 'data_train_matrix' not found
> plot(som_model, type="changes")
Error in plot(som_model, type = "changes") : object 'som_model' not found

我不明白这一点。没有data_train_matrix是什么意思?我之前有data_train_matrix 几行。当我只运行前 3 行代码(到data_train_matrix &lt;- as.matrix(scale(data_train)))时,它会写出这个错误:

data_train_matrix <- as.matrix(scale(data_train))
Error in scale(data_train) : object 'data_train' not found

当我只运行前两行时它写道:

data_train <- data[, c(2,4,5,8)]
Error in data[, c(2, 4, 5, 8)] : 
  object of type 'closure' is not subsettable  

当我使用相同的代码有这么多错误时,这段代码怎么可能在教程中工作?

【问题讨论】:

  • 你安装了kohonen然后激活了吗?

标签: r som


【解决方案1】:

看起来错误来自没有原始的类似数据框的对象。变量“data-train”是“data”的一个子集,从未被正确分配。 您需要首先遵循创建训练数据集的注释行。

    # Create a training data set (rows are samples, columns are variables
    # Here I am selecting a subset of my variables available in "data"
    data_train <- data[, c(2,4,5,8)]

R 还有一个名为“data”的函数,这就是它解释代码的方式。与 R 中的大多数函数一样,此函数不可子集化。

如果您在最前面创建一些数据,那么一切都应该正常。

    data = data.frame(matrix(rnorm(20), nrow=2))
    data_train <- data[, c(2,4,5,8)]
    # the rest of the script as written

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 2012-06-23
    • 2012-03-16
    • 2020-11-06
    • 1970-01-01
    相关资源
    最近更新 更多