2
$\begingroup$

I have the following code

rf = RandomForestClassifier()  
rf.fit(X_train, Y_train)  
print ("Features sorted by their score:")
print (sorted(zip(map(lambda x: round(x, 2), rf.feature_importances_), X_train), reverse=True))

and I get the following error:

TypeError Traceback (most recent call last)

ipython-input-109-c48c3ffd74e2> in ()

2 rf.fit(X_train, Y_train)

3 print ("Features sorted by their score:")

----> 4 print (sorted(zip(map(lambda x: round(x, 2), rf.feature_importances_), X_train), reverse=True))

TypeError: '<' not supported between instances of 'int' and 'str'

I am not sure what I am doing wrong. I only have int and float in my dataframe.

$\endgroup$
  • $\begingroup$ What library are you attempting to use? $\endgroup$ – Media Sep 21 '18 at 18:12
  • $\begingroup$ I am just using Numpy, Pandas and sklearn $\endgroup$ – TigSh Sep 21 '18 at 18:16
  • $\begingroup$ I guess rf.feature_importances_ are strings. $\endgroup$ – Media Sep 21 '18 at 18:19
  • $\begingroup$ I checked. Its decimal values $\endgroup$ – TigSh Sep 21 '18 at 18:20
  • $\begingroup$ What about X_train? $\endgroup$ – Media Sep 21 '18 at 18:22
0
$\begingroup$

Based on our discussion, omit reverse = True and use list instead of sorted and print it. I guess you will see what you want.

$\endgroup$
  • $\begingroup$ Have you found the answer why the error raised? $\endgroup$ – bakka Sep 21 '18 at 20:02
  • $\begingroup$ Yes, actually. Have you seen here? $\endgroup$ – Media Sep 21 '18 at 20:07
  • $\begingroup$ I still don't understand. I tried above code, and no error. $\endgroup$ – bakka Sep 21 '18 at 20:12
  • $\begingroup$ Do you have the same data? $\endgroup$ – Media Sep 21 '18 at 20:47
  • $\begingroup$ No. ok then it might be something wrong in X_train $\endgroup$ – bakka Sep 21 '18 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.