R误差中的K-均值聚类
我在R中创建了一个数据集,其结构如下:R误差中的K-均值聚类,r,machine-learning,k-means,R,Machine Learning,K Means,我在R中创建了一个数据集,其结构如下: > head(btc_data) Date btc_close eth_close vix_close gold_close DEXCHUS change 1647 2010-07-18 0.09 NA NA NA NA 0 1648 2010-07-19 0.08 NA 25.97 115.730 NA
> head(btc_data)
Date btc_close eth_close vix_close gold_close DEXCHUS change
1647 2010-07-18 0.09 NA NA NA NA 0
1648 2010-07-19 0.08 NA 25.97 115.730 NA -1
1649 2010-07-20 0.07 NA 23.93 116.650 NA -1
1650 2010-07-21 0.08 NA 25.64 115.850 NA 1
1651 2010-07-22 0.05 NA 24.63 116.863 NA -1
1652 2010-07-23 0.06 NA 23.47 116.090 NA 1
我正试图用k-均值对观测值进行聚类。但是,我收到以下错误消息:
> km <- kmeans(trainingDS, 3)
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
> complete_cases <- btc_data[complete.cases(btc_data), ]
> km <- kmeans(complete_cases, 3, nstart = 20)
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
> sum(!sapply(btc_data, is.finite))
[1] 8008
> sum(sapply(btc_data, is.nan))
[1] 0
>
> sum(!sapply(complete_cases, is.finite))
[1] 0
> sum(sapply(complete_cases, is.nan))
[1] 0
以下是数据的格式:
> sapply(btc_data, class)
Date btc_close eth_close vix_close gold_close DEXCHUS change
"Date" "numeric" "numeric" "numeric" "numeric" "numeric" "factor"
获取此错误消息有多种原因,特别是在存在无效数据类型(NA、NaN、Inf)或日期的情况下。让我们看一下: 但首先,让我们检查一下它是否适用于
mtcars
数据集,因为我将使用它:
kmeans(mtcars, 3)
K-means clustering with 3 clusters of sizes 9, 7, 16
--- lengthy output omitted
可能的问题1:无效的数据类型:NA/NaN/Inf
df <- mtcars
df[1,1] <- NA
kmeans(df, 3)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
df[1,1] <- Inf
kmeans(df, 3)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
df[1,1] <- NaN
kmeans(df, 3)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
相反,使用例如
df[apply(sapply(df, is.finite), 1, all),]
您也可以重新分配或插补这些值,但这是一个完全不同的过程
可能的问题二:日期:请参见以下内容:
df[1:3,1] <- c(NA, Inf, NaN) # one NA, one Inf, one NaN
sum(sapply(df, is.na))
[1] 2
sum(sapply(df, is.infinite))
[1] 1
sum(sapply(df, is.nan))
[1] 1
library(lubridate)
df <- mtcars
df$date <- seq.Date(from=ymd("1990-01-01"), length.out = nrow(df), by=1)
kmeans(df, 3)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning message:
In kmeans(df, 3) : NAs introduced by coercion
您是否在集群中使用了“日期”列
在使用k-means聚类时,应该使用数字类型数据
试试这个
btc_数据$Date=as.numeric(gsub(“-”,“”),as.character(btc_数据$Date)))
检查要对其进行群集的变量的数据类型。如果数据类型是非数字的,则最有可能出现错误。在群集之前,请尝试正确处理日期格式。我理解,但有两件事-当我删除NA时,数据很小。更重要的是,我运行了
complete_cases
,然后执行了knn
,但我仍然得到相同的错误。如果您没有(足够)数据,我们真的无法为您提供太多帮助。您唯一可以尝试的是输入数据,而不是删除数据。关于你的第二点,我不能对此发表评论,因为你没有发布那个代码或那个错误消息。>sum(!sappy(btc_数据,is.finite))[1]8008>sum(!sappy(btc_数据,is.nan))[1]18193
@coffeinjunky我编辑了这个问题,我不知道为什么完整案例的总和是721
,特别是因为只有103个观察结果…当我手动检查df时,没有NA'sOk-我删除了
并编辑了问题…您的意思是,knn
应该可以处理完成案例
,但我仍然收到相同的错误消息OK,我添加了该信息很酷。在这种情况下,日期列可能是罪魁祸首。不需要gsub/as.character
,只需要as.numeric(btc_data$date)
就可以了。这并没有给一年前的现有答案和公认答案增加任何内容。
library(lubridate)
df <- mtcars
df$date <- seq.Date(from=ymd("1990-01-01"), length.out = nrow(df), by=1)
kmeans(df, 3)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning message:
In kmeans(df, 3) : NAs introduced by coercion
df$newdate <- seq_along(df$date)
df$date <- NULL
kmeans(df, 3)
K-means clustering with 3 clusters of sizes 9, 7, 16
---- lengthy output omitted
df <- mtcars
df$date <- seq.Date(from=ymd("1990-01-01"), length.out = nrow(df), by=1)
df$date <- as.numeric(df$date)
kmeans(df, 3)
K-means clustering with 3 clusters of sizes 9, 16, 7
--- lengthy output omitted