【问题标题】:Error on Kmeans in RR中的Kmeans错误
【发布时间】:2018-04-17 00:53:58
【问题描述】:

我正在尝试在 r 中使用 kmeans 进行聚类,但它给出了错误

Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning message:
In storage.mode(x) <- "double" : NAs introduced by coercion

这是代码

Cluster_df <- kmeans(cluster_data, 2, algorithm = "Forgy")

也试过

Cluster_df <- kmeans(cluster_data, 2, algorithm = "Lloyd")

这是数据集

> dput(cluster_data)
structure(list(Value = c(1.2, 40.8, 3.7, 39.5, 2.3, 10.8, 1, 
1.2, 0.1, 0.1, 0), Type = c("1 rk", "1 bhk", "1.5 bhk", "2 bhk", 
"2.5 bhk", "3 bhk", "3.5 bhk", "4 bhk", "4.5 bhk", "5 bhk", "5.5 bhk"
)), .Names = c("Value", "Type"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -11L), spec = structure(list(cols = structure(list(
    Value = structure(list(), class = c("collector_double", "collector"
    )), Type = structure(list(), class = c("collector_character", 
    "collector"))), .Names = c("Value", "Type")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))

这是我正在使用的R 的版本

> version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          4.0                         
year           2017                        
month          04                          
day            21                          
svn rev        72570                       
language       R                           
version.string R version 3.4.0 (2017-04-21)
nickname       You Stupid Darkness 

任何建议都会有所帮助。

谢谢。

多尼克

【问题讨论】:

  • 你使用什么包来创建你的对象cluster_data?函数do_one 来自哪里?每当您使用外部包时,您应该通过调用 library(pkgname) 来启动脚本。
  • @RuiBarradas clusterd_data是从csv文件中导入的数据,其内容也已发布
  • @RuiBarradas 我在写代码的时候没有导入任何新的包,我只是将数据从csv导入到对象clusterd_data并使用了kmeans()的功能
  • 好的,知道了。问题是kmeans 想要一个数字矩阵作为其第一个参数,而您的数据有一个列,第二个列不是数字。

标签: r dataframe cluster-analysis k-means


【解决方案1】:

K-means 只能处理连续变量。

否则,它无法计算 mean

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 2017-12-19
    • 2017-11-07
    • 2019-08-06
    • 2018-07-31
    • 2016-02-02
    • 2016-06-28
    相关资源
    最近更新 更多