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 <module>()
> 
> 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$
10
  • $\begingroup$ What library are you attempting to use? $\endgroup$ Commented Sep 21, 2018 at 18:12
  • $\begingroup$ I am just using Numpy, Pandas and sklearn $\endgroup$
    – TigSh
    Commented Sep 21, 2018 at 18:16
  • $\begingroup$ I guess rf.feature_importances_ are strings. $\endgroup$ Commented Sep 21, 2018 at 18:19
  • $\begingroup$ I checked. Its decimal values $\endgroup$
    – TigSh
    Commented Sep 21, 2018 at 18:20
  • $\begingroup$ What about X_train? $\endgroup$ Commented Sep 21, 2018 at 18:22

1 Answer 1

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$
5
  • $\begingroup$ Have you found the answer why the error raised? $\endgroup$ Commented Sep 21, 2018 at 20:02
  • $\begingroup$ Yes, actually. Have you seen here? $\endgroup$ Commented Sep 21, 2018 at 20:07
  • $\begingroup$ I still don't understand. I tried above code, and no error. $\endgroup$ Commented Sep 21, 2018 at 20:12
  • $\begingroup$ Do you have the same data? $\endgroup$ Commented Sep 21, 2018 at 20:47
  • $\begingroup$ No. ok then it might be something wrong in X_train $\endgroup$ Commented Sep 21, 2018 at 21:02

Your Answer

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

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