-
Notifications
You must be signed in to change notification settings - Fork 5
/
FeatureImportance_RF.py
148 lines (123 loc) · 5.19 KB
/
FeatureImportance_RF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/home/superusuario/py-env/py3IA/lib/python3.7
# -*- coding: utf-8 -*-
"""
Produzido por Geodatin - Dados e Geoinformacao
DISTRIBUIDO COM GPLv2
@author: geodatin
"""
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#import json
#from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import RandomForestClassifier
# endereco da pasta onde estam as amostras em csv
camCarta = '/run/media/superusuario/Almacen/amostras/mycartas/'
# endereco da pasta onde seram guardadas as os graficos de importancia
camino = '/home/superusuario/Dados/ProjMapbiomas/collection4/'
filelist = os.listdir(camCarta)
#lsFeatures = []
# ===== diccionario que guardará as bandas de interesse como key
# =====e como valores a suma da importancia por carta de cada banda
dictImportancia = {}
inicio = False
file = open(camino + "listaFeatNum2.txt", "w")
for myCart in filelist[:2]:
print(myCart)
dfPtos = pd.read_csv(camCarta + myCart, dtype=None, error_bad_lines=False)
#print(dfPtos.head())
nameFeat = []
#===== lsFeatExt Bandas de la imagem presentes no csv e que não debem entrar na analises
lsFeatExt = ['system:index','ano','carta','latitude','longitude','.geo', 'class', 'extreme', 'outlier']
#==== loop para criar um diccionario com as bandas de interesse
for ii in dfPtos.columns:
if ii not in lsFeatExt:
#print(ii)
nameFeat.append(ii)
# confere que os nomes das bandas estam presentes
print("lista com nome das bandas", nameFeat)
# loop paea encher ou nao o diccionario de importancia
if inicio == False:
for ii in nameFeat:
dictImportancia[ii] = 0.0
inicio = True
## == extrair do DataFrame os dados de referencia seram usados no modelo
dadosRef = dfPtos['class'][:]
print(dadosRef[:5])
# == esta operacao e opcional mas ajuda entender o como vc tem distribuida
# == as suas amostras
#===== calcular o histograma da banda de referencia ===
# === de forma automatica extrai
print("Construcao do histograma de classes da carta")
mydict = {}
for ii in dadosRef:
if ii not in mydict.keys():
mydict[ii] = 0
else:
valor = mydict[ii]
mydict[ii] = valor + 1
print(mydict)
# Build a forest and compute the feature importances
forest = RandomForestClassifier(bootstrap=True,
n_estimators=250,
max_leaf_nodes=6,
random_state=0)
forest.fit(dfPtos[nameFeat], dadosRef)
# == calcula a importancia das features
importances = forest.feature_importances_
#print(importances)
# dictImportancia , nameFeat
for ii in range(len(importances)):
print( 'indice ' + str(ii) + ' importancia ' + str(importances[ii]))
vv = dictImportancia[nameFeat[ii]] + importances[ii]
print(vv)
dictImportancia[nameFeat[ii]] = vv
#print(dictImportancia)
# == calcula o desvio padrao das features importante para cada Arvore
std = np.std([tree.feature_importances_ for tree in forest.estimators_], axis=0)
# === ordenar o pela importancia
indices = np.argsort(importances)[::-1]
print(indices)
print(type(importances))
#=================================================================
#===== salvar a lista das Features mais importante em text========
# plt.figure(figsize=(30,10))
# plt.title("Feature importances")
# plt.bar(range(len(nameFeat)), importances[indices],
# color="r", yerr=std[indices], align="center")
# plt.xticks(range(len(nameFeat)), indices)
# plt.xlim([-1, len(nameFeat)])
# nomeSave = camino + "plotimportFeat_"+myCart[:-4]+".png"
# plt.savefig(nomeSave, dpi=70, facecolor='w', edgecolor='w',
# orientation='portrait', papertype=None, format='png',
# transparent=False, bbox_inches=None, pad_inches=0.1,
# frameon=None, metadata=None)
plt.show()
#Print of name Feature
print("aqui lista de importnacia")
for mkeys, valor in dictImportancia.items():
print(mkeys + ' : ' + str(valor))
importances = []
for ii in dictImportancia.values():
print(ii)
importances.append(ii)
#importances = np.array(dictImportancia.values())
indices = np.argsort(importances)[::-1]
myNameFeatImp = []
myFeatimportante= []
for ii in indices:
myFeatimportante.append(importances[ii])
myNameFeatImp.append(nameFeat[ii])
df = pd.DataFrame(list(zip(myFeatimportante,myNameFeatImp))).set_index(1) # indices,
df.to_csv(camino + 'tabelaImportanciasFeaturesRandomForest.csv', header= ['importancia'], sep= ';')
nomeSave = camino+"plotimportFeat_All_Cartas.png"
ax = df.plot.bar(figsize=(30,10), title= 'Importancia das Caracteristicas', color='r')
ax.legend(loc=0, labels= ['soma da importancia X Carta']) #
ax.set_xlabel("Lista de Features")
ax.set_ylabel("indice importancia")
fig = ax.get_figure()
fig.savefig(nomeSave, dpi=70, facecolor='w', edgecolor='w',
orientation='portrait', papertype=None, format='png',
transparent=False, bbox_inches=None, pad_inches=0.1,
frameon=None, metadata=None)