GithubHelp home page GithubHelp logo

Comments (1)

arvkevi avatar arvkevi commented on July 19, 2024

Hello @shineisagoodgirl, thanks for your question. There is a unit test that checks the knee points of a sine wave. The trick is that direction and curve need to be modified to detect different types of knees in one line. Kneed can't guess which curve is the one the user is trying to identify.

You could use the code from the unit test to help get you started:

x = np.arange(0, 10, 0.1)
y_sin = np.sin(x)

sine_combos = [
    ('decreasing', 'convex'),
    ('increasing', 'convex'),
    ('increasing', 'concave'),
    ('decreasing', 'concave'),
]
expected_knees = [4.5, 4.9, 7.7, 1.8]
detected_knees = []
for direction, curve in sine_combos:
    kl_sine = KneeLocator(
        x, y_sin, direction=direction, curve=curve, S=1, online=True
    )
    detected_knees.append(kl_sine.knee)
assert np.isclose(expected_knees, detected_knees).all()

You could plot this:

plt.style.use("fivethirtyeight")
plt.figure(figsize=(8, 8))
plt.plot(x, y_sin)
plt.vlines(
    detected_knees[0],
    min(y_sin),
    max(y_sin),
    linestyles="--",
    color="#fc4f30",
    label="convex decreasing",
)
plt.vlines(
    detected_knees[1],
    min(y_sin),
    max(y_sin),
    linestyles="--",
    color="#e5ae38",
    label="convex increasing",
)
plt.vlines(
    detected_knees[2],
    min(y_sin),
    max(y_sin),
    linestyles="--",
    color="#6d904f",
    label="concave increasing",
)
plt.vlines(
    detected_knees[3],
    min(y_sin),
    max(y_sin),
    linestyles="--",
    color="#8b8b8b",
    label="concave decreasing",
)
plt.legend()

sine_knee

Notice that the concave increasing knee could be at two locations in the figure. If there is more than one potential knee point for a line, kneed can show you all the candidate knee points by accessing the attribute .all_knees.

kl_sine = KneeLocator(
    x, y_sin, direction='increasing', curve='concave', S=1, online=True
)

print(kl_sine.all_knees)
{1.4000000000000001, 7.7}

from kneed.

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.