pyEntropy (pyentrp)

repository·master·Indexed 18 days ago

https://github.com/nikdon/pyentropy

A lightweight Python library built on NumPy for computing entropy measures for time series analysis. Version 2.1.0 provides functions for Shannon, sample, multiscale, composite multiscale, permutation, multiscale permutation, and weighted permutation entropy.

Tokens
1.9K
Snippets
11
Records
11
Agent score
13%

What's inside pyentrp

  1. Compute entropy for time series with pyEntropy

    master

    Import the entropy module from pyentrp to access various entropy computation functions. The library is built on NumPy and is designed for time series analysis.

    Supported entropy types include:

    • shannon_entropy
    • sample_entropy
    • multiscale_entropy
    • composite_multiscale_entropy
    • permutation_entropy
    • multiscale_permutation_entropy
    • weighted_permutation_entropy
    from pyentrp import entropy as ent
    import numpy as np
    
    ts = [1, 4, 5, 1, 7, 3, 1, 2, 5, 8, 9, 7, 3, 7, 9, 5, 4, 3]
    std_ts = np.std(ts)
    # Example: computing sample entropy with an embedding dimension of 4 and a tolerance of 0.2 * std
    sample_entropy = ent.sample_entropy(ts, 4, 0.2 * std_ts)
  2. Calculate Multiscale Permutation Entropy (MSPE)

    master

    Computes Multiscale Permutation Entropy by applying permutation entropy to coarse-grained versions of the time series across a specified number of scales.

    from pyentrp.entropy import multiscale_permutation_entropy
    import numpy as np
    
    time_series = np.random.rand(100)
    # m: order, delay: time delay, scale: number of scales
    mspe = multiscale_permutation_entropy(time_series, m=3, delay=1, scale=10)
  3. Calculate Permutation Entropy

    master

    Computes Permutation Entropy based on ordinal patterns.

    • order: The embedding dimension (order of permutation).
    • delay: The time delay between embedded points.
    • normalize: If True, the result is scaled between 0 and 1 by dividing by log2(factorial(order)).
    from pyentrp.entropy import permutation_entropy
    
    x = [4, 7, 9, 10, 6, 11, 3]
    
    # Standard permutation entropy (in bits)
    pe = permutation_entropy(x, order=2)
    
    # Normalized permutation entropy (between 0 and 1)
    pe_norm = permutation_entropy(x, order=3, normalize=True)
  4. Calculate Weighted Permutation Entropy (WPE)

    master

    Computes Weighted Permutation Entropy, which captures information from both the ordinal patterns (motifs) and the amplitude of the signal. This is an extension of standard permutation entropy.

    from pyentrp.entropy import weighted_permutation_entropy
    
    x = [4, 7, 9, 10, 6, 11, 3]
    
    # Standard WPE
    wpe = weighted_permutation_entropy(x, order=2)
    
    # Normalized WPE
    wpe_norm = weighted_permutation_entropy(x, order=3, normalize=True)
  5. Calculate Multiscale Entropy (MSE)

    master

    Computes Multiscale Entropy by analyzing the time series at different scales. It uses util_granulate_time_series to create coarse-grained versions of the input and then calculates the sample entropy for each scale.

    from pyentrp.entropy import multiscale_entropy
    import numpy as np
    
    time_series = np.random.rand(100)
    # Returns an array of Multiscale Entropies for each scale up to maxscale
    mse = multiscale_entropy(time_series, sample_length=3, maxscale=10)
  6. Calculate Composite Multiscale Entropy (CMSE)

    master

    Computes Composite Multiscale Entropy. Unlike standard MSE which uses a single coarse-graining method, CMSE uses multiple random coarse-graining procedures to reduce the variability introduced by the coarse-graining process itself.

    from pyentrp.entropy import composite_multiscale_entropy
    import numpy as np
    
    time_series = np.random.rand(100)
    # sample_length: length of sequential points, scale: number of scales
    cmse = composite_multiscale_entropy(time_series, sample_length=3, scale=10)
  7. Calculate Sample Entropy

    master

    Calculates the sample entropy (SE) of a 1-D time series using the Chebyshev norm. It returns an array where SE[k] is the ratio of matching templates of length k+1 to templates of length k.

    from pyentrp.entropy import sample_entropy
    import numpy as np
    
    time_series = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])
    # sample_length is the length of the longest template vector
    sampen = sample_entropy(time_series, sample_length=3, tolerance=0.1)
  8. Time-Delayed Embedding

    master

    Transforms a 1-D time series into a multi-dimensional embedded series using a specified embedding dimension and delay.

    from pyentrp.entropy import time_delay_embedding
    import numpy as np
    
    time_series = np.array([1, 2, 3, 4, 5, 6, 7, 8])
    # embedding_dimension: order, delay: delay between points
    embedded = time_delay_embedding(time_series, embedding_dimension=3, delay=1)
  9. Coarse-grain a Time Series

    master

    Extracts a coarse-grained time series by averaging segments of the original series based on a scale factor.

    from pyentrp.entropy import util_granulate_time_series
    import numpy as np
    
    time_series = np.array([1, 2, 3, 4, 5, 6, 7, 8])
    # scale: factor to group points
    cts = util_granulate_time_series(time_series, scale=2)
    # Result: [mean(1,2), mean(3,4), mean(5,6), mean(7,8)]
  10. Calculate Shannon Entropy

    master

    Computes the Shannon Entropy of a dataset. It supports both numerical time series (as np.ndarray) and strings (as list[str]). For strings, it calculates entropy based on character frequency. For numerical arrays, it uses unique value counts.

    from pyentrp.entropy import shannon_entropy
    import numpy as np
    
    # For numerical data
    data = np.array([1, 2, 1, 2, 3, 4, 1])
    entropy = shannon_entropy(data)
    
    # For string data
    text = "hello world"
    entropy_text = shannon_entropy(text)