Why does visualising my KNN solution keep throwing an error?

I have put together an analytical piece on the Cleveland Heart Disease dataset and have built a number of ML solutions.

One of those solutions is KNN (K-Nearest Neighbours) and this works fine. However, when I attempt to visualise the solution, the following error is thrown:

Error in knn(train = train[, -14], test = grid_set, cl = train[, 14],  : 
  dims of 'test' and 'train' differ

The error is self-explanatory, so I go back through the code and check it line-by-line and, sure enough, the dimensions of train and test are indeed different.

The trouble is that I am using visualisation code that I have from a ML course that I took several years ago and am following that example, but no matter what I try, I can't seem to get it to work.

Once again, the solution itself, which I built, works fine, but I really want to understand what the code that I have from the other source is actually doing so that I can fully understand what is going wrong.

The full code is quite lengthy, so I won't post it here as it may take up too much space, although I think it is useful to see how the datasets are constructed and mutated which is why I would like the whole piece to be visible.

As such, it can be found on this Hastebin.

Any suggestions and/or explanations as to what the visualisation code is doing (especially around variables X1, X2 and subsequently grid_set) would be most welcomed.

Things change in R over time, and sometimes this means that old code no longer works, particularly if they relied on a bug that has now been fixed. Usually the code will throw and error or warning after the change.

For your example, maybe it used to silently recycle the data that was the wrong length, giving you a (silently wrong) result for your analysis. This kind of recycling is being removed from R. Just a guess.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.