# MeanLinearVectorModel¶

class menpo.model.MeanLinearVectorModel(components, mean)[source]

Bases: LinearVectorModel

A Linear Model containing a matrix of vector components, each component vector being made up of features. The model additionally has a mean component which is handled accordingly when either:

1. A component of the model is selected

2. A projection operation is performed

Parameters
• components ((n_components, n_features) ndarray) – The components array.

• mean ((n_features,) ndarray) – The mean vector.

component(index, with_mean=True, scale=1.0)[source]

A particular component of the model, in vectorized form.

Parameters
• index (int) – The component that is to be returned

• with_mean (bool, optional) – If True, the component will be blended with the mean vector before being returned. If not, the component is returned on it’s own.

• scale (float, optional) – A scale factor that should be directly applied to the component. Only valid in the case where with_mean == True.

Returns

component_vector ((n_features,) ndarray) – The component vector.

copy()

Generate an efficient copy of this object.

Note that Numpy arrays and other Copyable objects on self will be deeply copied. Dictionaries and sets will be shallow copied, and everything else will be assigned (no copy will be made).

Classes that store state other than numpy arrays and immutable types should overwrite this method to ensure all state is copied.

Returns

type(self) – A copy of this object

instance(weights)

Creates a new vector instance of the model by weighting together the components.

Parameters

weights ((n_weights,) ndarray or list) –

The weightings for the first n_weights components that should be used.

weights[j] is the linear contribution of the j’th principal component to the instance vector.

Returns

vector ((n_features,) ndarray) – The instance vector for the weighting provided.

instance_vectors(weights)

Creates new vectorized instances of the model using all the components of the linear model.

Parameters

weights ((n_vectors, n_weights) ndarray or list of lists) –

The weightings for all components of the linear model. All components will be used to produce the instance.

weights[i, j] is the linear contribution of the j’th principal component to the i’th instance vector produced.

Raises

ValueError – If n_weights > n_available_components

Returns

vectors ((n_vectors, n_features) ndarray) – The instance vectors for the weighting provided.

mean()[source]

Return the mean of the model.

Type

ndarray

orthonormalize_against_inplace(linear_model)

Enforces that the union of this model’s components and another are both mutually orthonormal.

Both models keep its number of components unchanged or else a value error is raised.

Parameters

linear_model (LinearVectorModel) – A second linear model to orthonormalize this against.

Raises

ValueError – The number of features must be greater or equal than the sum of the number of components in both linear models ({} < {})

orthonormalize_inplace()

Enforces that this model’s components are orthonormalized, s.t. component_vector(i).dot(component_vector(j) = dirac_delta.

project(vector)

Projects the vector onto the model, retrieving the optimal linear reconstruction weights.

Parameters

vector ((n_features,) ndarray) – A vectorized novel instance.

Returns

weights ((n_components,) ndarray) – A vector of optimal linear weights.

project_out(vector)

Returns a version of vector where all the basis of the model have been projected out.

Parameters

vector ((n_features,) ndarray) – A novel vector.

Returns

projected_out ((n_features,) ndarray) – A copy of vector with all basis of the model projected out.

project_out_vectors(vectors)[source]

Returns a version of vectors where all the bases of the model have been projected out.

Parameters

vectors ((n_vectors, n_features) ndarray) – A matrix of novel vectors.

Returns

projected_out ((n_vectors, n_features) ndarray) – A copy of vectors with all bases of the model projected out.

project_vectors(vectors)[source]

Projects each of the vectors onto the model, retrieving the optimal linear reconstruction weights for each instance.

Parameters

vectors ((n_samples, n_features) ndarray) – Array of vectorized novel instances.

Returns

projected ((n_samples, n_components) ndarray) – The matrix of optimal linear weights.

reconstruct(vector)

Project a vector onto the linear space and rebuild from the weights found.

Parameters

vector ((n_features, ) ndarray) – A vectorized novel instance to project.

Returns

reconstructed ((n_features,) ndarray) – The reconstructed vector.

reconstruct_vectors(vectors)

Projects the vectors onto the linear space and rebuilds vectors from the weights found.

Parameters

vectors ((n_vectors, n_features) ndarray) – A set of vectors to project.

Returns

reconstructed ((n_vectors, n_features) ndarray) – The reconstructed vectors.

property components

The components matrix of the linear model.

Type

(n_available_components, n_features) ndarray

property n_components

The number of bases of the model.

Type

int

property n_features

The number of elements in each linear component.

Type

int