Skip to content

CovMatrix

Covariance matrix.

Parameters

  • ddof – defaults to 1

    Delta Degrees of Freedom.

Examples

>>> import numpy as np
>>> import pandas as pd
>>> from river import misc

>>> np.random.seed(42)
>>> X = pd.DataFrame(np.random.random((8, 3)), columns=["red", "green", "blue"])
>>> X
        red     green      blue
0  0.374540  0.950714  0.731994
1  0.598658  0.156019  0.155995
2  0.058084  0.866176  0.601115
3  0.708073  0.020584  0.969910
4  0.832443  0.212339  0.181825
5  0.183405  0.304242  0.524756
6  0.431945  0.291229  0.611853
7  0.139494  0.292145  0.366362

>>> cov = misc.CovMatrix()
>>> for x in X.to_dict(orient="records"):
...     cov = cov.update(x)
>>> cov
        blue     green    red
 blue    0.076    0.020   -0.010
green    0.020    0.113   -0.053
  red   -0.010   -0.053    0.079

There is also an update_many method to process mini-batches. The results are identical.

>>> cov = misc.CovMatrix()
>>> cov = cov.update_many(X)
>>> cov
          blue     green    red
 blue    0.076    0.020   -0.010
green    0.020    0.113   -0.053
  red   -0.010   -0.053    0.079

The covariances are stored in a dictionary, meaning any one of them can be accessed as such:

>>> cov["blue", "green"]
Cov: 0.020292

Methods

clear

D.clear() -> None. Remove all items from D.

copy
fromkeys
get

D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.

Parameters

  • key
  • default – defaults to None
items

D.items() -> a set-like object providing a view on D's items

keys

D.keys() -> a set-like object providing a view on D's keys

pop

D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised.

Parameters

  • key
  • default – defaults to <object object at 0x7fda6d520150>
popitem

D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.

setdefault

D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

Parameters

  • key
  • default – defaults to None
update

Update with a single sample.

Parameters

  • x (dict)
update_many

Update with many samples.

Parameters

  • X (pandas.core.frame.DataFrame)
values

D.values() -> an object providing a view on D's values