Skip to content

Single feature RandomForestClassifier throws index out of range exception #5

Description

@lichard49

I've built a very simple single feature RandomForestClassifier:

from sklearn.ensemble import RandomForestClassifier
import numpy as np

from sklearn_porter import Porter

rf = RandomForestClassifier()
features = [[i] for i in xrange(0, 10)]
labels = [i > 5 for i in xrange(0, 10)]

rf.fit(features, labels)

for feature in xrange(-20, 20):
	print feature, '->', rf.predict(np.array([feature]).reshape(1, -1))

result = Porter(language='java').port(rf)
print result

which gives the following stack trace:

Traceback (most recent call last):
  File "generateModel.py", line 21, in <module>
    result = Porter(language='java').port(rf)
  File "/usr/local/lib/python2.7/dist-packages/sklearn_porter/__init__.py", line 72, in port
    ported_model = instance.port(model)
  File "/usr/local/lib/python2.7/dist-packages/sklearn_porter/classifier/RandomForestClassifier/__init__.py", line 84, in port
    return self.predict()
  File "/usr/local/lib/python2.7/dist-packages/sklearn_porter/classifier/RandomForestClassifier/__init__.py", line 95, in predict
    return self.create_class(self.create_method())
  File "/usr/local/lib/python2.7/dist-packages/sklearn_porter/classifier/RandomForestClassifier/__init__.py", line 198, in create_method
    tree = self.create_single_method(idx, model)
  File "/usr/local/lib/python2.7/dist-packages/sklearn_porter/classifier/RandomForestClassifier/__init__.py", line 162, in create_single_method
    indices.append([str(j) for j in range(model.n_features_)][i])
IndexError: list index out of range

The line in question involves indexing into the feature vector, but sometimes the index is negative, which is fine except when it wraps around the list twice. In this case, model.n_features_ is 1 but i (the index) is -2, giving the list out of range exception. What is the best solution for this? Would simply taking the modulus of the index by the length of list be correct?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions