TensorCPD

# Import statement

from hottbox.core import TensorCPD
class TensorCPD(fmat, core_values, mode_names=None)[source]

Representation of a tensor in the Kruskal form (CPD).

Parameters
fmatlist[np.ndarray]

List of factor matrices for the CP representation of a tensor

core_valuesnp.ndarray

Array of coefficients on the super-diagonal of a core for the CP representation of a tensor

mode_nameslist[str]

List of names for the factor matrices

Attributes

_fmat

(list[np.ndarray]) Placeholder for a list of factor matrices for the CP representation of a tensor

_core_values

(np.ndarray) Placeholder for an array of coefficients on the super-diagonal of a core for the CP representation of a tensor

_modes

(list[Mode]) Description of the factor matrix for the corresponding mode

Raises
TensorTopologyError

If there is inconsistency in shapes of factor matrices and core values

Examples

  1. Create kruskal representation of a tensor with default meta information

>>> import numpy as np
>>> from hottbox.core import TensorCPD
>>> I, J, K = 5, 6, 7   # shape of the tensor in full form
>>> R = 4               # Kruskal rank
>>> A = np.ones((I, R))
>>> B = np.ones((J, R))
>>> C = np.ones((K, R))
>>> fmat = [A, B , C]
>>> core_values = np.arange(R)
>>> tensor_cpd = TensorCPD(fmat, core_values)
>>> print(tensor_cpd)
    Kruskal representation of a tensor with rank=(4,).
    Factor matrices represent properties: ['mode-0', 'mode-1', 'mode-2']
    With corresponding latent components described by (5, 6, 7) features respectively.
  1. Create kruskal representation of a tensor with custom meta information

>>> import numpy as np
>>> from hottbox.core import TensorCPD
>>> I, J, K = 5, 6, 7   # shape of the tensor in full form
>>> R = 4               # Kruskal rank
>>> A = np.ones((I, R))
>>> B = np.ones((J, R))
>>> C = np.ones((K, R))
>>> fmat = [A, B , C]
>>> core_values = np.arange(R)
>>> mode_names = ["Year", "Month", "Day"]
>>> tensor_cpd = TensorCPD(fmat, core_values, mode_names)
>>> print(tensor_cpd)
    Kruskal representation of a tensor with rank=(4,).
    Factor matrices represent properties: ['Year', 'Month', 'Day']
    With corresponding latent components described by (5, 6, 7) features respectively.

Methods

copy(self)

Produces a copy of itself as a new object

copy_modes(self, tensor)

Copy modes meta from tensor

reconstruct(self[, keep_meta])

Converts the CP representation of a tensor into a full tensor

reset_mode_index(self[, mode])

Drop index for the specified mode number

reset_mode_name(self[, mode])

Set default name for the specified mode number

set_mode_index(self, mode_index)

Set index for specified mode

set_mode_names(self, mode_names)

Rename modes of a tensor representation

copy(self)[source]

Produces a copy of itself as a new object

Returns
new_objectTensorCPD
copy_modes(self, tensor)[source]

Copy modes meta from tensor

Parameters
tensor{Tensor, TensorCPD, TensorTKD, TensorTT}
Returns
selfTensorCPD

Notes

Most of the time this method should only be used by the decomposition algorithms

property core

Core tensor of the CP representation of a tensor

Returns
core_tensorTensor
property fmat

List of factor matrices for the CP representation of a tensor

Returns
factor_matriceslist[np.ndarray]
property ft_shape

Shape of a TensorCPD in the full format

Returns
full_shapetuple
property mode_names

Description of the physical modes for a TensorCPD

Returns
list[str]
property modes

Meta data for the factor matrices

Returns
list[Mode]
property order

Order of a tensor represented through the CPD

Returns
orderint
property rank

Rank of the CP representation of a tensor.

Returns
ranktuple

Notes

Most often referred to as the Kryskal rank

reconstruct(self, keep_meta=0)[source]

Converts the CP representation of a tensor into a full tensor

Parameters
keep_metaint

Keep meta information about modes of the given tensor. 0 - the output will have default values for the meta data 1 - keep only mode names 2 - keep mode names and indices

Returns
tensorTensor
reset_mode_index(self, mode=None)[source]

Drop index for the specified mode number

Parameters
modeint

Mode number which index to be dropped By default resets all indices

Returns
selfTensorCPD
reset_mode_name(self, mode=None)[source]

Set default name for the specified mode number

Parameters
modeint

Mode number which name to be set to default value By default resets names of all modes

Returns
selfTensorCPD
set_mode_index(self, mode_index)[source]

Set index for specified mode

Parameters
mode_indexdict

New indices for the factor matrices in form of a dictionary. Key defines the mode whose index to be changed. Value contains a list of new indices for this mode.

Returns
selfTensorCPD
set_mode_names(self, mode_names)[source]

Rename modes of a tensor representation

Parameters
mode_namesdict

New names for the tensor modes in form of a dictionary The name of the mode defined by the Key of the dict will be renamed to the corresponding Value

Returns
selfTensorCPD