1
votes

I am using sklearn's nearest neighbor for a classification problem. My features are patches of the shape (3600, 2, 5). For example:

a = [[5,5,5,5,5], [5,5,5,5,5]]
b = [[5,5,5,5,5], [5,5,5,5,5]]

features = []

for i in xrange(len(a)):
    features.append([a[i], b[i]]) #I have 3600 of these in reality.

neigh = KNeighborsClassifier()

neigh.fit(train_features, target_class)

predictions = neigh.predict(test_features)

I get the error:

ValueError: Found array with dim 3. Expected <= 2

This is on the line where I use neigh.fit. Target class is a 1 dimensional array with corresponding classes. The train_features, test_features are simply the data split into training and testing data.

Link to the same error: check_arrays() limiting array dimensions in scikit-learn?

Is there a fix for this?

2

2 Answers

3
votes

Sounds like you want to use features which are themselves multi-dimensional. I'm not sure this works. Consider the increase in complexity that would occur for a distance-based metric like KNN; multi-dimensional features would require distance metrics and would get a lot more involved.

I'd first try just flattening the arrays, so that each exemplar is an n-dimensional vector, rather than an array. So if features[0] in your current approach is given by:

[[5, 5, 5, 5, 5], [5, 5, 5, 5, 5]]

you can instead go with:

[5, 5, 5, 5, 5, 5, 5, 5, 5, 5] 

You could also consider creating new features that represent the information carried by the multi-dimensional features. You could use the mean, the max, the min, etc. Here's where your domain-specific knowledge kicks in.

0
votes

It seams this it not possible.

But you can flatten features into a 1-d array than use a custom distance metric (use metric parameter) that before computing the distance reconstructs your features. Of course that you have to write the distance metric yourself, too. But I don`t think this is an issue.