dimod.as_samples#
- as_samples(samples_like: ~typing.Sequence[float | ~numpy.floating | ~numpy.integer] | ~typing.Mapping[~typing.Hashable, float | ~numpy.floating | ~numpy.integer] | ~typing.Tuple[~typing.Sequence[float | ~numpy.floating | ~numpy.integer], ~typing.Sequence[~typing.Hashable]] | ~typing.Tuple[~numpy.ndarray, ~typing.Sequence[~typing.Hashable]] | ~numpy.ndarray | ~typing.Sequence[~typing.Sequence[float | ~numpy.floating | ~numpy.integer]] | ~typing.Tuple[~typing.Sequence[~typing.Sequence[float | ~numpy.floating | ~numpy.integer]], ~typing.Sequence[~typing.Hashable]] | ~typing.Sequence[~typing.Sequence[float | ~numpy.floating | ~numpy.integer] | ~typing.Mapping[~typing.Hashable, float | ~numpy.floating | ~numpy.integer] | ~typing.Tuple[~typing.Sequence[float | ~numpy.floating | ~numpy.integer], ~typing.Sequence[~typing.Hashable]] | ~typing.Tuple[~numpy.ndarray, ~typing.Sequence[~typing.Hashable]] | ~numpy.ndarray] | ~typing.Iterator[~typing.Sequence[float | ~numpy.floating | ~numpy.integer] | ~typing.Mapping[~typing.Hashable, float | ~numpy.floating | ~numpy.integer] | ~typing.Tuple[~typing.Sequence[float | ~numpy.floating | ~numpy.integer], ~typing.Sequence[~typing.Hashable]] | ~typing.Tuple[~numpy.ndarray, ~typing.Sequence[~typing.Hashable]] | ~numpy.ndarray], dtype: ~numpy.dtype[~typing.Any] | None | type[~typing.Any] | ~numpy._typing._dtype_like._SupportsDType[~numpy.dtype[~typing.Any]] | str | tuple[~typing.Any, int] | tuple[~typing.Any, ~typing.SupportsIndex | ~collections.abc.Sequence[~typing.SupportsIndex]] | list[~typing.Any] | ~numpy._typing._dtype_like._DTypeDict | tuple[~typing.Any, ~typing.Any] = None, copy: bool = False, order: ~typing.Literal['K', 'A', 'C', 'F'] = 'C', labels_type: type = <class 'list'>) Tuple[ndarray, Sequence[Hashable]] [source]#
- as_samples(samples_like: ~typing.Iterator[~typing.Sequence[float | ~numpy.floating | ~numpy.integer] | ~typing.Mapping[~typing.Hashable, float | ~numpy.floating | ~numpy.integer] | ~typing.Tuple[~typing.Sequence[float | ~numpy.floating | ~numpy.integer], ~typing.Sequence[~typing.Hashable]] | ~typing.Tuple[~numpy.ndarray, ~typing.Sequence[~typing.Hashable]] | ~numpy.ndarray], labels_type: type = <class 'list'>, **kwargs) Tuple[ndarray, Sequence[Hashable]]
- as_samples(samples_like: ~typing.Mapping[~typing.Hashable, float], dtype: ~numpy.dtype[~typing.Any] | None | type[~typing.Any] | ~numpy._typing._dtype_like._SupportsDType[~numpy.dtype[~typing.Any]] | str | tuple[~typing.Any, int] | tuple[~typing.Any, ~typing.SupportsIndex | ~collections.abc.Sequence[~typing.SupportsIndex]] | list[~typing.Any] | ~numpy._typing._dtype_like._DTypeDict | tuple[~typing.Any, ~typing.Any] = None, copy: bool = False, order: ~typing.Literal['K', 'A', 'C', 'F'] = 'C', labels_type: type = <class 'list'>) Tuple[ndarray, Sequence[Hashable]]
- as_samples(samples_like: ~typing.Tuple[~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]] | ~numpy._typing._nested_sequence._NestedSequence[~numpy._typing._array_like._SupportsArray[~numpy.dtype[~typing.Any]]] | bool | int | float | complex | str | bytes | ~numpy._typing._nested_sequence._NestedSequence[bool | int | float | complex | str | bytes], ~typing.Sequence[~typing.Hashable]], dtype: ~numpy.dtype[~typing.Any] | None | type[~typing.Any] | ~numpy._typing._dtype_like._SupportsDType[~numpy.dtype[~typing.Any]] | str | tuple[~typing.Any, int] | tuple[~typing.Any, ~typing.SupportsIndex | ~collections.abc.Sequence[~typing.SupportsIndex]] | list[~typing.Any] | ~numpy._typing._dtype_like._DTypeDict | tuple[~typing.Any, ~typing.Any] = None, copy: bool = False, order: ~typing.Literal['K', 'A', 'C', 'F'] = 'C', labels_type: type = <class 'list'>) Tuple[ndarray, Sequence[Hashable]]
- as_samples(samples_like: ~dimod.sampleset.SampleSet, dtype: ~numpy.dtype[~typing.Any] | None | type[~typing.Any] | ~numpy._typing._dtype_like._SupportsDType[~numpy.dtype[~typing.Any]] | str | tuple[~typing.Any, int] | tuple[~typing.Any, ~typing.SupportsIndex | ~collections.abc.Sequence[~typing.SupportsIndex]] | list[~typing.Any] | ~numpy._typing._dtype_like._DTypeDict | tuple[~typing.Any, ~typing.Any] = None, copy: bool = False, order: ~typing.Literal['K', 'A', 'C', 'F'] = 'C', labels_type: type = <class 'list'>) Tuple[ndarray, List[Hashable]]
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.