@fnet/relative-levels
v0.1.13
Published
The `@fnet/relative-levels` project is designed to analyze a given set of numbers and categorize them into specified levels based on their distribution. It helps users understand the relative positioning of numbers by dividing them into ranges and providi
Downloads
212
Readme
@fnet/relative-levels
The @fnet/relative-levels
project is designed to analyze a given set of numbers and categorize them into specified levels based on their distribution. It helps users understand the relative positioning of numbers by dividing them into ranges and providing counts for each of these ranges. This can be particularly useful for data analysis, allowing users to identify patterns or trends within a dataset.
How It Works
The project operates by taking an array of numbers and dividing them into multiple levels based on average values. Specifically, it calculates average values for each level, dividing the numbers from the middle outward to create upper and lower levels. It then applies linear interpolation to predict average values between the calculated points. Lastly, it categorizes the input numbers into ranges determined by these predicted averages.
Key Features
- Dynamic Level Creation: Capable of segmenting data into a customizable number of levels, with the requirement that the number must be odd and greater than two.
- Average Calculation: Computes average values for each level and uses these to predict intermediate averages.
- Range and Count Analysis: Provides ranges based on predicted averages and counts the number of elements falling within each range.
- Level Identification: Offers a feature to find out which range a particular number falls into.
Conclusion
The @fnet/relative-levels
project provides a straightforward way of categorizing numeric data into levels, offering insights into the distribution and range of the dataset. Its ability to predict and use linear interpolation for this purpose makes it a useful tool for those seeking to understand and segment data effectively without any unnecessary complexity.
@fnet/relative-levels Developer Guide
Overview
The @fnet/relative-levels library provides a way to categorize a list of numbers into distinct levels using averages and a quadratic prediction model. Developers can use this library to segment numerical data into ranges, analyze spread and concentration, and query level assignments for individual numbers. The core functionality is accessible through a simple interface.
Installation
To install the @fnet/relative-levels library, you can use either npm or yarn:
Using npm:
npm install @fnet/relative-levels
Using yarn:
yarn add @fnet/relative-levels
Usage
Here's a step-by-step guide on how to use the core function provided by the library:
Import the Library: First, import the library into your project.
import relativeLevels from '@fnet/relative-levels';
Prepare Your Data: Ensure you have an array of numbers you wish to analyze. For example:
const numbers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
Invoke the Function: Use the
relativeLevels
function to compute the levels. You can specify the number of levels, which must be an odd number greater than 2.const { ranges, get } = relativeLevels({ numbers, levels: 5 });
Utilize the Results: You can then use the results to examine the ranges and counts of each level or determine the level of a specific number.
console.log(ranges); // Output: An array of objects indicating the range and count of numbers within each level. const numberLevel = get(10); console.log(numberLevel); // Output: The level index of the number 10.
Examples
Below are code examples demonstrating typical use cases of the library:
Example 1: Basic Level Segmentation
import relativeLevels from '@fnet/relative-levels';
const numbers = [1, 7, 3, 5, 9];
const { ranges } = relativeLevels({ numbers, levels: 3 });
console.log(ranges);
// Example output: [
// { range: [Number.NEGATIVE_INFINITY, 4], count: 2 },
// { range: [4, 8], count: 2 },
// { range: [8, Number.POSITIVE_INFINITY], count: 1 }
// ]
Example 2: Finding Level of a Number
import relativeLevels from '@fnet/relative-levels';
const numbers = [12, 15, 14, 19, 23];
const { get } = relativeLevels({ numbers, levels: 3 });
const levelIndex = get(15);
console.log(levelIndex);
// Example output: 1 (Level index for the number 15)
Acknowledgement
This guide was created to help developers integrate and utilize the @fnet/relative-levels library effectively in their projects. We appreciate any feedback for continuous improvement.
Input Schema
$schema: https://json-schema.org/draft/2020-12/schema
title: Index Function Schema
type: object
properties:
numbers:
type: array
items:
type: number
description: The array of numbers to process.
levels:
type: integer
default: 5
description: The number of levels, must be odd and greater than 2.
minimum: 3
pattern: ^(?![02468]$).*$
returns:
type: array
items:
type: object
properties:
range:
type: array
items:
anyOf:
- type: number
- enum:
- Number.NEGATIVE_INFINITY
- Number.POSITIVE_INFINITY
minItems: 2
maxItems: 2
description: Defines the range for the level.
count:
type: integer
description: The count of numbers within this range.