PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 17, 2022

[FIXED] How can I treat a "empty cluster" warning, in K-means function?

 July 17, 2022     error-handling, k-means, r, warnings     No comments   

Issue

I'm searching for a solution to treat a warning message, from the Kmeans function of amap package. The warning message is the following:

empty cluster: try a better set of initial centers.

Is there anyway I could get a signal, so could know when this error message thrown, and then handle the problem? (e.g: running the algorithm until the there is the return has no empty cluster)

It is quite hard to make a nice reproducible example for me. But, I came with this ugly, but functional:

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)

If you run this on you terminal, it might send you the warning message I'm talking about.

Note: My thoughts of solving this problem is catching the signal of the warning, and then execute the solution. However, I have no idea how can I possibly do that


Solution

From ?options (scrolling down a long ways to find 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.

So using tryCatch you can specify a warning handler function to do stuff upon catching a warning:

> tryCatch(expr = {Kmeans((time.series), centers = centers, iter.max = 99)},
         warning = function(e) "Caught warning")
[1] "Caught warning"

Or you can set all warnings to be escalated to errors via:

options(warn = 2)

as described in the docs. Then,

> tryCatch(expr = {Kmeans((time.series), centers = centers, iter.max = 99)},
           error = function(e) "Caught error")
[1] "Caught error"

Although many people seem to prefer tryCatch, I often like the explicitness of try, which feels easier to me if I want to do some sort of if...else block after running the expression:

options(warn = 2)
attempt <- try(expr = {Kmeans((time.series), centers = centers, iter.max = 99)},silent = TRUE)
> class(attempt)
[1] "try-error"

So then you can check class(attempt) in an if statement (the preferred way is to check inherits(attempt,"try-error")) and do stuff accordingly.



Answered By - joran
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing