1

I'm not sure how to get rid of this error. Below is my example datasets. Is there another step that I'm missing?

enter image description here

Code below: 
from sklearn.model_selection import train_test_split 
from sklearn.ensemble import RandomForestClassifier 
models = RandomForestClassifier(n_estimators=100) 
np.random.seed(42)

X = re_arrange.drop('Gender',axis=1) 
y = re_arrange['Gender']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

models.fit(X_train,y_train)
models.score(X_test, y_test)
2
  • 3
    RandomForestClassifier requires features to be numerical (float or int). Branch is string and cannot be converted to float. You should look at either categorical or one-hot-encoder. Commented May 26, 2020 at 3:55
  • @QuangHoang appreicate the fast response. will look into this Commented May 26, 2020 at 4:06

2 Answers 2

2

Your column "Branch" has letters whereas the RandomForestClassifier expects numbers. I believe it is of categorical type. So you can encode the column "Branch" using some categorical encoding as shown below before you do train test split

X["Branch"] = pd.get_dummies(X["Branch"])

It will map letters 'A', 'B' etc in numbers. It does not change your data but just converts them in computational-friendly state

Sign up to request clarification or add additional context in comments.

1 Comment

appreicate the fast response. never knew what get_dummies was. going to play around with this as well.
0

RandomForestClassifier can handle only numerical values in any of its features. As you can see, you have text/object data in almost all your features. So 1st of all: do X.info() to know the data type of your features. If you find 'string' & 'object', encode all those features in numbers using One-Hot-Encoder or LabelEncoding.

One-Hot-Encoding

LabelEncoding

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.