2

I'm trying to use a RandomForestClassifier on some data I have. The code is below:

print train_data[0,0:20]
print train_data[0,21::]
print test_data[0]

print 'Training...'
forest = RandomForestClassifier(n_estimators=100)
forest = forest.fit( train_data[0::,0::20], train_data[0::,21::] )

print 'Predicting...'
output = forest.predict(test_data)

but this generates the following error:

ValueError: Number of features of the model must match the input. Model n_features is 3 and input n_features is 21

The output from the first three print statements is:

[   0.            0.            0.            0.            1.            0.
    0.            0.            0.            0.            1.            0.
    0.            0.            0.           37.7745986  -122.42589168
    0.            0.            0.        ]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  1.  0.]
[   0.            0.            0.            0.            0.            0.
    0.            1.            0.            0.            1.            0.
    0.            0.            0.            0.           37.73505101
 -122.3995877     0.            0.            0.        ]

I had assumed that the data was in the correct format for my fit/predict calls, but it is erroring out on the predict. Can anyone see what I am doing wrong here?

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486

1 Answers1

1

The input data used to train the model is train_data[0::,0::20], which I think is a mistake (why skip features in between?) -- it should be train_data[0::,0:20] instead based on the debug prints you did in the beginning.

Also, it seems that the last column represents the labels in both train_data and test_data. When predicting, you might want to pass test_data[:, :20] instead of test_data when calling thepredict function.

YS-L
  • 14,358
  • 3
  • 47
  • 58
  • I support this answer and would like to add, that when fitting the classifier you do not need an extra assignment. Your line of code with "fit" should look like `forest.fit( train_data[:,:21], train_data[:,21:])` (assuming the first 21 columns with indexes from 0 to 20 are features and the rest columns with indexes from 21 till the last column are labels) – lanenok Sep 20 '15 at 16:17