Converts a None, list, np.ndarray, or torch.Tensor to np.ndarray
173185
def _to_numpy(Z):
        """"""
        if isinstance(Z, list):
            return [Classifier._to_numpy(z) for z in Z]
        else:
            return Classifier._to_numpy(Z)
182905
def _coerce_to_ndarray(cls, data):
        

        if not isinstance(data, (np.ndarray, Index)):
            if data is None or is_scalar(data):
                cls._scalar_data_error(data)

            # other iterable of some kind
            if not isinstance(data, (ABCSeries, list, tuple)):
                data = list(data)
            data = np.asarray(data)
        return data
134794
def to_array(tensor):  # type: (TensorProto) -> np.ndarray[Any]
    
    if tensor.HasField("segment"):
        raise ValueError(
            "Currently not supporting loading segments.")
    if tensor.data_type == TensorProto.UNDEFINED:
        raise ValueError("The data type is not defined.")

    tensor_dtype = tensor.data_type
    np_dtype = mapping.TENSOR_TYPE_TO_NP_TYPE[tensor_dtype]
    storage_type = mapping.TENSOR_TYPE_TO_STORAGE_TENSOR_TYPE[tensor_dtype]
    storage_np_dtype = mapping.TENSOR_TYPE_TO_NP_TYPE[storage_type]
    storage_field = mapping.STORAGE_TENSOR_TYPE_TO_FIELD[storage_type]
    dims = tensor.dims

    if tensor.data_type == TensorProto.STRING:
        utf8_strings = getattr(tensor, storage_field)
        ss = list(s.decode('utf-8') for s in utf8_strings)
        return np.asarray(ss).astype(np_dtype).reshape(dims)

    if tensor.HasField("raw_data"):
        # Raw_bytes support: using frombuffer.
        return np.frombuffer(
            tensor.raw_data,
            dtype=np_dtype).reshape(dims)
    else:
        data = getattr(tensor, storage_field),  # type: Sequence[np.complex64]
        if (tensor_dtype == TensorProto.COMPLEX64
                or tensor_dtype == TensorProto.COMPLEX128):
            data = combine_pairs_to_complex(data)
        return (
            np.asarray(
                data,
                dtype=storage_np_dtype)
            .astype(np_dtype)
            .reshape(dims)
        )
702892
def cast(attrs, inputs, proto_obj):
    
    try:
        from onnx.mapping import TENSOR_TYPE_TO_NP_TYPE
    except ImportError:
        raise ImportError("Onnx and protobuf need to be installed. "
                          + "Instructions to install - https://github.com/onnx/onnx")
    new_attrs = translation_utils._fix_attribute_names(attrs, {'to' : 'dtype'})
    new_attrs['dtype'] = TENSOR_TYPE_TO_NP_TYPE[int(new_attrs['dtype'])]
    return 'cast', new_attrs, inputs
1000011
def dict_to_numpy_dict(obj_dict):
    
    return {key: np.asarray(value) if value is not None else None for key, value in obj_dict.items()}
23801
def wrap_in_ndarray(value):
    

    if hasattr(value, "__len__"):
        return np.array(value)
    else:
        return np.array([value])
448535
def combine_initial_dims(tensor: torch.Tensor) -> torch.Tensor:
    
    if tensor.dim() <= 2:
        return tensor
    else:
        return tensor.view(-1, tensor.size(-1))
491569
def _safe_sparse_mask(tensor: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
    
    # pylint: disable=protected-access
    try:
        return tensor.sparse_mask(mask)
    except AttributeError:
        # TODO(joelgrus): remove this and/or warn at some point
        return tensor._sparse_mask(mask)
1185602
def _to_ndarray(self, a):
        """"""

        if isinstance(a, (list, tuple)):
            a = numpy.array(a)

        if not is_ndarray(a):
            raise TypeError("Expected an ndarray but got object of type '{}' instead".format(type(a)))

        return a
1155632
def _remove_none_values(dictionary):
    
    return list(map(dictionary.pop,
                    [i for i in dictionary if dictionary[i] is None]))
1291001
def to_float(*args):
    
    floats = [np.array(a, dtype=np.float32) for a in args]
    return floats[0] if len(floats) == 1 else floats
735538
def to_vector(np_array):
    
    if len(np_array.shape) == 1:
        return Vectors.dense(np_array)
    else:
        raise Exception("An MLLib Vector can only be created from a one-dimensional " +
                        "numpy array, got {}".format(len(np_array.shape)))
931290
def as_list(self):
        
        if self._dims is None:
            raise ValueError("as_list() is not defined on an unknown TensorShape.")
        return [dim.value for dim in self._dims]
167266
def shape_list(l,shape,dtype):
    
    return np.array(l, dtype=dtype).reshape(shape)
841280
def _to_numpy(Z):
        """"""
        if Z is None:
            return Z
        elif issparse(Z):
            return Z.toarray()
        elif isinstance(Z, np.ndarray):
            return Z
        elif isinstance(Z, list):
            return np.array(Z)
        elif isinstance(Z, torch.Tensor):
            return Z.cpu().numpy()
        else:
            msg = (
                f"Expected None, list, numpy.ndarray or torch.Tensor, "
                f"got {type(Z)} instead."
            )
            raise Exception(msg)
691698
def remove_nones(**kwargs):
    
    return dict((k, v) for k, v in kwargs.iteritems() if v is not None)
413976
def _not_none(items):
    """"""
    if not isinstance(items, (tuple, list)):
        items = (items,)
    return all(item is not _none for item in items)