-
Notifications
You must be signed in to change notification settings - Fork 30
/
_classify_API.py
159 lines (136 loc) · 5.19 KB
/
_classify_API.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
148
149
150
151
152
153
154
155
156
157
158
159
from .classifiers import (
BoxPlot,
EqualInterval,
FisherJenks,
FisherJenksSampled,
HeadTailBreaks,
JenksCaspall,
JenksCaspallForced,
JenksCaspallSampled,
MaxP,
MaximumBreaks,
NaturalBreaks,
Quantiles,
Percentiles,
StdMean,
UserDefined
)
__author__ = ("Stefanie Lumnitz <stefanie.lumitz@gmail.com>")
_classifiers = {
'boxplot': BoxPlot,
'equalinterval': EqualInterval,
'fisherjenks': FisherJenks,
'fisherjenkssampled': FisherJenksSampled,
'headtailbreaks': HeadTailBreaks,
'jenkscaspall': JenksCaspall,
'jenkscaspallforced': JenksCaspallForced,
'jenkscaspallsampled': JenksCaspallSampled,
'maxp': MaxP,
'maximumbreaks': MaximumBreaks,
'naturalbreaks': NaturalBreaks,
'quantiles': Quantiles,
'percentiles': Percentiles,
'stdmean': StdMean,
'userdefined': UserDefined,
}
def classify(y, scheme, k=5, pct=[1,10,50,90,99,100],
pct_sampled=0.10, truncate=True,
hinge=1.5, multiples=[-2,-1,1,2], mindiff=0,
initial=100, bins=None):
"""
Classify your data with `mapclassify.classify`
Note: Input parameters are dependent on classifier used.
Parameters
----------
y : array
(n,1), values to classify
scheme : str
pysal.mapclassify classification scheme
k : int, optional
The number of classes. Default=5.
pct : array, optional
Percentiles used for classification with `percentiles`.
Default=[1,10,50,90,99,100]
pct_sampled : float, optional
The percentage of n that should form the sample
(JenksCaspallSampled, FisherJenksSampled)
If pct is specified such that n*pct > 1000, then pct = 1000./n
truncate : boolean, optional
truncate pct_sampled in cases where pct * n > 1000., (Default True)
hinge : float, optional
Multiplier for IQR when `BoxPlot` classifier used.
Default=1.5.
multiples : array, optional
The multiples of the standard deviation to add/subtract from
the sample mean to define the bins using `std_mean`.
Default=[-2,-1,1,2].
mindiff : float, optional
The minimum difference between class breaks
if using `maximum_breaks` classifier. Deafult =0.
initial : int
Number of initial solutions to generate or number of runs
when using `natural_breaks` or `max_p_classifier`.
Default =100.
Note: setting initial to 0 will result in the quickest
calculation of bins.
bins : array, optional
(k,1), upper bounds of classes (have to be monotically
increasing) if using `user_defined` classifier.
Default =None, Example =[20, max(y)].
Returns
-------
classifier : pysal.mapclassify.classifier instance
Object containing bin ids for each observation (.yb),
upper bounds of each class (.bins), number of classes (.k)
and number of observations falling in each class (.counts)
Note: Supported classifiers include: quantiles, box_plot, euqal_interval,
fisher_jenks, fisher_jenks_sampled, headtail_breaks, jenks_caspall,
jenks_caspall_sampled, jenks_caspall_forced, max_p, maximum_breaks,
natural_breaks, percentiles, std_mean, user_defined
Examples
--------
Imports
>>> from libpysal import examples
>>> import geopandas as gpd
>>> from mapclassify import classify
Load Example Data
>>> link_to_data = examples.get_path('columbus.shp')
>>> gdf = gpd.read_file(link_to_data)
>>> x = gdf['HOVAL'].values
Classify values by quantiles
>>> quantiles = classify(x, 'quantiles')
Classify values by box_plot and set hinge to 2
>>> box_plot = classify(x, 'box_plot', hinge=2)
"""
# reformat
scheme_lower = scheme.lower()
scheme = scheme_lower.replace('_', '')
# check if scheme is a valid scheme
if scheme not in _classifiers:
raise ValueError("Invalid scheme. Scheme must be in the"
" set: %r" % _classifiers.keys())
elif scheme == 'boxplot':
classifier = _classifiers[scheme](y, hinge)
elif scheme == 'fisherjenkssampled':
classifier = _classifiers[scheme](y, k,
pct_sampled, truncate)
elif scheme == 'headtailbreaks':
classifier = _classifiers[scheme](y)
elif scheme == 'percentiles':
classifier = _classifiers[scheme](y, pct)
elif scheme == 'stdmean':
classifier = _classifiers[scheme](y, multiples)
elif scheme == 'jenkscaspallsampled':
classifier = _classifiers[scheme](y, k,
pct_sampled)
elif scheme == 'maximumbreaks':
classifier = _classifiers[scheme](y, k, mindiff)
elif scheme in ['naturalbreaks', 'maxp']:
classifier = _classifiers[scheme](y, k, initial)
elif scheme == 'userdefined':
classifier = _classifiers[scheme](y, bins)
elif scheme in ['equalinterval', 'fisherjenks',
'jenkscaspall','jenkscaspallforced',
'quantiles']:
classifier = _classifiers[scheme](y, k)
return classifier