munichpavel / fake-data-for-learning

Sample interesting fake data for machine and human learning
https://munichpavel.github.io/fake-data-for-learning
MIT License
7 stars 0 forks source link

Refactor (conditional) probability methods with xarray #16

Open munichpavel opened 4 years ago

munichpavel commented 4 years ago

The mental bookkeeping required to access (conditional) probability tables might be made easier by using xarray, e.g.

cpt_data_array = xr.DataArray(
        np.array([
            [0.2, 0.8],
            [0.6, 0.4]
        ]),
        dims=('X', 'U'),
        coords={'X': range(2), 'U': range(2)}
    )
cpt_data_array.sel(X=0).values
# array([0.2, 0.8])
munichpavel commented 4 years ago

or my new favorite

cpt_array = xr.DataArray(
    np.array([
            [
                [0.3, 0.7],
                [0.7, 0.3]
            ],
           [
                [0.1, 0.9],
                [0.2, 0.8]
            ]
        ]),
    dims=('U', 'X', 'Y'),
    coords={'U': range(2), 'X': range(2), 'Y': range(2)}
)
cpt_array.sel(U=0, X=1).values
# array([0.7, 0.3])