dimod.Response.from_matrix

classmethod Response.from_matrix(samples, data_vectors, vartype=None, info=None, variable_labels=None)[source]

Build a response from a NumPy array-like object.

Parameters:
  • samples (array_like/str) – Samples as a numpy.matrix or NumPy array-like object. See Notes.
  • data_vectors (dict[field, numpy.array/list]) – Additional per-sample data as a dict of vectors. Each vector is the same length as samples_matrix. The key ‘energy’ and its vector are required.
  • vartype (Vartype, optional, default=None) – Vartype of the response. If not provided, vartype is inferred from the samples matrix if possible or a ValueError is raised.
  • info (dict, optional, default=None) – Information about the response as a whole formatted as a dict.
  • variable_labels (list, optional, default=None) – Maps (by index) variable labels to the columns of the samples matrix.
Returns:

A dimod Response object based on the input NumPy array-like object.

Return type:

Response

Raises:

ValueError – If vartype is not provided and samples are all 1s, have more than two unique values, or have values of an unknown vartype.

Examples

This example code snippet builds a response from a NumPy matrix.

samples = np.matrix([[0, 1], [1, 0]])
energies = [0.0, 1.0]
response = Response.from_matrix(samples, {'energy': energies})

This example code snippet builds a response from a NumPy array-like object (a Python list).

samples = [[0, 1], [1, 0]]
energies = [0.0, 1.0]
response = Response.from_matrix(samples, {'energy': energies})

Notes

SciPy defines array_like in the following way: “In general, numerical data arranged in an array-like structure in Python can be converted to arrays through the use of the array() function. The most obvious examples are lists and tuples. See the documentation for array() for details for its use. Some objects may support the array-protocol and allow conversion to arrays this way. A simple way to find out if the object can be converted to a numpy array using array() is simply to try it interactively and see if it works! (The Python Way).” [array_like]

References

[array_like]Docs.scipy.org. (2018). Array creation - NumPy v1.14 Manual. [online] Available at: https://docs.scipy.org/doc/numpy/user/basics.creation.html [Accessed 16 Feb. 2018].