[英]K-Means clustering in R error
我有一個我在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 -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-means聚類觀察。 但是,我收到以下錯誤消息:
> 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
這是什么意思? 我是否錯誤地預處理了數據? 我該怎么辦才能修復它? 我不能放棄NA的,因為在4500次初始觀察中,如果我運行complete cases
我只剩下100次觀察。
從本質上講,我希望3個集群將形成基礎上, change
其中有1,0,1值列。 然后,我希望分析每個群集的組件,以找到最強的變化預測因子。 還有哪些其他最有用的算法呢?
我還嘗試使用以下代碼刪除所有NA值,但我仍然得到相同的錯誤消息:
> 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[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
為了擺脫這些,我們可以刪除相應的觀察結果。 但請注意, complete.cases
不會刪除Inf
:
complete_df <- df[complete.cases(df),]
sum(sapply(complete_df, is.infinite))
[1] 1
相反,使用例如
df[apply(sapply(df, is.finite), 1, all),]
您也可以重新分配這些值或將它們歸為它們,但這是一個完全不同的過程。
可能的問題II:日期:請參閱以下內容:
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
或者,在將日期傳遞給kmeans
之前,您可以嘗試將日期強制為數字:
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
檢查要聚類的變量的數據類型。 如果數據類型是非數字的,很可能會出現錯誤。 在群集之前,還要嘗試正確處理日期格式。
您是否在群集中使用“日期”列?
您應該使用數字類型數據來使用k-means聚類。
試試這個,
btc_data $ Date = as.numeric(gsub(“ - ”,“”,as.character(btc_data $ Date)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.