dimod.as_samples¶
- as_samples(samples_like: typing.Union[typing.Sequence[typing.Union[float, numpy.floating, numpy.integer]], typing.Mapping[typing.Hashable, typing.Union[float, numpy.floating, numpy.integer]], typing.Tuple[typing.Sequence[typing.Union[float, numpy.floating, numpy.integer]], typing.Sequence[typing.Hashable]], typing.Tuple[numpy.ndarray, typing.Sequence[typing.Hashable]], numpy.ndarray, typing.Sequence[typing.Sequence[typing.Union[float, numpy.floating, numpy.integer]]], typing.Tuple[typing.Sequence[typing.Sequence[typing.Union[float, numpy.floating, numpy.integer]]], typing.Sequence[typing.Hashable]], typing.Sequence[typing.Union[typing.Sequence[typing.Union[float, numpy.floating, numpy.integer]], typing.Mapping[typing.Hashable, typing.Union[float, numpy.floating, numpy.integer]], typing.Tuple[typing.Sequence[typing.Union[float, numpy.floating, numpy.integer]], typing.Sequence[typing.Hashable]], typing.Tuple[numpy.ndarray, typing.Sequence[typing.Hashable]], numpy.ndarray]], typing.Iterator[typing.Union[typing.Sequence[typing.Union[float, numpy.floating, numpy.integer]], typing.Mapping[typing.Hashable, typing.Union[float, numpy.floating, numpy.integer]], typing.Tuple[typing.Sequence[typing.Union[float, numpy.floating, numpy.integer]], typing.Sequence[typing.Hashable]], typing.Tuple[numpy.ndarray, typing.Sequence[typing.Hashable]], numpy.ndarray]]], dtype: typing.Union[numpy.dtype, None, type, numpy.typing._dtype_like._SupportsDType[numpy.dtype], str, typing.Tuple[typing.Any, int], typing.Tuple[typing.Any, typing.Union[typing_extensions.SupportsIndex, typing.Sequence[typing_extensions.SupportsIndex]]], typing.List[typing.Any], numpy.typing._dtype_like._DTypeDict, typing.Tuple[typing.Any, typing.Any]] = None, copy: bool = False, order: str = 'C', labels_type: type = <class 'list'>) Tuple[numpy.ndarray, Sequence[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()
.labels_type – The return type of the variables labels.
labels_type
should be aSequence
. Thelabels_type
constructor should accept zero arguments, or an iterable as a single argument.
- Returns
A 2-tuple containing the samples as a
ndarray
and the variables labels, as alabels_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'])
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.