Last active
February 26, 2020 02:44
-
-
Save wmayner/d3abd5ad15428e9eae9b963ac063a145 to your computer and use it in GitHub Desktop.
Revisions
-
wmayner revised this gist
Feb 26, 2020 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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], ... ]) >>> 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( -
wmayner created this gist
Feb 26, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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