如何在 K-means 函数中处理 "empty cluster" 警告?
How can I treat a "empty cluster" warning, in K-means function?
我正在从 amap
包的 Kmeans
函数中寻找处理警告消息的解决方案。警告信息如下:
empty cluster: try a better set of initial centers
。
有没有办法让我得到一个信号,这样我就可以知道什么时候抛出这个错误信息,然后处理这个问题? (例如:运行调整算法直到 return 没有空簇)
很难为我制作一个很好的可复制示例。但是,我带来了这个丑陋但实用的东西:
library(amap)
numberK = 20
ts.len = 7
time.series <- rep(sample(1:8000, numberK, replace = TRUE),ts.len)
time.series <- rep(rbind(time.series, time.series), 30)
time.series <- matrix(time.series, ncol = ts.len)
centers <- matrix( sample(1:3000, numberK*ts.len), ncol = ts.len)
Kmeans((time.series), centers = centers, iter.max = 99)
如果你 运行 在你的终端上使用它,它可能会向你发送我正在谈论的警告消息。
注意: 我解决这个问题的思路是捕捉警告信号,然后执行解决方案。但是,我不知道我该怎么做
来自 ?options
(向下滚动很长一段路找到 warn
...):
sets the handling of warning messages. If warn is negative all
warnings are ignored. If warn is zero (the default) warnings are
stored until the top–level function returns. If 10 or fewer warnings
were signalled they will be printed otherwise a message saying how
many were signalled. An object called last.warning is created and can
be printed through the function warnings. If warn is one, warnings are
printed as they occur. If warn is two or larger all warnings are
turned into errors.
所以使用 tryCatch
你可以指定一个 warning
处理函数来处理警告:
> tryCatch(expr = {Kmeans((time.series), centers = centers, iter.max = 99)},
warning = function(e) "Caught warning")
[1] "Caught warning"
或者您可以通过以下方式将所有警告升级为错误:
options(warn = 2)
如文档中所述。那么,
> tryCatch(expr = {Kmeans((time.series), centers = centers, iter.max = 99)},
error = function(e) "Caught error")
[1] "Caught error"
虽然很多人似乎更喜欢 tryCatch
,但我通常喜欢 try
的明确性,如果我想在 运行 表达式:
options(warn = 2)
attempt <- try(expr = {Kmeans((time.series), centers = centers, iter.max = 99)},silent = TRUE)
> class(attempt)
[1] "try-error"
那么你可以在 if
语句中检查 class(attempt)
(首选方法是检查 inherits(attempt,"try-error")
)并相应地做一些事情。
我正在从 amap
包的 Kmeans
函数中寻找处理警告消息的解决方案。警告信息如下:
empty cluster: try a better set of initial centers
。
有没有办法让我得到一个信号,这样我就可以知道什么时候抛出这个错误信息,然后处理这个问题? (例如:运行调整算法直到 return 没有空簇)
很难为我制作一个很好的可复制示例。但是,我带来了这个丑陋但实用的东西:
library(amap)
numberK = 20
ts.len = 7
time.series <- rep(sample(1:8000, numberK, replace = TRUE),ts.len)
time.series <- rep(rbind(time.series, time.series), 30)
time.series <- matrix(time.series, ncol = ts.len)
centers <- matrix( sample(1:3000, numberK*ts.len), ncol = ts.len)
Kmeans((time.series), centers = centers, iter.max = 99)
如果你 运行 在你的终端上使用它,它可能会向你发送我正在谈论的警告消息。
注意: 我解决这个问题的思路是捕捉警告信号,然后执行解决方案。但是,我不知道我该怎么做
来自 ?options
(向下滚动很长一段路找到 warn
...):
sets the handling of warning messages. If warn is negative all warnings are ignored. If warn is zero (the default) warnings are stored until the top–level function returns. If 10 or fewer warnings were signalled they will be printed otherwise a message saying how many were signalled. An object called last.warning is created and can be printed through the function warnings. If warn is one, warnings are printed as they occur. If warn is two or larger all warnings are turned into errors.
所以使用 tryCatch
你可以指定一个 warning
处理函数来处理警告:
> tryCatch(expr = {Kmeans((time.series), centers = centers, iter.max = 99)},
warning = function(e) "Caught warning")
[1] "Caught warning"
或者您可以通过以下方式将所有警告升级为错误:
options(warn = 2)
如文档中所述。那么,
> tryCatch(expr = {Kmeans((time.series), centers = centers, iter.max = 99)},
error = function(e) "Caught error")
[1] "Caught error"
虽然很多人似乎更喜欢 tryCatch
,但我通常喜欢 try
的明确性,如果我想在 运行 表达式:
options(warn = 2)
attempt <- try(expr = {Kmeans((time.series), centers = centers, iter.max = 99)},silent = TRUE)
> class(attempt)
[1] "try-error"
那么你可以在 if
语句中检查 class(attempt)
(首选方法是检查 inherits(attempt,"try-error")
)并相应地做一些事情。