MeanInstanceLinearModel

class menpo.model.MeanInstanceLinearModel(components, mean_vector, template_instance)[source]

Bases: MeanLinearModel, InstanceBackedModel

Mixin of MeanLinearModel and InstanceBackedModel objects.

Parameters:
  • components ((n_components, n_features) ndarray) – The components array.
  • mean_vector ((n_features,) ndarray) – The mean vector.
  • template_instance (Vectorizable) – The template instance.
component(index, with_mean=True, scale=1.0)[source]

Return a particular component of the linear model.

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 applied to the component. Only valid in the case where with_mean == True. See component_vector() for how this scale factor is interpreted.
Returns:

component (type(self.template_instance)) – The requested component.

component_vector(index, with_mean=True, scale=1.0)

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 instance of the model using the first len(weights) components.

Parameters:weights ((n_weights,) ndarray or list) – weights[i] is the linear contribution of the i’th component to the instance vector.
Raises:ValueError – If n_weights > n_components
Returns:instance (type(self.template_instance)) – An instance of the model.
instance_vector(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:Vectorizable
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 (LinearModel) – 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(instance)

Projects the instance onto the model, retrieving the optimal linear weightings.

Parameters:novel_instance (Vectorizable) – A novel instance.
Returns:projected ((n_components,) ndarray) – A vector of optimal linear weightings.
project_out(instance)

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

Parameters:instance (Vectorizable) – A novel instance of Vectorizable.
Returns:projected_out (self.instance_class) – A copy of instance, with all basis of the model projected out.
project_out_vector(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)

Returns a version of vectors where all the basis 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 basis of the model projected out.
project_vector(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_vectors(vectors)

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(instance)

Projects a instance onto the linear space and rebuilds from the weights found.

Syntactic sugar for:

instance(project(instance))

but faster, as it avoids the conversion that takes place each time.

Parameters:instance (Vectorizable) – A novel instance of Vectorizable.
Returns:reconstructed (self.instance_class) – The reconstructed object.
reconstruct_vector(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.
components

The components matrix of the linear model.

Type:(n_available_components, n_features) ndarray
n_components

The number of bases of the model.

Type:int
n_features

The number of elements in each linear component.

Type:int