Python numpy.signedinteger() Examples

The following are 30 code examples for showing how to use numpy.signedinteger(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: gradient_boosting.py    License: MIT License 6 votes vote down vote up
def fit(self, X, y, sample_weight=None):
        """Fit the estimator.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Training data

        y : numpy, shape (n_samples, n_targets)
            Target values. Will be cast to X's dtype if necessary

        sample_weight : array, shape (n_samples,)
            Individual weights for each sample
        """
        if np.issubdtype(y.dtype, np.signedinteger):
            # classification
            self.n_classes = np.unique(y).shape[0]
            if self.n_classes == 2:
                self.n_classes = 1
        else:
            # regression
            self.n_classes = 1 
Example 2
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: __init__.py    License: MIT License 6 votes vote down vote up
def safe_mask(X, mask):
    """Return a mask which is safe to use on X.

    Parameters
    ----------
    X : {array-like, sparse matrix}
        Data on which to apply mask.

    mask : array
        Mask to be used on X.

    Returns
    -------
        mask
    """
    mask = np.asarray(mask)
    if np.issubdtype(mask.dtype, np.signedinteger):
        return mask

    if hasattr(X, "toarray"):
        ind = np.arange(mask.shape[0])
        mask = ind[mask]
    return mask 
Example 3
Project: trax   Author: google   File: lax_numpy_test.py    License: Apache License 2.0 6 votes vote down vote up
def _dtypes_are_compatible_for_bitwise_ops(args):
  if len(args) <= 1:
    return True
  is_signed = lambda dtype: lnp.issubdtype(dtype, onp.signedinteger)
  width = lambda dtype: lnp.iinfo(dtype).bits
  x, y = args
  # `lnp.iinfo(dtype).bits` can't be called on bools, so we convert bools to
  # ints.
  if x == lnp.bool_:
    x = lnp.int32
  if y == lnp.bool_:
    y = lnp.int32
  if width(x) > width(y):
    x, y = y, x
  if x == lnp.uint32 and y == lnp.uint64:
    return False
  # The following condition seems a little ad hoc, but seems to capture what
  # numpy actually implements.
  return (
      is_signed(x) == is_signed(y)
      or (width(x) == 32 and width(y) == 32)
      or (width(x) == 32 and width(y) == 64 and is_signed(y))) 
Example 4
Project: coremltools   Author: apple   File: type_mapping.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def promote_types(dtype1, dtype2):
    """
    Get the smallest type to which the given scalar types can be cast.

    Args:
        dtype1 (builtin):
        dtype2 (builtin):

    Returns:
        A builtin datatype or None.
    """
    nptype1 = nptype_from_builtin(dtype1)
    nptype2 = nptype_from_builtin(dtype2)
    # Circumvent the undesirable np type promotion:
    # >> np.promote_types(np.float32, np.int)
    # dtype('float64')
    if np.issubdtype(nptype1, np.floating) and np.issubdtype(nptype2, np.signedinteger):
        nppromoted = nptype1
    elif np.issubdtype(nptype2, np.floating) and np.issubdtype(
        nptype1, np.signedinteger
    ):
        nppromoted = nptype2
    else:
        nppromoted = np.promote_types(nptype1, nptype2)
    return numpy_type_to_builtin_type(nppromoted) 
Example 5
Project: sklearn-onnx   Author: onnx   File: label_encoder.py    License: MIT License 6 votes vote down vote up
def convert_sklearn_label_encoder(scope, operator, container):
    op = operator.raw_operator
    op_type = 'LabelEncoder'
    attrs = {'name': scope.get_unique_operator_name(op_type)}
    classes = op.classes_
    if np.issubdtype(classes.dtype, np.floating):
        attrs['keys_floats'] = classes
    elif np.issubdtype(classes.dtype, np.signedinteger):
        attrs['keys_int64s'] = classes
    else:
        attrs['keys_strings'] = np.array([s.encode('utf-8') for s in classes])
    attrs['values_int64s'] = np.arange(len(classes))

    container.add_node(op_type, operator.input_full_names,
                       operator.output_full_names, op_domain='ai.onnx.ml',
                       op_version=2, **attrs) 
Example 6
Project: twitter-stock-recommendation   Author: alvarobartt   File: __init__.py    License: MIT License 6 votes vote down vote up
def safe_mask(X, mask):
    """Return a mask which is safe to use on X.

    Parameters
    ----------
    X : {array-like, sparse matrix}
        Data on which to apply mask.

    mask : array
        Mask to be used on X.

    Returns
    -------
        mask
    """
    mask = np.asarray(mask)
    if np.issubdtype(mask.dtype, np.signedinteger):
        return mask

    if hasattr(X, "toarray"):
        ind = np.arange(mask.shape[0])
        mask = ind[mask]
    return mask 
Example 7
Project: onnxmltools   Author: onnx   File: Classifier.py    License: MIT License 6 votes vote down vote up
def calculate_xgboost_classifier_output_shapes(operator):
    check_input_and_output_numbers(operator, input_count_range=1, output_count_range=2)
    check_input_and_output_types(operator, good_input_types=[FloatTensorType, Int64TensorType])
    N = operator.inputs[0].type.shape[0]

    xgb_node = operator.raw_operator
    params = get_xgb_params(xgb_node)
    booster = xgb_node.get_booster()
    atts = booster.attributes()
    ntrees = len(booster.get_dump(with_stats=True, dump_format = 'json'))
    objective = params["objective"]
            
    if objective == "binary:logistic":
        ncl = 2
    else:
        ncl = ntrees // params['n_estimators']
        if objective == "reg:logistic" and ncl == 1:
            ncl = 2
    classes = xgb_node.classes_
    if (np.issubdtype(classes.dtype, np.floating) or
            np.issubdtype(classes.dtype, np.signedinteger)):
        operator.outputs[0].type = Int64TensorType(shape=[N])
    else:
        operator.outputs[0].type = StringTensorType(shape=[N])
    operator.outputs[1].type = operator.outputs[1].type = FloatTensorType([N, ncl]) 
Example 8
Project: PHATE   Author: KrishnaswamyLab   File: test.py    License: GNU General Public License v2.0 5 votes vote down vote up
def test_simple():
    tree_data, tree_clusters = phate.tree.gen_dla(n_branch=3)
    phate_operator = phate.PHATE(knn=15, t=100, verbose=False)
    tree_phate = phate_operator.fit_transform(tree_data)
    assert tree_phate.shape == (tree_data.shape[0], 2)
    clusters = phate.cluster.kmeans(phate_operator, n_clusters='auto')
    assert np.issubdtype(clusters.dtype, np.signedinteger)
    assert len(np.unique(clusters)) >= 2
    assert len(clusters.shape) == 1
    assert len(clusters) == tree_data.shape[0]
    clusters = phate.cluster.kmeans(phate_operator, n_clusters=3)
    assert np.issubdtype(clusters.dtype, np.signedinteger)
    assert len(np.unique(clusters)) == 3
    assert len(clusters.shape) == 1
    assert len(clusters) == tree_data.shape[0]
    phate_operator.fit(phate_operator.graph)
    G = graphtools.Graph(
        phate_operator.graph.kernel,
        precomputed="affinity",
        use_pygsp=True,
        verbose=False,
    )
    phate_operator.fit(G)
    G = pygsp.graphs.Graph(G.W)
    phate_operator.fit(G)
    phate_operator.fit(anndata.AnnData(tree_data))
    with assert_raises_message(TypeError, "Expected phate_op to be of type PHATE. Got 1"):
        phate.cluster.kmeans(1) 
Example 9
Project: recruit   Author: Frank-qlu   File: test_abc.py    License: Apache License 2.0 5 votes vote down vote up
def test_abstract(self):
        assert_(issubclass(np.number, numbers.Number))

        assert_(issubclass(np.inexact, numbers.Complex))
        assert_(issubclass(np.complexfloating, numbers.Complex))
        assert_(issubclass(np.floating, numbers.Real))

        assert_(issubclass(np.integer, numbers.Integral))
        assert_(issubclass(np.signedinteger, numbers.Integral))
        assert_(issubclass(np.unsignedinteger, numbers.Integral)) 
Example 10
Project: recruit   Author: Frank-qlu   File: numeric.py    License: Apache License 2.0 5 votes vote down vote up
def _assert_safe_casting(cls, data, subarr):
        """
        Ensure incoming data can be represented as ints.
        """
        if not issubclass(data.dtype.type, np.signedinteger):
            if not np.array_equal(data, subarr):
                raise TypeError('Unsafe NumPy casting, you must '
                                'explicitly cast') 
Example 11
Project: lambda-packs   Author: ryfeus   File: measurements.py    License: MIT License 5 votes vote down vote up
def _safely_castable_to_int(dt):
    """Test whether the numpy data type `dt` can be safely cast to an int."""
    int_size = np.dtype(int).itemsize
    safe = ((np.issubdtype(dt, np.signedinteger) and dt.itemsize <= int_size) or
            (np.issubdtype(dt, np.unsignedinteger) and dt.itemsize < int_size))
    return safe 
Example 12
Project: vnpy_crypto   Author: birforce   File: test_abc.py    License: MIT License 5 votes vote down vote up
def test_abstract(self):
        assert_(issubclass(np.number, numbers.Number))

        assert_(issubclass(np.inexact, numbers.Complex))
        assert_(issubclass(np.complexfloating, numbers.Complex))
        assert_(issubclass(np.floating, numbers.Real))

        assert_(issubclass(np.integer, numbers.Integral))
        assert_(issubclass(np.signedinteger, numbers.Integral))
        assert_(issubclass(np.unsignedinteger, numbers.Integral)) 
Example 13
Project: vnpy_crypto   Author: birforce   File: numeric.py    License: MIT License 5 votes vote down vote up
def _assert_safe_casting(cls, data, subarr):
        """
        Ensure incoming data can be represented as ints.
        """
        if not issubclass(data.dtype.type, np.signedinteger):
            if not np.array_equal(data, subarr):
                raise TypeError('Unsafe NumPy casting, you must '
                                'explicitly cast') 
Example 14
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_abc.py    License: MIT License 5 votes vote down vote up
def test_abstract(self):
        assert_(issubclass(np.number, numbers.Number))

        assert_(issubclass(np.inexact, numbers.Complex))
        assert_(issubclass(np.complexfloating, numbers.Complex))
        assert_(issubclass(np.floating, numbers.Real))

        assert_(issubclass(np.integer, numbers.Integral))
        assert_(issubclass(np.signedinteger, numbers.Integral))
        assert_(issubclass(np.unsignedinteger, numbers.Integral)) 
Example 15
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_abc.py    License: MIT License 5 votes vote down vote up
def test_abstract(self):
        assert_(issubclass(np.number, numbers.Number))

        assert_(issubclass(np.inexact, numbers.Complex))
        assert_(issubclass(np.complexfloating, numbers.Complex))
        assert_(issubclass(np.floating, numbers.Real))

        assert_(issubclass(np.integer, numbers.Integral))
        assert_(issubclass(np.signedinteger, numbers.Integral))
        assert_(issubclass(np.unsignedinteger, numbers.Integral)) 
Example 16
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_base.py    License: MIT License 5 votes vote down vote up
def test_constructor6(self):
        # infer dimensions and dtype from lists
        indptr = [0, 1, 3, 3]
        indices = [0, 5, 1, 2]
        data = [1, 2, 3, 4]
        csr = csr_matrix((data, indices, indptr))
        assert_array_equal(csr.shape, (3,6))
        assert_(np.issubdtype(csr.dtype, np.signedinteger)) 
Example 17
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_base.py    License: MIT License 5 votes vote down vote up
def test_constructor6(self):
        # infer dimensions and dtype from lists
        indptr = [0, 1, 3, 3]
        indices = [0, 5, 1, 2]
        data = [1, 2, 3, 4]
        csc = csc_matrix((data, indices, indptr))
        assert_array_equal(csc.shape,(6,3))
        assert_(np.issubdtype(csc.dtype, np.signedinteger)) 
Example 18
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_kdtree.py    License: MIT License 5 votes vote down vote up
def test_single_query(self):
        d, i = self.kdtree.query(np.array([0,0,0]))
        assert_(isinstance(d,float))
        assert_(np.issubdtype(i, np.signedinteger)) 
Example 19
Project: dcor   Author: vnmabus   File: _utils.py    License: MIT License 5 votes vote down vote up
def _can_be_double(x):
    """
    Return if the array can be safely converted to double.

    That happens when the dtype is a float with the same size of
    a double or narrower, or when is an integer that can be safely
    converted to double (if the roundtrip conversion works).

    """
    return ((np.issubdtype(x.dtype, np.floating) and
             x.dtype.itemsize <= np.dtype(float).itemsize) or
            (np.issubdtype(x.dtype, np.signedinteger) and
             np.can_cast(x, float))) 
Example 20
def test_abstract(self):
        assert_(issubclass(np.number, numbers.Number))

        assert_(issubclass(np.inexact, numbers.Complex))
        assert_(issubclass(np.complexfloating, numbers.Complex))
        assert_(issubclass(np.floating, numbers.Real))

        assert_(issubclass(np.integer, numbers.Integral))
        assert_(issubclass(np.signedinteger, numbers.Integral))
        assert_(issubclass(np.unsignedinteger, numbers.Integral)) 
Example 21
def _assert_safe_casting(cls, data, subarr):
        """
        Ensure incoming data can be represented as ints.
        """
        if not issubclass(data.dtype.type, np.signedinteger):
            if not np.array_equal(data, subarr):
                raise TypeError('Unsafe NumPy casting, you must '
                                'explicitly cast') 
Example 22
Project: pySINDy   Author: luckystarufo   File: test_abc.py    License: MIT License 5 votes vote down vote up
def test_abstract(self):
        assert_(issubclass(np.number, numbers.Number))

        assert_(issubclass(np.inexact, numbers.Complex))
        assert_(issubclass(np.complexfloating, numbers.Complex))
        assert_(issubclass(np.floating, numbers.Real))

        assert_(issubclass(np.integer, numbers.Integral))
        assert_(issubclass(np.signedinteger, numbers.Integral))
        assert_(issubclass(np.unsignedinteger, numbers.Integral)) 
Example 23
Project: Brancher   Author: AI-DI   File: utilities.py    License: MIT License 5 votes vote down vote up
def coerce_to_dtype(data, is_observed=False):
    """Summary"""
    def reformat_tensor(result):
        if is_observed:
            result = torch.unsqueeze(result, dim=0)
            result_shape = result.shape
            if len(result_shape) == 2:
                result = result.contiguous().view(size=result_shape + tuple([1, 1]))
            elif len(result_shape) == 3:
                result = result.contiguous().view(size=result_shape + tuple([1]))
            #if len(result_shape) == 2:
            #   result = result.contiguous().view(size=result_shape + tuple([1]))
        else:
            result = torch.unsqueeze(torch.unsqueeze(result, dim=0), dim=1)
        return result

    dtype = type(data) ##TODO: do we need any additional shape checking?
    if dtype is torch.Tensor: # to tensor
        result = data.float()
    elif dtype is np.ndarray: # to tensor
        result = torch.tensor(data).float()
    elif dtype is pd.DataFrame:
        result = torch.tensor(data.values).float()
    elif dtype in [float, int] or dtype.__base__ in [np.floating, np.signedinteger]: # to tensor
        result = torch.tensor(data * np.ones(shape=(1, 1))).float()
    elif dtype in [list, set, tuple, dict, str]: # to discrete
        return data
    else:
        raise TypeError("Invalid input dtype {} - expected float, integer, np.ndarray, or torch var.".format(dtype))

    result = reformat_tensor(result)
    return result.to(device) 
Example 24
Project: spatial_access   Author: GeoDaCenter   File: p2p.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _get_type_of_series(series):
        """
        Returns: type of the series (int16, int32, int64, int128 or str)

        Raises:
            ImproperIndecesTypeException: If the series
            is not one of the expected types.
        """

        if type(series[0]) == str:
            return str
        elif issubdtype(series.dtype, integer) or issubdtype(series.dtype, signedinteger):
            return integer
        raise ImproperIndecesTypeException(str(series.dtype)) 
Example 25
Project: Splunking-Crime   Author: nccgroup   File: numeric.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def _assert_safe_casting(cls, data, subarr):
        """
        Ensure incoming data can be represented as ints.
        """
        if not issubclass(data.dtype.type, np.signedinteger):
            if not np.array_equal(data, subarr):
                raise TypeError('Unsafe NumPy casting, you must '
                                'explicitly cast') 
Example 26
Project: elasticintel   Author: securityclippy   File: numeric.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _assert_safe_casting(cls, data, subarr):
        """
        Ensure incoming data can be represented as ints.
        """
        if not issubclass(data.dtype.type, np.signedinteger):
            if not np.array_equal(data, subarr):
                raise TypeError('Unsafe NumPy casting, you must '
                                'explicitly cast') 
Example 27
Project: sklearn-onnx   Author: onnx   File: one_hot_encoder.py    License: MIT License 5 votes vote down vote up
def calculate_sklearn_one_hot_encoder_output_shapes(operator):
    op = operator.raw_operator
    categories_len = 0
    for index, categories in enumerate(op.categories_):
        if hasattr(op, 'drop_idx_') and op.drop_idx_ is not None:
            categories = (categories[np.arange(len(categories)) !=
                          op.drop_idx_[index]])
        categories_len += len(categories)
    instances = operator.inputs[0].type.shape[0]
    if np.issubdtype(op.dtype, np.signedinteger):
        operator.outputs[0].type = Int64TensorType([instances, categories_len])
    else:
        operator.outputs[0].type = FloatTensorType([instances, categories_len]) 
Example 28
Project: sklearn-onnx   Author: onnx   File: _parse.py    License: MIT License 5 votes vote down vote up
def _parse_sklearn_classifier(scope, model, inputs, custom_parsers=None):
    probability_tensor = _parse_sklearn_simple_model(
            scope, model, inputs, custom_parsers=custom_parsers)
    if model.__class__ in [NuSVC, SVC] and not model.probability:
        return probability_tensor
    options = scope.get_options(model, dict(zipmap=True))
    if not options['zipmap']:
        return probability_tensor
    this_operator = scope.declare_local_operator('SklearnZipMap')
    this_operator.inputs = probability_tensor
    label_type = Int64TensorType([None])
    classes = get_label_classes(scope, model)

    if (isinstance(model.classes_, list) and
            isinstance(model.classes_[0], np.ndarray)):
        # multi-label problem
        pass
    elif np.issubdtype(classes.dtype, np.floating):
        classes = np.array(list(map(lambda x: int(x), classes)))
        if set(map(lambda x: float(x), classes)) != set(model.classes_):
            raise RuntimeError("skl2onnx implicitly converts float class "
                               "labels into integers but at least one label "
                               "is not an integer. Class labels should "
                               "be integers or strings.")
        this_operator.classlabels_int64s = classes
    elif np.issubdtype(classes.dtype, np.signedinteger):
        this_operator.classlabels_int64s = classes
    elif np.issubdtype(classes.dtype, np.unsignedinteger):
        this_operator.classlabels_int64s = classes
    else:
        classes = np.array([s.encode('utf-8') for s in classes])
        this_operator.classlabels_strings = classes
        label_type = StringTensorType([None])

    output_label = scope.declare_local_variable('output_label', label_type)
    output_probability = scope.declare_local_variable(
        'output_probability',
        SequenceType(DictionaryType(label_type, scope.tensor_type())))
    this_operator.outputs.append(output_label)
    this_operator.outputs.append(output_probability)
    return this_operator.outputs 
Example 29
Project: sklearn-onnx   Author: onnx   File: utils_classifier.py    License: MIT License 5 votes vote down vote up
def _finalize_converter_classes(scope, argmax_output_name, output_full_name,
                                container, classes):
    """
    See :func:`convert_voting_classifier`.
    """
    if np.issubdtype(classes.dtype, np.floating):
        class_type = onnx_proto.TensorProto.INT32
        classes = np.array(list(map(lambda x: int(x), classes)))
    elif np.issubdtype(classes.dtype, np.signedinteger):
        class_type = onnx_proto.TensorProto.INT32
    else:
        classes = np.array([s.encode('utf-8') for s in classes])
        class_type = onnx_proto.TensorProto.STRING

    classes_name = scope.get_unique_variable_name('classes')
    container.add_initializer(classes_name, class_type, classes.shape, classes)

    array_feature_extractor_result_name = scope.get_unique_variable_name(
                                            'array_feature_extractor_result')
    container.add_node(
        'ArrayFeatureExtractor', [classes_name, argmax_output_name],
        array_feature_extractor_result_name, op_domain='ai.onnx.ml',
        name=scope.get_unique_operator_name('ArrayFeatureExtractor'))

    output_shape = (-1,)
    if class_type == onnx_proto.TensorProto.INT32:
        cast2_result_name = scope.get_unique_variable_name('cast2_result')
        reshaped_result_name = scope.get_unique_variable_name(
                                                'reshaped_result')
        apply_cast(scope, array_feature_extractor_result_name,
                   cast2_result_name, container,
                   to=onnx_proto.TensorProto.FLOAT)
        apply_reshape(scope, cast2_result_name, reshaped_result_name,
                      container, desired_shape=output_shape)
        apply_cast(scope, reshaped_result_name, output_full_name, container,
                   to=onnx_proto.TensorProto.INT64)
    else:  # string labels
        apply_reshape(scope, array_feature_extractor_result_name,
                      output_full_name, container, desired_shape=output_shape) 
Example 30
Project: coffeegrindsize   Author: jgagneastro   File: test_abc.py    License: MIT License 5 votes vote down vote up
def test_abstract(self):
        assert_(issubclass(np.number, numbers.Number))

        assert_(issubclass(np.inexact, numbers.Complex))
        assert_(issubclass(np.complexfloating, numbers.Complex))
        assert_(issubclass(np.floating, numbers.Real))

        assert_(issubclass(np.integer, numbers.Integral))
        assert_(issubclass(np.signedinteger, numbers.Integral))
        assert_(issubclass(np.unsignedinteger, numbers.Integral))