Comments (1)
Hi @martinfleis, thank you for reporting the bug and suggesting the solution.
I think having multiple values in the last bin is inevitable if these values are identical - it is not possible to allocate several identical values into different bins. The example you gave is a nice illustration - there are essentially two maximum values in the input data:
data = np.random.pareto(2, 1000)
data = np.append(data, data.max()) #now we have two maximum values in the input data
Your solution looks good to me! I think we can safely get rid of if len(values) > 1:
and just replace it with if len(set(values)) > 1:
. So it will be like:
def head_tail_breaks(values, cuts):
"""
head tail breaks helper function
"""
values = np.array(values)
mean = np.mean(values)
cuts.append(mean)
if len(set(values)) > 1:
return head_tail_breaks(values[values >= mean], cuts)
return cuts
After making the changes, I rerun the example you provided:
np.random.seed(0)
data = np.random.pareto(2, 1000)
data = np.append(data, data.max())
mc.HeadTailBreaks(data)
The output would have two values in the last bin.
If I twist the appended maximum value slightly - add a small value (0.00001) to it to break the tie:
np.random.seed(0)
data = np.random.pareto(2, 1000)
data = np.append(data, data.max()+0.00001)
mc.HeadTailBreaks(data)
The only change to the output classification is an additional bin.
So if you'd like to open an PR, I would happy to review and merge it!
from mapclassify.
Related Issues (20)
- silence warnings option when `k` classes not possible? HOT 2
- `v2.5.0` release HOT 11
- update branch of gh-action-pypi-publish
- GHA for updating `versioneer`
- Legend shows -inf when using lowest in UserDefined HOT 4
- Fix linting failures
- test against the nightly wheel – `numpy`
- modernize infrastructure (2023/09) HOT 1
- testing with `geopandas.dataset` module – deprecation
- minimal requirements CI env
- Tests give false positives, as _assertions requires a1.all() == a2.all() instead of np.all(a1==a2)
- 311-dev CI failures [2024-03-16] HOT 1
- 311-dev CI failures [2024-04-01]
- Consider handling NANs HOT 3
- [CI] failing dev from `libpysal.graph` --> `numpy.float_`
- doctest failures [2024-06-23] HOT 1
- accidental commit to main [2024-07-11] -- Apple silicon CI
- integrate `legendgram` into `mapclassify`? HOT 1
- time top drop Python 3.9 support? HOT 2
- remove mention of `geoplot` from `README`? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapclassify.