Skip to content

Instantly share code, notes, and snippets.

@wmayner
Last active February 26, 2020 02:44
Show Gist options
  • Select an option

  • Save wmayner/d3abd5ad15428e9eae9b963ac063a145 to your computer and use it in GitHub Desktop.

Select an option

Save wmayner/d3abd5ad15428e9eae9b963ac063a145 to your computer and use it in GitHub Desktop.

Revisions

  1. wmayner revised this gist Feb 26, 2020. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion state_pairs_to_tpm.py
    Original file line number Diff line number Diff line change
    @@ -37,13 +37,21 @@ def state_pairs_to_tpm(previous_states, next_states):
    ... [0, 0],
    ... [1, 1],
    ... ])
    >>> state_pairs_to_tpm(sensors, motors)
    >>> tpm = state_pairs_to_tpm(sensors, motors)
    >>> tpm
    next (0, 0) (0, 1) (1, 0) (1, 1)
    previous
    (0, 0) 0.0 0.0 1.0 0.0
    (0, 1) 0.0 1.0 0.0 0.0
    (1, 0) 0.5 0.5 0.0 0.0
    (1, 1) 0.5 0.0 0.0 0.5
    >>> # The underlying NumPy array can be accessed with the
    >>> # `.values` attribute:
    >>> tpm.values
    array([[0. , 0. , 1. , 0. ],
    [0. , 1. , 0. , 0. ],
    [0.5, 0.5, 0. , 0. ],
    [0.5, 0. , 0. , 0.5]])
    """
    if previous_states.ndim != 2 or next_states.ndim != 2:
    raise ValueError(
  2. wmayner created this gist Feb 26, 2020.
    58 changes: 58 additions & 0 deletions state_pairs_to_tpm.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    # -*- coding: utf-8 -*-
    # state_pairs_to_tpm.py

    import numpy as np
    import pandas as pd


    def state_pairs_to_tpm(previous_states, next_states):
    """Return a TPM from observed state transitions.
    Arguments:
    previous_states (array-like): An array where the first dimension
    indexes states, such that ``previous_states[i]`` precedes
    ``next_states[i]``.
    next_states (array-like): An array where the first dimension indexes
    states, such that ``next_states[i]`` follows
    ``previous_states[i]``.
    Returns:
    pd.DataFrame: A TPM with unique ``previous_states`` as the index and
    unique ``next_states`` as the columns.
    Example:
    >>> sensors = np.array([
    ... [0, 0],
    ... [0, 1],
    ... [1, 0],
    ... [1, 0],
    ... [1, 1],
    ... [1, 1],
    ... ])
    >>> motors = np.array([
    ... [1, 0],
    ... [0, 1],
    ... [0, 0],
    ... [0, 1],
    ... [0, 0],
    ... [1, 1],
    ... ])
    >>> state_pairs_to_tpm(sensors, motors)
    next (0, 0) (0, 1) (1, 0) (1, 1)
    previous
    (0, 0) 0.0 0.0 1.0 0.0
    (0, 1) 0.0 1.0 0.0 0.0
    (1, 0) 0.5 0.5 0.0 0.0
    (1, 1) 0.5 0.0 0.0 0.5
    """
    if previous_states.ndim != 2 or next_states.ndim != 2:
    raise ValueError(
    f"input arrays must be 2D; got {previous_states.ndim}D and "
    f"{next_states.ndim}D"
    )
    previous_states = pd.Categorical([tuple(states) for states in previous_states])
    next_states = pd.Categorical([tuple(states) for states in next_states])
    tpm = pd.crosstab(previous_states, next_states, normalize="index")
    tpm.index.name = "previous"
    tpm.columns.name = "next"
    return tpm