baypy

dot_product(data: pd.DataFrame, regressors: dict[str, float | int]) np.ndarray

Computes the dot product between columns of data and values of regressors.

Parameters

datapandas.DataFrame

The dataframe to be used for the dot product. It cannot be empty. It must contain all regressors keys.

regressorsdict

Dictionary with regressors’ values. It cannot be empty. All regressors and relative values must be a key-value pair.

Returns

numpy.ndarray

Array of computed dot product. Each element is the dot product of a data row with respect to each regressors. It has the same length of data.

Raises

TypeError
  • If data is not a pandas.DataFrame,

  • if regressors is not a dict,

  • if a regressors value is not a int or a float.

KeyError

If a regressors key is not a column of data.

ValueError
  • If data is an empty pandas.DataFrame,

  • if regressors is an empty dict.

Examples

>>> import pandas as pd
>>> data = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
>>> regressors = {'a': 2, 'b': -1}
>>> dot_product(data = data, regressors = regressors)
>>> array([-2, -1,  0])
flatten_matrix(matrix: ndarray) ndarray

Flattens a matrix of dimensions (M, N) to (M*N, ).

Parameters

matrixnumpy.ndarray

Matrix with M rows and N columns.

Returns

numpy.ndarray

Flatten array with M*N elements.

Raises

TypeError

If matrix is not a numpy.ndarray.

ValueError

If matrix is an empty numpy.ndarray.

Examples

>>> import numpy
>>> a = numpy.array([[1, 2], [3, 4], [5, 6]])
>>> a
>>> array([[1, 2],
...        [3, 4],
...        [5, 6]])
>>> flatten_matrix(a)
>>> array([1, 2, 3, 4, 5, 6])
matrices_to_frame(matrices_dict: dict[str, ndarray]) DataFrame

Organizes a dictionary of matrices in a pandas.DataFrame. Each matrix becomes a frame column, with column name equal to the matrix’ relative key in the dictionary. If the matrix has dimensions (M, N), then the relative frame column has length M*N.

Parameters

matrices_dictdict

Dictionary of matrices to be organized. Matrices and relative names are key-value pairs. Each matrix is a numpy.ndarray with dimensions (M, N).

Returns

pandas.DataFrame

Reorganized matrices frame. Matrices are organized in a pandas.DataFrame, one for each column. The length of the frame is M*N.

Raises

TypeError
  • If matrices_dict is not a dict,

  • if a matrices_dict value is not a numpy.ndarray.

ValueError

If a matrices_dict value is an empty numpy.ndarray.

Examples

>>> import numpy
>>> a = numpy.array([[1, 2], [3, 4], [5, 6]])
>>> b = numpy.array([[7, 8], [9, 10], [11, 12]])
>>> d = {'a': a, 'b': b}
>>> matrices_to_frame(d)
>>>    a   b
>>> 0  1   7
>>> 1  2   8
>>> 2  3   9
>>> 3  4  10
>>> 4  5  11
>>> 5  6  12