-
Notifications
You must be signed in to change notification settings - Fork 30
/
pooling.py
112 lines (96 loc) · 3.09 KB
/
pooling.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
import numpy as np
from .classifiers import (
BoxPlot,
EqualInterval,
FisherJenks,
FisherJenksSampled,
Quantiles,
UserDefined,
NaturalBreaks,
MaximumBreaks,
MaxP,
StdMean,
)
__all__ = ["Pooled"]
dispatcher = {
"boxplot": BoxPlot,
"equalinterval": EqualInterval,
"fisherjenks": FisherJenks,
"fisherjenkssampled": FisherJenksSampled,
"quantiles": Quantiles,
"maximumbreaks": MaximumBreaks,
"stdmean": StdMean,
"userdefined": UserDefined,
}
class Pooled(object):
"""Applying global binning across columns
Parameters
----------
Y : array
(n, m), values to classify, with m>1
classifier : string
Name of mapclassify.classifier to apply
**kwargs : dict
additional keyword arguments for classifier
Attributes
----------
global_classifier : MapClassifier
Instance of the pooled classifier defined as the classifier
applied to the union of the columns.
col_classifier : list
Elements are MapClassifier instances with the pooled classifier
applied to the associated column of Y.
Examples
--------
>>> import numpy as np
>>> import mapclassify as mc
>>> n = 20
>>> data = np.array([np.arange(n)+i*n for i in range(1,4)]).T
>>> res = mc.Pooled(data)
>>> res.col_classifiers[0].counts
array([12, 8, 0, 0, 0])
>>> res.col_classifiers[1].counts
array([ 0, 4, 12, 4, 0])
>>> res.col_classifiers[2].counts
array([ 0, 0, 0, 8, 12])
>>> res.global_classifier.counts
array([12, 12, 12, 12, 12])
>>> res.global_classifier.bins == res.col_classifiers[0].bins
array([ True, True, True, True, True])
>>> res.global_classifier.bins
array([31.8, 43.6, 55.4, 67.2, 79. ])
"""
def __init__(self, Y, classifier="Quantiles", **kwargs):
self.__dict__.update(kwargs)
Y = np.asarray(Y)
n, cols = Y.shape
y = np.reshape(Y, (-1, 1), order="f")
method = classifier.lower()
if method not in dispatcher:
print(f"{method} not a valid classifier.")
return None
global_classifier = dispatcher[method](y, **kwargs)
# self.k = global_classifier.k
col_classifiers = []
name = f"Pooled {classifier}"
for c in range(cols):
res = UserDefined(Y[:, c], bins=global_classifier.bins)
res.name = name
col_classifiers.append(res)
self.col_classifiers = col_classifiers
self.global_classifier = global_classifier
self._summary()
def _summary(self):
yb = self.global_classifier.yb
self.classes = self.global_classifier.classes
self.tss = self.global_classifier.tss
self.adcm = self.global_classifier.adcm
self.gadf = self.global_classifier.gadf
def __str__(self):
s = "Pooled Classifier"
rows = [s]
for c in self.col_classifiers:
rows.append(c.table())
return "\n\n".join(rows)
def __repr__(self):
return self.__str__()