# matrix Package¶

## dense_matrix Module¶

Created on Sep 17, 2012

class composes.matrix.dense_matrix.DenseMatrix(data, *args, **kwargs)

classdocs

all_close(matrix_)

Checks of the values in two matrices are all_close.

Args:
matrix_: input matrix of type DenseMatrix
Returns:
bool: True if the elements are allclose (using np.allclose).
assert_positive()

Asserts that all values are larger or equal to 0.

Raises:
ValueError if not all values are >= 0.
get_non_negative()

Turns negative entries to 0.

Returns:
A new DenseMatrix matrix in which negative entries are set to 0.
hstack(matrix_)

Horizontal stack of two matrices.

Args:
matrix_: a second matrix of type DenseMatrix
Returns:
A DenseMatrix, horizontal stack of the two matrices.
Raises:
TypeError: if the argument is not of type DenseMatrix
static identity(size)

Builds the identity matrix.

Args:
size: integer, the result matrix is of shape size x size
Returns:
Identity DenseMatrix.
is_mostly_positive()

Checks if more than 50% of the non zero elements of a matrix are positive.

multiply(matrix_)

Computes component-wise multiplication of two matrices.

Args:
matrix_: a second matrix of type DenseMatrix
Returns:
A DenseMatrix containing the cw multiplication of the two.
Raises:
TypeError: if the argument is not of type DenseMatrix ValueError: if the two matrices don t have the same shape.
classmethod nary_hstack(mat_list)

Class method, horizontal stack of n matrices.

Args:
mat_list: a list of matrices of type DenseMatrix
Returns:
A DenseMatrix, horizontal stack of the arguments.
classmethod nary_vstack(mat_list)

Class method, vertical stack of n matrices.

Args:
mat_list: a list of matrices of type DenseMatrix
Returns:
A DenseMatrix, vertical stack of the arguments.
norm(axis=None)

Computes the norms on a certain axis or of the entire matrix.

Args:
axis: 0/1 or None, if axis is None computes the norm of the
full matrix
Returns:
nd.array containing the norms on a given axis, or a scalar if the axis is None.
plog()

Applies positive log to the matrix elements.

Elements smaller than 1 (leading to not-defined log or negative log) are set to 0. Log is applied on all other elements.

Modifies the current matrix.

remove_small_values(epsilon)

Sets values smaller than an epsilon to 0.

Args:
epsilon: scalar, threshold
Returns:
A DenseMatrix in which all values smaller than epsilon are
set to 0.
reshape(new_shape)

Reshapes current matrix.

Overwrites the current matrix with a new matrix of the given shape!

Args:
shape: length 2 tuple or pair of integers
Raises:
ValueError: if shape is not an integer pair or
if new shape is inconsistent with the total size of the current matrix.
scale_columns(array_)

Scales each column of the matrix by the values given in an array.

Args:
array_: ndarray containing the values to scale by
Returns:
A new DenseMatrix with scaled columns.
scale_rows(array_)

Scales each row of the matrix by the values given in an array.

Args:
array_: ndarray containing the values to scale by
Returns:
A new DenseMatrix with scaled rows.
to_dense_matrix(copy=False)

Returns a copy is copy=True, returns self otherwise.

to_non_negative()

Turns negative entries to 0.

Modifies the current matrix: all negative entries are set to 0.

to_ones()

Turns strictly positive entries to 1 and negative entries to 0.

Modifies the current matrix: all strictly positive entries are
set to 1, all negative entries are set to 0.
to_sparse_matrix()

Converts to SparseMatrix.

transpose()

Transposes the current matrix.

Returns:
DenseMatrix, a transpose of the current matrix.
vstack(matrix_)

Vertical stack of two matrices.

Args:
matrix_: a second matrix of type DenseMatrix
Returns:
A DenseMatrix, vertical stack of the two matrices.
Raises:
TypeError: if the argument is not of type DenseMatrix

## linalg Module¶

Created on Oct 4, 2012

class composes.matrix.linalg.Linalg

Bases: object

Contains a set of liniar algebra utilities defined to work both with sparse and with dense matrices as an input (i.e. with objects of type SparseMatrix/DenseMatrix).

Implements:
svd, nmf (LIN algorithm, add citation here!), pinv, ordinary least squares regression, ridge regression
classmethod lstsq_regression(matrix_a, matrix_b, intercept=False)

Performs Least Squares Regression.

Solves the problem:

$$X = argmin(||AX - B||_2)$$

Args:
matrix_a: input matrix A, of type Matrix matrix_b: input matrix A, of type Matrix intercept: bool. If True intercept is used. Optional, False by default.
Returns:
solution X of type Matrix
static nmf(v, w_init, h_init)

Performs Non-negative Matrix Factorization.

It solves the problem: $$W,H = argmin(||X - WH||_2)$$ such that W and H are non-negative matrices.

Args:
w_init: initial value for matrix W, type Matrix h_init: initial value for matrix H, type Matrix
Returns:
W, H <Matrix>: where W, H solve the NMF problem stated above.
static pinv(matrix_)

Computes the pseudo-inverse of a matrix.

Args:
matrix_: input matrix, of type Matrix
Returns:
Pseudo-inverse of input matrix, of type Matrix
Raises:
TypeError, if input is not of type Matrix
static ridge_regression(matrix_a, matrix_b, lambda_, intercept=False)

Performs Ridge Regression.

This method use the general formula:
...
to solve the problem:
$$X = argmin(||AX - B||_2 + \lambda||X||_2)$$
Args:
matrix_a: input matrix A, of type Matrix matrix_b: input matrix A, of type Matrix lambda_: scalar, lambda parameter intercept: bool. If True intercept is used. Optional, default False.
Returns:
solution X of type Matrix
static svd(matrix_, reduced_dimension)

Performs SVD decomposition.

If the rank is smaller than the requested reduced dimension, reduction to rank is performed. Dense SVD uses Linalg._SVD_TOL to decide the rank of the matrix.

Args:
matrix_: input of type Matrix reduced_dimension: int, the desired reduced dimension
Returns:
U,S,V of the decomposition X = USV^T. U, V: Matrix type, S: ndarray of singular values.

## matrix Module¶

Created on Sep 17, 2012

class composes.matrix.matrix.Matrix(*args, **kwargs)

Bases: object

Provides a common interface for matrix implementations.

Provides a common interface for different matrix implementations (sparse/dense). In vector space models, a matrix is used to encode a set of entities such as words or phrases (rows) described in terms of contextual features (columns).

assert_same_shape(matrix_)

Asserts that the matrix has the same shape as a second matrix.

Args:
matrix_: A second matrix of type Matrix.
Raises:
ValueError: If the current matrix and the argument matrix
do not have the same shape.
copy()
get_mat()
get_shape()
mat

Stores the actual matrix structure of the Matrix object. Of type numpy.matrix for DenseMatrix, and scipy.sparse.csr_matrix for SparseMatrix.

set_mat(mat_)
shape

Shape of the matrix, tuple with two elements.

sorted_permutation(norm_function, axis_)

Computes the permutation resulted when sorting the matrix on an axis, according to a function, in descending order.

Sorts the rows or the columns (as given by axis) of a matrix according to a norm_function and returns the permutation of this as a np.array

Args:
norm_function: One of sum/length. A function that
takes an axis as an argument (i.e. 0 or 1) and returns an array of values (i.e. sum of all rows if axis = 0 and norm_function = sum).

axis_: axis value, one of 0/1

Returns:
perm_srtd: np.array containing the permutation of the
sorting
sum(axis=None)

## sparse_matrix Module¶

Created on Sep 17, 2012

class composes.matrix.sparse_matrix.SparseMatrix(data)

classdocs

all_close(matrix_)

Checks of the values in two matrices are all_close.

Args:
matrix_: input matrix of type SparseMatrix
Returns:
bool: True if the elements are allclose (using np.allclose).
assert_positive()

Asserts that all values are larger or equal to 0.

Raises:
ValueError if not all values are >= 0.
get_non_negative()

Turns negative entries to 0.

Returns:
A new SparseMatrix matrix in which negative entries are set to 0.
hstack(matrix_)

Horizontal stack of two matrices.

Args:
matrix_: a second matrix of type SparseMatrix
Returns:
A SparseMatrix, horizontal stack of the two matrices.
Raises:
TypeError: if the argument is not of type SparseMatrix
static identity(size)

Builds the identity matrix.

Args:
size: integer, the result matrix is of shape size x size
Returns:
Identity SparseMatrix.
is_mostly_positive()

Checks if more than 50% of the non zero elements of a matrix are positive.

multiply(matrix_)

Computes component-wise multiplication of two matrices.

Args:
matrix_: a second matrix of type SparseMatrix
Returns:
A SparseMatrix containing the cw multiplication of the two.
Raises:
TypeError: if the argument is not of type SparseMatrix ValueError: if the two matrices don t have the same shape.
classmethod nary_hstack(mat_list)

Class method, horizontal stack of n matrices.

Args:
mat_list: a list of matrices of type SparseMatrix
Returns:
A SparseMatrix, horizontal stack of the arguments.
classmethod nary_vstack(mat_list)

Class method, vertical stack of n matrices.

Args:
mat_list: a list of matrices of type SparseMatrix
Returns:
A SparseMatrix, vertical stack of the arguments.
norm(axis=None)

Computes the norms on a certain axis or of the entire matrix.

Args:
axis: 0/1 or None, if axis is None computes the norm of the
full matrix
Returns:
nd.array containing the norms on a given axis, or a scalar if the axis is None.
plog()

Applies positive log to the matrix elements.

Elements smaller than 1 (leading to not-defined log or negative log) are set to 0. Log is applied on all other elements.

Modifies the current matrix.

remove_small_values(epsilon)

Sets values smaller than an epsilon to 0.

Args:
epsilon: scalar, threshold
Returns:
A SparseMatrix in which all values smaller than epsilon are
set to 0.
reshape(new_shape)

Reshapes current matrix.

Overwrites the current matrix with a new matrix of the given shape!

Args:
shape: length 2 tuple or pair of integers
Raises:
ValueError: if shape is not an integer pair or
if new shape is inconsistent with the total size of the current matrix.
scale_columns(array_)

Scales each column of the matrix by the values given in an array.

Args:
array_: ndarray containing the values to scale by
Returns:
A new SparseMatrix with scaled columns.
scale_rows(array_)

Scales each row of the matrix by the values given in an array.

Args:
array_: ndarray containing the values to scale by
Returns:
A new SparseMatrix with scaled rows.
to_dense_matrix()

Converts to DenseMatrix.

to_non_negative()

Turns negative entries to 0.

Modifies the current matrix: all negative entries are set to 0.

to_ones()

Turns strictly positive entries to 1 and negative entries to 0.

Modifies the current matrix: all strictly positive entries are
set to 1, all negative entries are set to 0.
to_sparse_matrix(copy=False)

Returns a copy is copy=True, returns self otherwise.

transpose()

Transposes the current matrix.

Returns:
SparseMatrix, a transpose of the current matrix.
vstack(matrix_)

Vertical stack of two matrices.

Args:
matrix_: a second matrix of type SparseMatrix
Returns:
A SparseMatrix, vertical stack of the two matrices.
Raises:
TypeError: if the argument is not of type SparseMatrix