dimod.as_samples

as_samples(samples_like: Union[Sequence[float], Mapping[Hashable, Union[float, numpy.floating, numpy.integer]], Sequence[Sequence[Sequence[Sequence[Sequence[Any]]]]], numpy.typing._array_like._SupportsArray[numpy.dtype], Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]], Sequence[Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]]], Sequence[Sequence[Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]]]], Sequence[Sequence[Sequence[Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]]]]], bool, int, float, complex, str, bytes, Sequence[Union[bool, int, float, complex, str, bytes]], Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]], Sequence[Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]]], Sequence[Sequence[Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]]]], Sequence[Sequence[float]], Tuple[Sequence[float], List[Hashable]], Tuple[Sequence[Sequence[float]], List[Hashable]], Sequence[Union[Sequence[float], Mapping[Hashable, Union[float, numpy.floating, numpy.integer]], Sequence[Sequence[Sequence[Sequence[Sequence[Any]]]]], numpy.typing._array_like._SupportsArray[numpy.dtype], Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]], Sequence[Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]]], Sequence[Sequence[Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]]]], Sequence[Sequence[Sequence[Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]]]]], bool, int, float, complex, str, bytes, Sequence[Union[bool, int, float, complex, str, bytes]], Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]], Sequence[Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]]], Sequence[Sequence[Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]]]]]], Iterator[Union[Sequence[float], Mapping[Hashable, Union[float, numpy.floating, numpy.integer]], Sequence[Sequence[Sequence[Sequence[Sequence[Any]]]]], numpy.typing._array_like._SupportsArray[numpy.dtype], Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]], Sequence[Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]]], Sequence[Sequence[Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]]]], Sequence[Sequence[Sequence[Sequence[numpy.typing._array_like._SupportsArray[numpy.dtype]]]]], bool, int, float, complex, str, bytes, Sequence[Union[bool, int, float, complex, str, bytes]], Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]], Sequence[Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]]], Sequence[Sequence[Sequence[Sequence[Union[bool, int, float, complex, str, bytes]]]]]]]], dtype: Union[numpy.dtype, None, type, numpy.typing._dtype_like._SupportsDType[numpy.dtype], str, Tuple[Any, int], Tuple[Any, Union[typing_extensions.SupportsIndex, Sequence[typing_extensions.SupportsIndex]]], List[Any], numpy.typing._dtype_like._DTypeDict, Tuple[Any, Any]] = None, copy: bool = False, order: str = 'C') Tuple[numpy.ndarray, List[Hashable]][source]

Convert a samples_like object to a NumPy array and list of labels.

Parameters
  • samples_like – A collection of raw samples. samples_like is an extension of NumPy’s array_like structure. See examples below.

  • dtype – 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 – If true, then samples_like is guaranteed to be copied, otherwise it is only copied if necessary.

  • order – Specify the memory layout of the array. See numpy.array().

Returns

A 2-tuple containing:

numpy.ndarray: Samples.

list: Variable labels

Return type

tuple

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'])

Deprecated since version 0.10.13: Support for a 2-tuple of (dict, list) as a samples-like will be removed in dimod 0.12.0.