Git Product home page Git Product logo

Comments (1)

weikang9009 avatar weikang9009 commented on June 15, 2024

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.
image

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.
image

So if you'd like to open an PR, I would happy to review and merge it!

from mapclassify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.