4

I am using a supervised learning algorithm Random Forest classifier for training the data.

    clf = RandomForestClassifier(n_estimators=50, n_jobs=3, random_state=42)

Different parameter in the grid are:

    param_grid = { 
    'n_estimators': [200, 700],
    'max_features': ['auto', 'sqrt', 'log2'],
    'max_depth': [5,10],
    'min_samples_split': [5,10]
    }

Classifier "clf" and parameter grid "param_grid" are passed in the GridSearhCV method.

    clf_rfc = GridSearchCV(estimator=clf, param_grid=param_grid)

When I fit the features with labels using

    clf_rfc.fit(X_train, y_train)

I get the error "Too many indices in the array". Shape of X_train is (204,3) and of y_train is (204,1).

Tried with the option clf_rfc.fit(X_train.values, y_train.values) but could not get rid of the error.

Any suggestions would be appreciated !!

2
  • Please post the full stack trace of error. Commented Mar 21, 2017 at 14:28
  • Also try reshaping your y_train to y_train.reshape(204) to make it a sequence from a 1-d array Commented Mar 21, 2017 at 14:33

3 Answers 3

5

As mentioned in previous post the problems appears to be in y_train which dimensions are (204,1). I think this is the problem instead of (204,1) should be (204,), click here for more info.

So if you rewrite y_train everything should be fine:

c, r = y_train.shape
y_train = y_train.reshape(c,)

If it gives as error such as: AttributeError: 'DataFrame' object has no attribute 'reshape' then try:

c, r = y_train.shape
y_train = y_train.values.reshape(c,)
Sign up to request clarification or add additional context in comments.

Comments

1

The shape of the 'y-train' dataframe is not correct. Try this:

clf_rfc.fit(X_train, y_train[0].values)

OR

clf_rfc.fit(X_train, y_train.values.ravel())

Comments

1

y_train should be a 1-dimensional array

I have tried clf_rfc.fit(X_train, y_train.flatten()), and it did work!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.