### Install Jenkspy from PyPI Source: https://github.com/mthh/jenkspy/blob/master/README.md Install the jenkspy library using pip for quick access to its functionalities. ```shell pip install jenkspy ``` -------------------------------- ### Install Jenkspy from Source Source: https://github.com/mthh/jenkspy/blob/master/README.md Clone the jenkspy repository and install it from source using pip. This method is useful for development or when needing the latest changes. ```shell git clone http://github.com/mthh/jenkspy cd jenkspy/ pip install . ``` -------------------------------- ### Install Jenkspy for Anaconda Users Source: https://github.com/mthh/jenkspy/blob/master/README.md Install jenkspy using conda from the conda-forge channel, suitable for users within the Anaconda ecosystem. ```shell conda install -c conda-forge jenkspy ``` -------------------------------- ### JenksNaturalBreaks Class Source: https://github.com/mthh/jenkspy/blob/master/README.md The `JenksNaturalBreaks` class provides a scikit-learn-like interface for computing natural breaks. It includes methods for fitting data, predicting group labels, and grouping elements. ```APIDOC ## JenksNaturalBreaks Class ### Description Provides a scikit-learn-inspired interface for computing natural breaks. The `.fit()` and `.group()` methods handle values outside the min/max range by assigning them to the first or last group, respectively. ### Method `JenksNaturalBreaks(n_classes=None)` ### Parameters #### Path Parameters None #### Query Parameters - **n_classes** (int) - Optional - The desired number of classes. If None, it defaults to a value determined by the data. ### Methods - **fit(X)**: Fits the model to the input data `X`. - **predict(X)**: Predicts the group label for input data `X`. - **group(X)**: Groups the elements of `X` into their respective classes. ### Request Example (Fit, Predict, Group) ```python from jenkspy import JenksNaturalBreaks x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] jnb = JenksNaturalBreaks(4) # Asking for 4 clusters jnb.fit(x) print(jnb.labels_) print(jnb.groups_) print(jnb.breaks_) print(jnb.inner_breaks_) print(jnb.predict(15)) print(jnb.predict([2.5, 3.5, 6.5])) print(jnb.group([2.5, 3.5, 6.5])) ``` ### Response (Attributes after fit) #### Success Response (200) - **labels_** (numpy.ndarray) - Labels for the fitted data. - **groups_** (list of numpy.ndarray) - Content of each group. - **breaks_** (list) - Break values, including minimum and maximum. - **inner_breaks_** (list) - Inner breaks (i.e., `breaks_[1:-1]`). #### Response Example (Attributes) ```json { "labels_": [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3], "groups_": [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11] ], "breaks_": [0.0, 2.0, 5.0, 8.0, 11.0], "inner_breaks_": [2.0, 5.0, 8.0] } ``` #### Response Example (Predict) ```json { "predict(15)": 3, "predict([2.5, 3.5, 6.5])": [1, 1, 2] } ``` #### Response Example (Group) ```json { "group([2.5, 3.5, 6.5])": [ [], [2.5, 3.5], [6.5], [] ] } ``` ``` -------------------------------- ### Jenks Breaks with Integer-like Float for n_classes Source: https://context7.com/mthh/jenkspy/llms.txt Shows that using a float value for 'n_classes' that is equivalent to an integer (e.g., 3.0) is accepted and works correctly. ```python import jenkspy breaks = jenkspy.jenks_breaks([1, 2, 3, 4, 5], 3.0) # Works fine print(breaks) ``` -------------------------------- ### Calculate Goodness of Variance Fit (GVF) Source: https://context7.com/mthh/jenkspy/llms.txt The `goodness_of_variance_fit` method quantifies how well the natural breaks classification explains the variance in the data. A GVF score closer to 1.0 indicates a better fit. ```python from jenkspy import JenksNaturalBreaks # Sample data with clear clusters data = [1, 2, 3, 10, 11, 12, 50, 51, 52] # Initialize and fit the classifier jnb = JenksNaturalBreaks(n_classes=3) jnb.fit(data) # Calculate GVF gvf = jnb.goodness_of_variance_fit(data) print(f"GVF: {gvf}") # Output: GVF: 0.953... ``` -------------------------------- ### Find Optimal Classification with JenksNaturalBreaks Source: https://context7.com/mthh/jenkspy/llms.txt Iterate through different numbers of classes to find the optimal classification by evaluating the goodness of variance fit (GVF). This method is useful for determining the best number of breaks for a given dataset. ```python for n_classes in range(2, 6): jnb = JenksNaturalBreaks(n_classes=n_classes) jnb.fit(data) gvf = jnb.goodness_of_variance_fit(data) print(f"n_classes={n_classes}: GVF={gvf:.4f}, breaks={jnb.breaks_}") ``` -------------------------------- ### Handle TypeError for Jenks Breaks (invalid input sequence) Source: https://context7.com/mthh/jenkspy/llms.txt Demonstrates catching a TypeError when the input data is not a sequence of numbers. This ensures the input is in the correct format. ```python import jenkspy try: jenkspy.jenks_breaks("not a number sequence", 2) except TypeError as e: print(f"TypeError: {e}") ``` -------------------------------- ### Handle ValueError for Jenks Breaks (non-finite values) Source: https://context7.com/mthh/jenkspy/llms.txt Demonstrates catching a ValueError when the input data contains non-finite values (NaN or Inf). This ensures data integrity for classification. ```python import jenkspy try: jenkspy.jenks_breaks([1, 2, float('inf'), 4], 2) except ValueError as e: print(f"ValueError: {e}") ``` -------------------------------- ### goodness_of_variance_fit Method Source: https://context7.com/mthh/jenkspy/llms.txt Calculates the Goodness of Variance Fit (GVF) score to measure how well the classification captures the variance in the data. ```APIDOC ## goodness_of_variance_fit Method ### Description The `goodness_of_variance_fit` method calculates the Goodness of Variance Fit (GVF) score, which measures how well the classification captures the variance in the data. A GVF closer to 1.0 indicates a better fit where within-class variance is minimized relative to total variance. ### Method `goodness_of_variance_fit(data, n_classes)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from jenkspy import JenksNaturalBreaks # Sample data with clear clusters data = [1, 2, 3, 10, 11, 12, 50, 51, 52] # Calculate GVF for different numbers of classes gvf_3_classes = JenksNaturalBreaks(n_classes=3).goodness_of_variance_fit(data) gvf_2_classes = JenksNaturalBreaks(n_classes=2).goodness_of_variance_fit(data) print(f"GVF with 3 classes: {gvf_3_classes}") print(f"GVF with 2 classes: {gvf_2_classes}") ``` ### Response #### Success Response (200) - **gvf_score** (float) - The Goodness of Variance Fit score. ``` -------------------------------- ### JenksNaturalBreaks Class for Object-Oriented Workflows Source: https://context7.com/mthh/jenkspy/llms.txt Utilize `JenksNaturalBreaks` for a scikit-learn-like experience. Fit the classifier to data, predict class labels, group data, and calculate GVF. Values outside the original range are assigned to the first or last class. ```python from jenkspy import JenksNaturalBreaks import numpy as np # Initialize classifier with desired number of classes jnb = JenksNaturalBreaks(n_classes=4) # Sample data data = [132, 915, 312, 1424, 1240, 1370, 113, 1028, 1078, 416, 963, 359, 1422, 658, 73, 1326, 245, 848, 683, 255] # Fit the classifier to the data jnb.fit(data) # Access computed breaks (including min and max) print(f"All breaks: {jnb.breaks_}") # Output: [73.0, 416.0, 848.0, 1240.0, 1424.0] # Access inner breaks (excluding min and max) print(f"Inner breaks: {jnb.inner_breaks_}") # Output: [416.0, 848.0, 1240.0] # Get labels for each data point (0-indexed class assignments) print(f"Labels: {jnb.labels_}") # Output: [0 2 0 3 3 3 0 2 2 0 2 0 3 1 0 3 0 2 1 0] # Get data grouped by class for i, group in enumerate(jnb.groups_): print(f"Group {i}: {group}") # Output: # Group 0: [132 312 113 416 359 73 245 255] # Group 1: [658 683] # Group 2: [915 1028 1078 963 848] # Group 3: [1424 1240 1370 1422 1326] # Predict class for a single new value label = jnb.predict(500) print(f"Predicted class for 500: {label}") # Output: 1 # Predict classes for multiple values labels = jnb.predict([100, 500, 900, 1300]) print(f"Predicted classes: {labels}") # Output: [0 1 2 3] # Group new values into their respective classes groups = jnb.group([100, 500, 900, 1300]) for i, group in enumerate(groups): print(f"Group {i}: {group}") # Output: # Group 0: [100] # Group 1: [500] # Group 2: [900] # Group 3: [1300] # Values outside original range are handled gracefully print(jnb.predict(0)) # Output: 0 (below min, assigned to first class) print(jnb.predict(2000)) # Output: 3 (above max, assigned to last class) ``` -------------------------------- ### Handle TypeError for Jenks Breaks (non-integer float for n_classes) Source: https://context7.com/mthh/jenkspy/llms.txt Demonstrates catching a TypeError when a non-integer float is used for 'n_classes'. The number of classes must be a whole number. ```python import jenkspy try: jenkspy.jenks_breaks([1, 2, 3, 4, 5], 3.5) except TypeError as e: print(f"TypeError: {e}") ``` -------------------------------- ### Handle ValueError for Jenks Breaks (non-1D numpy array) Source: https://context7.com/mthh/jenkspy/llms.txt Demonstrates catching a ValueError when a non-1D NumPy array is provided as input. Jenkspy expects a one-dimensional array for classification. ```python import jenkspy import numpy as np try: jenkspy.jenks_breaks(np.array([[1, 2], [3, 4]]), 2) except ValueError as e: print(f"ValueError: {e}") ``` -------------------------------- ### Handle ValueError for Jenks Breaks (n_classes too high) Source: https://context7.com/mthh/jenkspy/llms.txt Demonstrates catching a ValueError when 'n_classes' exceeds the number of unique values in the data. This prevents invalid classification attempts. ```python import jenkspy try: jenkspy.jenks_breaks([1, 2, 3, 4], 10) except ValueError as e: print(f"ValueError: {e}") ``` -------------------------------- ### Handle TypeError for Jenks Breaks Source: https://context7.com/mthh/jenkspy/llms.txt Demonstrates catching a TypeError when the 'n_classes' argument is not an integer. This ensures the function receives valid input types. ```python import jenkspy try: jenkspy.jenks_breaks([1, 2, 3, 4], "a") except TypeError as e: print(f"TypeError: {e}") ``` -------------------------------- ### JenksNaturalBreaks Class Usage Source: https://github.com/mthh/jenkspy/blob/master/README.md Utilize the `JenksNaturalBreaks` class for fitting data, accessing labels, groups, breaks, and inner breaks. This class handles values outside the min/max range by assigning them to the first or last group. ```python >>> from jenkspy import JenksNaturalBreaks >>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] >>> jnb = JenksNaturalBreaks(4) # Asking for 4 clusters >>> jnb.fit(x) # Create the clusters according to values in 'x' >>> print(jnb.labels_) # Labels for fitted data ... print(jnb.groups_) # Content of each group ... print(jnb.breaks_) # Break values (including min and max) ... print(jnb.inner_breaks_) # Inner breaks (ie breaks_[1:-1]) [0 0 0 1 1 1 2 2 2 3 3 3] [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([ 9, 10, 11])] [0.0, 2.0, 5.0, 8.0, 11.0] [2.0, 5.0, 8.0] ``` ```python >>> print(jnb.predict(15)) # Predict the group of a value 3 ``` ```python >>> print(jnb.predict([2.5, 3.5, 6.5])) # Predict the group of several values [1 1 2] ``` ```python >>> print(jnb.group([2.5, 3.5, 6.5])) # Group the elements into there groups [array([], dtype=float64), array([2.5, 3.5]), array([6.5]), array([], dtype=float64)] ``` -------------------------------- ### Compute Natural Breaks with jenks_breaks Function Source: https://context7.com/mthh/jenkspy/llms.txt Use `jenks_breaks` for quick classification of numerical sequences. It accepts lists, tuples, array.array, or NumPy ndarrays and returns `n_classes + 1` break values, including the minimum and maximum. ```python import jenkspy import numpy as np # Basic usage with a list of floats data = [1.3, 7.1, 7.3, 2.3, 3.9, 4.1, 7.8, 1.2, 4.3, 7.3, 5.0, 4.3] breaks = jenkspy.jenks_breaks(data, n_classes=3) print(breaks) # Output: [1.2, 2.3, 5.0, 7.8] # Classes: [1.2-2.3], (2.3-5.0], (5.0-7.8] ``` ```python # Using with integers integer_data = [132, 915, 312, 1424, 1240, 1370, 113, 1028, 1078, 416] breaks = jenkspy.jenks_breaks(integer_data, n_classes=4) print(breaks) # Output: [113.0, 416.0, 915.0, 1240.0, 1424.0] ``` ```python # Using with numpy arrays np_data = np.array([0.5, 2.1, 3.4, 4.8, 6.2, 7.5, 8.9, 9.3]) breaks = jenkspy.jenks_breaks(np_data, n_classes=3) print(breaks) # Output: [0.5, 3.4, 7.5, 9.3] ``` ```python # Using with Python array.array from array import array arr_data = array('d', [5.0, 6.7, 1.0, 9.4, 4.5, 6.4, 8.9, 5.2]) breaks = jenkspy.jenks_breaks(arr_data, n_classes=5) print(breaks) # Output: [1.0, 4.5, 5.2, 6.7, 8.9, 9.4] ``` ```python # Handling negative numbers mixed_data = [0, 1, -2] breaks = jenkspy.jenks_breaks(mixed_data, n_classes=2) print(breaks) # Output: [-2, -2, 1] ``` -------------------------------- ### Compute Jenks Breaks from JSON Data Source: https://github.com/mthh/jenkspy/blob/master/README.md Use `jenks_breaks` to compute class limits from data loaded from a JSON file. Specify the desired number of classes. ```python >>> import jenkspy >>> import json >>> with open('tests/test.json', 'r') as f: ... # Read some data from a JSON file ... data = json.loads(f.read()) ... >>> jenkspy.jenks_breaks(data, n_classes=5) # Asking for 5 classes [0.0028109620325267315, 2.0935479691252112, 4.205495140049607, 6.178148351609707, 8.09175917180255, 9.997982932254672] # ^ ^ ^ ^ ^ ^ # Lower bound Upper bound Upper bound Upper bound Upper bound Upper bound # 1st class 1st class 2nd class 3rd class 4th class 5th class # (Minimum value) (Maximum value) ``` -------------------------------- ### jenks_breaks Function Source: https://github.com/mthh/jenkspy/blob/master/README.md The `jenks_breaks` function computes natural breaks on a list, tuple, array, or numpy.ndarray of integers or floats. It returns a list of values representing the class limits. ```APIDOC ## jenks_breaks Function ### Description Computes natural breaks (Fisher-Jenks algorithm) on a list, tuple, array, or numpy.ndarray of integers or floats. Returns a list of values that correspond to the limits of the classes. ### Method `jenks_breaks(data, n_classes=None)` ### Parameters #### Path Parameters None #### Query Parameters - **data** (list, tuple, array, numpy.ndarray) - Required - The input data to compute breaks on. - **n_classes** (int) - Optional - The desired number of classes. If None, it defaults to a value determined by the data. ### Request Example ```python import jenkspy import json with open('tests/test.json', 'r') as f: data = json.loads(f.read()) breaks = jenkspy.jenks_breaks(data, n_classes=5) print(breaks) ``` ### Response #### Success Response (200) - **breaks** (list) - A list of float values representing the class boundaries, starting with the minimum value and ending with the maximum value of the input data. #### Response Example ```json { "breaks": [0.0028109620325267315, 2.0935479691252112, 4.205495140049607, 6.178148351609707, 8.09175917180255, 9.997982932254672] } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.