
Re: ClassificationKNN.fit with cell array of strings as Y
Posted:
May 7, 2013 10:53 AM


On 5/7/2013 10:39 AM, Jay wrote: > Hello! > > I believe there is either a bug in ClassificationKNN.predict or the > documentation to ClassificationKNN.fit is wrong (or both). > 1. > The documentation states, that Y is supposed to be a numeric vector, > however in the example how to construct a KNN classifier 'species' of > the dataset 'fisheriris' is used as Y which is a cell array of strings. > > 2. > I have initialized my KNN classifier how it is done in the example, > using a cell array of strings as Y. My "training" looks like this: > mdl = ClassificationKNN.fit(featMat(train,:),classLabels(train,:), > 'BreakTies', 'nearest', 'NumNeighbors', testParams.k, 'Distance', > @myDistance); > > If I now use classes = predict(mdl, featMat(test,:)); > I occasionally get errors. I believe this is the case when there are > actually ties. The error is: > Conversion to cell from double is not possible. > > Error in ClassificationKNN/predict (line 609) > label(notNanRows(i)) = > CNeighbor(notNanRows(i),inner); > > What can I do about it? Is that actually because I used a cell array > of strings as Y? Do I have to convert my class labels to numbers then? > > 3. > Also, while I am at it: If I want to see what examples were close to > an input observation and influenced the classification (so to speak > the nearest neighbors) I have no chance of doing that, do I? For that > I would have to switch entirely to knnsearch? > > Thank you!
I am not sure where in the documentation you found your statement 1. Here's what I see when I look at the current documentation: http://www.mathworks.com/help/stats/classificationknn.fit.html
Y  Grouping variables of response values with the same number of elements (rows) as X. Each entry in Y is the response to the data in the corresponding row of X.
This does not say that the variable must be a numeric vector, but a grouping variable http://www.mathworks.com/help/stats/groupingvariables.html
As far as your problem 2, I am not sure what is going on. I wonder if your function myDistance is giving you a problem somehow.
Alan Weiss MATLAB mathematical toolbox documentation

