2017-02-23 1 views
0

안녕하세요 저는 여러 모델의 무작위 포리스트로 작업하고 있습니다. 내 모델의 k 최고의 기능을 얻고 싶습니다.내 모델에서 가장 중요한 k 개의 기능을 가진 그래프를 얻는 방법은 무엇입니까?

내 모델에서 더 관련성이 높은 3, 4 또는 k 기능을 의미합니다.

그러나이 방법의 문제점은 모든 기능을 음모를 꾸미는 것입니다. 많은 것을 컴퓨팅하고 있기 때문에 이것은 내가 원하는대로 해석 할 수 없으므로 수정할 지원이 필요합니다. 위의 코드는 기능의 고정 숫자 의 그래프를 얻으려는 매개 변수로 고치고 싶습니다.

여기

내가 내 모델의 단지 K 최적의 값을 얻기 위해 수정하고 싶은 부분하십시오 후

print("PREDICTION REPORT") 
# importing Confusion Matrix and recall 
from sklearn.metrics import confusion_matrix 
from sklearn.metrics import precision_recall_fscore_support 
from sklearn.metrics import confusion_matrix 

print(precision_recall_fscore_support(y_test, y_pred, average='macro')) 
print(confusion_matrix(y_test, y_pred)) 

# to print unique values 
print(set(y_test)) 
print(set(y_pred)) 

from sklearn.metrics import classification_report 
print(classification_report(y_test, y_pred)) 


Output: 

Feature ranking: 
1. feature 660 (0.403711) 
2. feature 655 (0.139531) 
3. feature 659 (0.058074) 
4. feature 658 (0.057855) 
5. feature 321 (0.015031) 
6. feature 322 (0.012731) 
7. feature 324 (0.011937) 
8. feature 336 (0.011728) 
9. feature 650 (0.011174) 
10. feature 656 (0.010441) 
11. feature 657 (0.009340) 
12. feature 337 (0.007385) 
13. feature 509 (0.005184) 
14. feature 330 (0.005056) 
15. feature 325 (0.004927) 
16. feature 344 (0.004891) 
17. feature 326 (0.004495) 
18. feature 334 (0.004349) 
19. feature 333 (0.004291) 
20. feature 352 (0.004284) 
21. feature 338 (0.004164) 
22. feature 285 (0.003909) 
23. feature 345 (0.003631) 
24. feature 652 (0.003341) 
25. feature 329 (0.003168) 
26. feature 651 (0.002890) 
27. feature 388 (0.002680) 
28. feature 146 (0.002650) 
29. feature 332 (0.002482) 
30. feature 217 (0.002475) 
31. feature 513 (0.002363) 
32. feature 216 (0.002309) 
33. feature 116 (0.002223) 
34. feature 323 (0.002107) 
35. feature 213 (0.002104) 
36. feature 328 (0.002101) 
37. feature 102 (0.002088) 
38. feature 315 (0.002083) 
39. feature 307 (0.002079) 
40. feature 427 (0.002043) 
41. feature 351 (0.001925) 
42. feature 259 (0.001888) 
43. feature 171 (0.001878) 
44. feature 243 (0.001863) 
45. feature 78 (0.001862) 
46. feature 490 (0.001815) 
47. feature 339 (0.001770) 
48. feature 103 (0.001767) 
49. feature 591 (0.001741) 
50. feature 55 (0.001734) 
51. feature 502 (0.001665) 
52. feature 194 (0.001632) 
53. feature 491 (0.001625) 
54. feature 50 (0.001591) 
55. feature 193 (0.001590) 
56. feature 97 (0.001549) 
57. feature 510 (0.001514) 
58. feature 245 (0.001504) 
59. feature 434 (0.001497) 
60. feature 8 (0.001468) 
61. feature 241 (0.001457) 
62. feature 108 (0.001454) 
63. feature 232 (0.001453) 
64. feature 292 (0.001443) 
65. feature 96 (0.001434) 
66. feature 99 (0.001381) 
67. feature 11 (0.001367) 
68. feature 106 (0.001360) 
69. feature 592 (0.001335) 
70. feature 60 (0.001334) 
71. feature 523 (0.001327) 
72. feature 72 (0.001324) 
73. feature 236 (0.001323) 
74. feature 128 (0.001320) 
75. feature 144 (0.001318) 
76. feature 288 (0.001300) 
77. feature 238 (0.001292) 
78. feature 654 (0.001287) 
79. feature 499 (0.001285) 
80. feature 223 (0.001283) 
81. feature 593 (0.001275) 
82. feature 33 (0.001264) 
83. feature 289 (0.001240) 
84. feature 94 (0.0) 
85. feature 433 (0.0) 
86. feature 129 (0.001227) 
87. feature 437 (0.001226) 
88. feature 113 (0.001221) 
89. feature 54 (0.001220) 
90. feature 271 (0.001213) 
91. feature 107 (0.001186) 
92. feature 562 (0.001165) 
93. feature 488 (0.001144) 
94. feature 521 (0.001128) 
95. feature 269 (0.001110) 
96. feature 313 (0.001102) 
97. feature 13 (0.001063) 
98. feature 59 (0.001059) 
99. feature 529 (0.001059) 
100. feature 278 (0.001055) 
101. feature 68 (0.001053) 
102. feature 189 (0.001038) 
103. feature 176 (0.001001) 
104. feature 367 (0.001000) 
105. feature 32 (0.001000) 
106. feature 18 (0.000984) 
107. feature 135 (0.000957) 
108. feature 127 (0.000933) 
109. feature 39 (0.000924) 
110. feature 391 (0.000921) 
111. feature 156 (0.000919) 
112. feature 316 (0.000904) 
113. feature 389 (0.000895) 
114. feature 522 (0.000885) 
115. feature 449 (0.000874) 
116. feature 4 (0.000872) 
117. feature 258 (0.000840) 
118. feature 489 (0.000828) 
119. feature 347 (0.000823) 
120. feature 264 (0.000790) 

:

importances = clf2.feature_importances_ 
std = np.std([tree.feature_importances_ for tree in clf2.estimators_], 
      axis=0) 
indices = np.argsort(importances)[::-1] 

# Print the feature ranking 
print("Feature ranking:") 

for f in range(X_train.shape[1]): 
    print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]])) 

#Plot the feature importances of the forest 
plt.figure() 
plt.title("Feature importances") 
plt.bar(range(X_train.shape[1]), importances[indices], 
     color="r", yerr=std[indices], align="center") 
plt.xticks(range(X_train.shape[1]), indices) 
plt.xlim([-1, X_train.shape[1]]) 
plt.savefig('fig1.png', dpi = 600) 

plt.show() 

이 코드의 다른 부분입니다 여기에서 나는 피드백을 시도했다 :

importances = clf2.feature_importances_ 
std = np.std([tree.feature_importances_ for tree in clf2.estimators_], 
      axis=0) 

indices = np.argsort(importances)[::-1] 
top_k = 10 
new_indices = indices[:top_k] 
#So you just need to change this part accordingly (just change top_k to your desired value): 

# Print the feature ranking 
print("Feature ranking:") 

for f in range(top_k): 
    print("%d. feature %d (%f)" % (f + 1, new_indices[f], importances[new_indices[f]])) 
#Same here for plotting the graph: 

#Plot the feature importances of the forest 
plt.figure() 
plt.title("Feature importances") 
plt.bar(range(top_k), importances[new_indices], 
     color="r", yerr=std[new_indices], align="center") 

    importances = clf2.feature_importances_ 
std = np.std([tree.feature_importances_ for tree in clf2.estimators_], 
      axis=0) 

indices = np.argsort(importances)[::-1] 
top_k = 10 
new_indices = indices[:top_k] 
#So you just need to change this part accordingly (just change top_k to your desired value): 

# Print the feature ranking 
print("Feature ranking:") 

for f in range(top_k): 
    print("%d. feature %d (%f)" % (f + 1, new_indices[f], importances[new_indices[f]])) 
#Same here for plotting the graph: 

#Plot the feature importances of the forest 
plt.figure() 
plt.title("Feature importances") 
plt.bar(range(top_k), importances[new_indices], 
     color="r", yerr=std[new_indices], align="center") 


plt.xticks(range(new_indices), new_indices) 
plt.xlim([-1, new_indices]) 
plt.savefig('fig1.png', dpi = 600) 
plt.show() 
plt.xticks(range(new_indices), new_indices) 
plt.xlim([-1, new_indices]) 
plt.savefig('fig1.png', dpi = 600) 
plt.show() 

그러나 나는 다음과 같은 오류가 발생했기 때문에이 작업을 극복하기위한 지원에 정말로 감사한다.

Feature ranking: 
1. feature 660 (0.405876) 
2. feature 655 (0.138400) 
3. feature 659 (0.056848) 
4. feature 658 (0.056631) 
5. feature 321 (0.014537) 
6. feature 336 (0.013202) 
7. feature 324 (0.012455) 
8. feature 322 (0.011517) 
9. feature 656 (0.011493) 
10. feature 650 (0.010850) 
Traceback (most recent call last): 
    File "random_forest.py", line 234, in <module> 
    plt.xticks(range(new_indices), new_indices) 
TypeError: only integer scalar arrays can be converted to a scalar index 
+0

당신이 슬라이스를 사용할 수 있습니다 중간 코드 부분에 변경 사항을 적용하십시오. –

답변

1

여기서 중요한 기능의 색인은 내림차순으로 정렬됩니다. 즉, 인덱스 [: 10]를 사용하면 상위 10 개 기능을 사용할 수 있습니다.

indices = np.argsort(importances)[::-1] 
top_k = 10 
new_indices = indices[:top_k] 

그래서 당신은 (당신의 원하는 값으로 top_k 변경) 따라서이 부분을 변경해야

# Print the feature ranking 
print("Feature ranking:") 

for f in range(top_k): 
    print("%d. feature %d (%f)" % (f + 1, new_indices[f], importances[new_indices[f]])) 

동일 여기 그래프 플롯 :

#Plot the feature importances of the forest 
plt.figure() 
plt.title("Feature importances") 
plt.bar(range(top_k), importances[new_indices], 
     color="r", yerr=std[new_indices], align="center") 

#Edited here (put top_k in range) 
plt.xticks(range(top_k), new_indices) 
#Edited here (put top_k) 
plt.xlim([-1, top_k]) 
plt.show() 
+0

@ neo33 문제를 해결하기 위해 코드를 편집했습니다. 그것은 내 편이 었어. 정수 대신 배열을 넣었습니다. 나는 그것을 지금 풀었다. 그리고 코드가 바뀌 었습니다. –

+0

덕분에 마침내이 상황을 극복했습니다. – neo33

관련 문제