Issue
I'm currently learning Random Forest for Sklearn. Output from feature_importances_ is a floating points as below. If I plot these as a graph, it is showing until 2 decimals but table is not. Could you let me know how I can limit these numbers? I saw some of the codes here but not working well.
rf.feature_importances_
array([6.92336967e-03, 8.60481032e-03, 6.18622612e-03, 7.71638229e-03,
4.21263594e-03, 3.37402779e-03, 8.14965067e-04, 7.00722079e-03,
5.83454373e-03, 8.59467929e-03, 5.98548992e-03, 4.62940042e-03,
1.55387211e-03, 1.51361606e-03, 6.75245234e-04, 9.46365248e-04,
2.87923249e-17, 4.55094829e-05, 8.49311652e-03, 2.02949840e-02,
8.56575989e-03, 4.47695994e-03, 6.81972091e-03, 7.68965578e-03,
7.03035697e-03, 6.81720151e-03, 1.17788767e-02, 6.71457293e-03,
6.46187701e-03, 2.68229216e-03, 1.78285095e-03, 5.57750940e-04,
1.20311774e-02, 9.01830930e-03, 3.93380252e-03, 0.00000000e+00,
5.83635286e-02, 2.18481659e-02, 2.01768748e-02, 9.69475232e-02,
6.85297377e-02, 2.88869920e-01, 7.93765418e-02, 1.28664918e-01,
3.74551656e-02])
Solution
Numpy has one method to do this:
np.around(data, 2)
You can find more documentation here
For example, for you data, if you execute the following code:
np.around(array_data, 4) # array_data is your data that will be transformed, and number 4 refers to the number of decimals you want
You will obtain:
array([0.0069, 0.0086, 0.0062, 0.0077, 0.0042, 0.0034, 0.0008, 0.007 ,
0.0058, 0.0086, 0.006 , 0.0046, 0.0016, 0.0015, 0.0007, 0.0009,
0. , 0. , 0.0085, 0.0203, 0.0086, 0.0045, 0.0068, 0.0077,
0.007 , 0.0068, 0.0118, 0.0067, 0.0065, 0.0027, 0.0018, 0.0006,
0.012 , 0.009 , 0.0039, 0. , 0.0584, 0.0218, 0.0202, 0.0969,
0.0685, 0.2889, 0.0794, 0.1287, 0.0375])
Answered By - Alex Serra Marrugat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.