本文整理汇总了Python中numpy.character方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.character方法的具体用法?Python numpy.character怎么用?Python numpy.character使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.character方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _check_string_truncate
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def _check_string_truncate(self, value):
"""
Emit a warning if any elements of ``value`` will be truncated when
``value`` is assigned to self.
"""
# Convert input ``value`` to the string dtype of this column and
# find the length of the longest string in the array.
value = np.asanyarray(value, dtype=self.dtype.type)
if value.size == 0:
return
value_str_len = np.char.str_len(value).max()
# Parse the array-protocol typestring (e.g. '|U15') of self.dtype which
# has the character repeat count on the right side.
self_str_len = dtype_bytes_or_chars(self.dtype)
if value_str_len > self_str_len:
warnings.warn('truncated right side string(s) longer than {} '
'character(s) during assignment'
.format(self_str_len),
StringTruncateWarning,
stacklevel=3)
示例2: _bytesize
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def _bytesize(tc):
DTYPE_MAP = {'d': np.float64, 'f': np.float32, 'l': np.long, 'i': np.int32,
'h': np.int16, 'b': np.int8, 'S1': np.character}
return np.dtype(DTYPE_MAP.get(tc, np.float)).itemsize
#==============================================================================
# Private Size from Shape Calculator
#==============================================================================
示例3: __getitem__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def __getitem__(self, obj):
val = numpy.ndarray.__getitem__(self, obj)
if isinstance(val, numpy.character):
temp = val.rstrip()
if numpy.char._len(temp) == 0:
val = ''
else:
val = temp
return val
示例4: normalize_attr_values
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def normalize_attr_values(a: Any, use_object_strings: bool = False) -> np.ndarray:
"""
Take all kinds of input values and validate/normalize them.
Args:
a List, tuple, np.matrix, np.ndarray or sparse matrix
Elements can be strings, numbers or bools
Returns
a_normalized An np.ndarray with elements conforming to one of the valid Loom attribute types
Remarks:
This method should be used to prepare the values to be stored in the HDF5 file. You should not
return the values to the caller; for that, use materialize_attr_values()
"""
scalar = False
if np.isscalar(a):
a = np.array([a])
scalar = True
arr = normalize_attr_array(a)
if np.issubdtype(arr.dtype, np.integer) or np.issubdtype(arr.dtype, np.floating):
pass # We allow all these types
elif np.issubdtype(arr.dtype, np.character) or np.issubdtype(arr.dtype, np.object_):
if use_object_strings:
arr = np.array([str(elm) for elm in a], dtype=object)
else:
arr = normalize_attr_strings(arr)
elif np.issubdtype(arr.dtype, np.bool_):
arr = arr.astype('ubyte')
if scalar:
return arr[0]
else:
return arr
示例5: check
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def check(self, x):
"""Record any information needed by transform."""
if not isinstance(x, (np.ndarray, tf.data.Dataset)):
raise TypeError('Expect the data to TextInput to be numpy.ndarray or '
'tf.data.Dataset, but got {type}.'.format(type=type(x)))
if isinstance(x, np.ndarray) and x.ndim != 1:
raise ValueError('Expect the data to TextInput to have 1 dimension, but '
'got input shape {shape} with {ndim} dimensions'.format(
shape=x.shape,
ndim=x.ndim))
if isinstance(x, np.ndarray) and not np.issubdtype(x.dtype, np.character):
raise TypeError('Expect the data to TextInput to be strings, but got '
'{type}.'.format(type=x.dtype))
示例6: common_dtype
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def common_dtype(cols):
"""
Use numpy to find the common dtype for a list of structured ndarray columns.
Only allow columns within the following fundamental numpy data types:
np.bool_, np.object_, np.number, np.character, np.void
"""
np_types = (np.bool_, np.object_, np.number, np.character, np.void)
uniq_types = set(tuple(issubclass(col.dtype.type, np_type) for np_type in np_types)
for col in cols)
if len(uniq_types) > 1:
# Embed into the exception the actual list of incompatible types.
incompat_types = [col.dtype.name for col in cols]
tme = TableMergeError('Columns have incompatible types {}'
.format(incompat_types))
tme._incompat_types = incompat_types
raise tme
arrs = [np.empty(1, dtype=col.dtype) for col in cols]
# For string-type arrays need to explicitly fill in non-zero
# values or the final arr_common = .. step is unpredictable.
for arr in arrs:
if arr.dtype.kind in ('S', 'U'):
arr[0] = '0' * arr.itemsize
arr_common = np.array([arr[0] for arr in arrs])
return arr_common.dtype.str
示例7: _expand_string_array_for_values
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def _expand_string_array_for_values(arr, values):
"""
For string-dtype return a version of ``arr`` that is wide enough for ``values``.
If ``arr`` is not string-dtype or does not need expansion then return ``arr``.
Parameters
----------
arr : np.ndarray
Input array
values : scalar or array_like
Values for width comparison for string arrays
Returns
-------
arr_expanded : np.ndarray
"""
if arr.dtype.kind in ('U', 'S') and values is not np.ma.masked:
# Find the length of the longest string in the new values.
values_str_len = np.char.str_len(values).max()
# Determine character repeat count of arr.dtype. Returns a positive
# int or None (something like 'U0' is not possible in numpy). If new values
# are longer than current then make a new (wider) version of arr.
arr_str_len = dtype_bytes_or_chars(arr.dtype)
if arr_str_len and values_str_len > arr_str_len:
arr_dtype = arr.dtype.byteorder + arr.dtype.kind + str(values_str_len)
arr = arr.astype(arr_dtype)
return arr
示例8: __setitem__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def __setitem__(self, index, value):
if self.dtype.char == 'S':
value = self._encode_str(value)
# Issue warning for string assignment that truncates ``value``
if issubclass(self.dtype.type, np.character):
self._check_string_truncate(value)
# update indices
self.info.adjust_indices(index, value, len(self))
# Set items using a view of the underlying data, as it gives an
# order-of-magnitude speed-up. [#2994]
self.data[index] = value
示例9: common_dtype
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def common_dtype(cols):
"""
Use numpy to find the common dtype for a list of columns.
Only allow columns within the following fundamental numpy data types:
np.bool_, np.object_, np.number, np.character, np.void
"""
try:
return metadata.common_dtype(cols)
except metadata.MergeConflictError as err:
tme = TableMergeError('Columns have incompatible types {}'
.format(err._incompat_types))
tme._incompat_types = err._incompat_types
raise tme
示例10: count_nonzero
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def count_nonzero(array, epsilon=1.0, accountant=None, axis=None, keepdims=False):
r"""Counts the number of non-zero values in the array ``array`` with differential privacy.
The word "non-zero" is in reference to the Python 2.x built-in method ``__nonzero__()`` (renamed ``__bool__()`` in
Python 3.x) of Python objects that tests an object's "truthfulness". For example, any number is considered truthful
if it is nonzero, whereas any string is considered truthful if it is not the empty string. Thus, this function
(recursively) counts how many elements in ``array`` (and in sub-arrays thereof) have their ``__nonzero__()`` or
``__bool__()`` method evaluated to ``True``.
Parameters
----------
array : array_like
The array for which to count non-zeros.
epsilon : float, default: 1.0
Privacy parameter :math:`\epsilon`.
accountant : BudgetAccountant, optional
Accountant to keep track of privacy budget.
axis : int or tuple, optional
Axis or tuple of axes along which to count non-zeros. Default is None, meaning that non-zeros will be counted
along a flattened version of ``array``.
keepdims : bool, optional
If this is set to True, the axes that are counted are left in the result as dimensions with size one. With this
option, the result will broadcast correctly against the input array.
Returns
-------
count : int or array of int
Differentially private number of non-zero values in the array along a given axis. Otherwise, the total number
of non-zero values in the array is returned.
"""
array = np.asanyarray(array)
if np.issubdtype(array.dtype, np.character):
array_bool = array != array.dtype.type()
else:
array_bool = array.astype(np.bool_, copy=False)
return sum(array_bool, axis=axis, dtype=np.intp, bounds=(0, 1), epsilon=epsilon, accountant=accountant,
keepdims=keepdims)
示例11: count_nonzero
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def count_nonzero(a, axis=None):
"""
Counts the number of non-zero values in the array ``a``.
The word "non-zero" is in reference to the Python 2.x
built-in method ``__nonzero__()`` (renamed ``__bool__()``
in Python 3.x) of Python objects that tests an object's
"truthfulness". For example, any number is considered
truthful if it is nonzero, whereas any string is considered
truthful if it is not the empty string. Thus, this function
(recursively) counts how many elements in ``a`` (and in
sub-arrays thereof) have their ``__nonzero__()`` or ``__bool__()``
method evaluated to ``True``.
Parameters
----------
a : array_like
The array for which to count non-zeros.
axis : int or tuple, optional
Axis or tuple of axes along which to count non-zeros.
Default is None, meaning that non-zeros will be counted
along a flattened version of ``a``.
.. versionadded:: 1.12.0
Returns
-------
count : int or array of int
Number of non-zero values in the array along a given axis.
Otherwise, the total number of non-zero values in the array
is returned.
See Also
--------
nonzero : Return the coordinates of all the non-zero values.
Examples
--------
>>> np.count_nonzero(np.eye(4))
4
>>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]])
5
>>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=0)
array([1, 1, 1, 1, 1])
>>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=1)
array([2, 3])
"""
if axis is None:
return multiarray.count_nonzero(a)
a = asanyarray(a)
# TODO: this works around .astype(bool) not working properly (gh-9847)
if np.issubdtype(a.dtype, np.character):
a_bool = a != a.dtype.type()
else:
a_bool = a.astype(np.bool_, copy=False)
return a_bool.sum(axis=axis, dtype=np.intp)
示例12: equivalent
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def equivalent(x, y):
"""
Checks the equivalence of two scalars or arrays with broadcasting. Assumes
a consistent dtype.
Parameters
----------
x : scalar or numpy.ndarray
y : scalar or numpy.ndarray
Returns
-------
equivalent : scalar or numpy.ndarray
The element-wise comparison of where two arrays are equivalent.
Examples
--------
>>> equivalent(1, 1)
True
>>> equivalent(np.nan, np.nan + 1)
True
>>> equivalent(1, 2)
False
>>> equivalent(np.inf, np.inf)
True
>>> equivalent(np.PZERO, np.NZERO)
True
"""
x = np.asarray(x)
y = np.asarray(y)
# Can't contain NaNs
if any(np.issubdtype(x.dtype, t) for t in [np.integer, np.bool_, np.character]):
return x == y
# Can contain NaNs
# FIXME: Complex floats and np.void with multiple values can't be compared properly.
# lgtm [py/comparison-of-identical-expressions]
return (x == y) | ((x != x) & (y != y))
# copied from zarr
# See https://github.com/zarr-developers/zarr-python/blob/master/zarr/util.py
示例13: common_dtype
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import character [as 别名]
def common_dtype(arrs):
"""
Use numpy to find the common dtype for a list of ndarrays.
Only allow arrays within the following fundamental numpy data types:
``np.bool_``, ``np.object_``, ``np.number``, ``np.character``, ``np.void``
Parameters
----------
arrs : list of ndarray objects
Arrays for which to find the common dtype
Returns
-------
dtype_str : str
String representation of dytpe (dtype ``str`` attribute)
"""
def dtype(arr):
return getattr(arr, 'dtype', np.dtype('O'))
np_types = (np.bool_, np.object_, np.number, np.character, np.void)
uniq_types = set(tuple(issubclass(dtype(arr).type, np_type) for np_type in np_types)
for arr in arrs)
if len(uniq_types) > 1:
# Embed into the exception the actual list of incompatible types.
incompat_types = [dtype(arr).name for arr in arrs]
tme = MergeConflictError('Arrays have incompatible types {}'
.format(incompat_types))
tme._incompat_types = incompat_types
raise tme
arrs = [np.empty(1, dtype=dtype(arr)) for arr in arrs]
# For string-type arrays need to explicitly fill in non-zero
# values or the final arr_common = .. step is unpredictable.
for i, arr in enumerate(arrs):
if arr.dtype.kind in ('S', 'U'):
arrs[i] = [('0' if arr.dtype.kind == 'U' else b'0') *
dtype_bytes_or_chars(arr.dtype)]
arr_common = np.array([arr[0] for arr in arrs])
return arr_common.dtype.str