-
Notifications
You must be signed in to change notification settings - Fork 1
/
hhi-featureselection.py
112 lines (78 loc) · 2.6 KB
/
hhi-featureselection.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
# Feature selection trial
# Using forests
# Adapted from sklearn tutorials.
# Original code here http://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances.html#sphx-glr-auto-examples-ensemble-plot-forest-importances-py
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.ensemble import ExtraTreesClassifier
import marketUtils
import random
nem = marketUtils.getNem()
interconnectors = marketUtils.getInterconnectorFlows()
(hhi, maxShareRetailers) = marketUtils.getHHI()
X = []
y = []
# Prepare the data for the classifier.
times = list(nem)
times.sort()
xLabels = list(interconnectors.itervalues().next())
xLabels.sort()
for time in times:
# Add the classifications:
if float(hhi[time]['Cumulative_HHI']) >= 2500:
classification = 1
else:
classification = 0
y.append(classification)
row = []
for attribute in xLabels:
row.append(interconnectors[time][attribute])
# for category in sorted(list(hhi[time])):
# row.append(hhi[time][category])
row.append(nem[time]['nsw']['demand'])
row.append(nem[time]['vic']['demand'])
row.append(nem[time]['qld']['demand'])
row.append(nem[time]['sa']['demand'])
row.append(nem[time]['tas']['demand'])
row.append(random.randint(1,2))
X.append(row)
# for category in sorted(list(hhi[time])):
# xLabels.append('nsw '+category)
xLabels.append('nsw demand')
xLabels.append('vic demand')
xLabels.append('qld demand')
xLabels.append('sa demand')
xLabels.append('tas demand')
xLabels.append('DUMMY VARIABLE')
X = np.array(X)
y = np.array(y)
print X
print np.sum(y)
# Build a classification task using 3 informative features
# X, y = make_classification(n_samples=1000,
# n_features=10,
# n_informative=3,
# n_redundant=0,
# n_repeated=0,
# n_classes=2,
# random_state=0,
# shuffle=False)
# print X
# Build a forest and compute the feature importances
forest = ExtraTreesClassifier(n_estimators=250,random_state=0)
forest.fit(X, y)
importances = forest.feature_importances_
std = np.std([tree.feature_importances_ for tree in forest.estimators_], axis=0)
indices = np.argsort(importances)[::-1]
# Print the feature ranking
print("Feature ranking:")
for f in range(X.shape[1]):
print("%d. feature %d (%f) %s" % (f + 1, indices[f], importances[indices[f]], str(xLabels[indices[f]])))
# Plot the feature importances of the forest
plt.figure()
plt.title("Feature importances")
plt.bar(range(X.shape[1]), importances[indices], color="r", yerr=std[indices], align="center")
plt.xticks(range(X.shape[1]), indices)
plt.xlim([-1, X.shape[1]])
plt.show()