dimod.as_samples¶
-
as_samples
(samples_like, dtype=None, copy=False, order='C')[source]¶ Convert a samples_like object to a NumPy array and list of labels.
- Parameters
samples_like (samples_like) – A collection of raw samples. samples_like is an extension of NumPy’s array_like structure. See examples below.
dtype (data-type, optional) – dtype for the returned samples array. If not provided, it is either derived from samples_like, if that object has a dtype, or set to the smallest dtype that can hold the given values.
copy (bool, optional, default=False) – If true, then samples_like is guaranteed to be copied, otherwise it is only copied if necessary.
order ({'K', 'A', 'C', 'F'}, optional, default='C') – Specify the memory layout of the array. See
numpy.array()
.
- Returns
A 2-tuple containing:
numpy.ndarray
: Samples.list: Variable labels
- Return type
Examples
The following examples convert a variety of samples_like objects:
NumPy arrays
>>> import numpy as np ... >>> dimod.as_samples(np.ones(5, dtype='int8')) (array([[1, 1, 1, 1, 1]], dtype=int8), [0, 1, 2, 3, 4]) >>> dimod.as_samples(np.zeros((5, 2), dtype='int8')) (array([[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], dtype=int8), [0, 1])
Lists
>>> dimod.as_samples([-1, +1, -1]) (array([[-1, 1, -1]], dtype=int8), [0, 1, 2]) >>> dimod.as_samples([[-1], [+1], [-1]]) (array([[-1], [ 1], [-1]], dtype=int8), [0])
Dicts
>>> dimod.as_samples({'a': 0, 'b': 1, 'c': 0}) (array([[0, 1, 0]], dtype=int8), ['a', 'b', 'c']) >>> dimod.as_samples([{'a': -1, 'b': +1}, {'a': 1, 'b': 1}]) (array([[-1, 1], [ 1, 1]], dtype=int8), ['a', 'b'])
A 2-tuple containing an array_like object and a list of labels
>>> dimod.as_samples(([-1, +1, -1], ['a', 'b', 'c'])) (array([[-1, 1, -1]], dtype=int8), ['a', 'b', 'c']) >>> dimod.as_samples((np.zeros((5, 2), dtype='int8'), ['in', 'out'])) (array([[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], dtype=int8), ['in', 'out'])