本文整理匯總了Python中numpy.ScalarType方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.ScalarType方法的具體用法?Python numpy.ScalarType怎麽用?Python numpy.ScalarType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.ScalarType方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_truediv
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ScalarType [as 別名]
def test_truediv(Poly):
# true division is valid only if the denominator is a Number and
# not a python bool.
p1 = Poly([1,2,3])
p2 = p1 * 5
for stype in np.ScalarType:
if not issubclass(stype, Number) or issubclass(stype, bool):
continue
s = stype(5)
assert_poly_almost_equal(op.truediv(p2, s), p1)
assert_raises(TypeError, op.truediv, s, p2)
for stype in (int, long, float):
s = stype(5)
assert_poly_almost_equal(op.truediv(p2, s), p1)
assert_raises(TypeError, op.truediv, s, p2)
for stype in [complex]:
s = stype(5, 0)
assert_poly_almost_equal(op.truediv(p2, s), p1)
assert_raises(TypeError, op.truediv, s, p2)
for s in [tuple(), list(), dict(), bool(), np.array([1])]:
assert_raises(TypeError, op.truediv, p2, s)
assert_raises(TypeError, op.truediv, s, p2)
for ptype in classes:
assert_raises(TypeError, op.truediv, p2, ptype(1))
示例2: check_truediv
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ScalarType [as 別名]
def check_truediv(Poly):
# true division is valid only if the denominator is a Number and
# not a python bool.
p1 = Poly([1,2,3])
p2 = p1 * 5
for stype in np.ScalarType:
if not issubclass(stype, Number) or issubclass(stype, bool):
continue
s = stype(5)
assert_poly_almost_equal(op.truediv(p2, s), p1)
assert_raises(TypeError, op.truediv, s, p2)
for stype in (int, long, float):
s = stype(5)
assert_poly_almost_equal(op.truediv(p2, s), p1)
assert_raises(TypeError, op.truediv, s, p2)
for stype in [complex]:
s = stype(5, 0)
assert_poly_almost_equal(op.truediv(p2, s), p1)
assert_raises(TypeError, op.truediv, s, p2)
for s in [tuple(), list(), dict(), bool(), np.array([1])]:
assert_raises(TypeError, op.truediv, p2, s)
assert_raises(TypeError, op.truediv, s, p2)
for ptype in classes:
assert_raises(TypeError, op.truediv, p2, ptype(1))
示例3: _record_tabular
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ScalarType [as 別名]
def _record_tabular(self, data, step):
if self._x_axis:
nonexist_axes = []
for axis in [self._x_axis] + self._additional_x_axes:
if axis not in data.as_dict:
nonexist_axes.append(axis)
if nonexist_axes:
self._warn('{} {} exist in the tabular data.'.format(
', '.join(nonexist_axes),
'do not' if len(nonexist_axes) > 1 else 'does not'))
for key, value in data.as_dict.items():
if isinstance(value,
np.ScalarType) and self._x_axis in data.as_dict:
if self._x_axis is not key:
x = data.as_dict[self._x_axis]
self._record_kv(key, value, x)
for axis in self._additional_x_axes:
if key is not axis and key in data.as_dict:
x = data.as_dict[axis]
self._record_kv('{}/{}'.format(key, axis), value, x)
else:
self._record_kv(key, value, step)
data.mark(key)
示例4: test_rvs
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ScalarType [as 別名]
def test_rvs(self):
vals = stats.randint.rvs(5,30,size=100)
assert_(numpy.all(vals < 30) & numpy.all(vals >= 5))
assert_(len(vals) == 100)
vals = stats.randint.rvs(5,30,size=(2,50))
assert_(numpy.shape(vals) == (2,50))
assert_(vals.dtype.char in typecodes['AllInteger'])
val = stats.randint.rvs(15,46)
assert_((val >= 15) & (val < 46))
assert_(isinstance(val, numpy.ScalarType), msg=repr(type(val)))
val = stats.randint(15,46).rvs(3)
assert_(val.dtype.char in typecodes['AllInteger'])
示例5: test_rvs
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ScalarType [as 別名]
def test_rvs(self):
vals = stats.randint.rvs(5, 30, size=100)
assert_(numpy.all(vals < 30) & numpy.all(vals >= 5))
assert_(len(vals) == 100)
vals = stats.randint.rvs(5, 30, size=(2, 50))
assert_(numpy.shape(vals) == (2, 50))
assert_(vals.dtype.char in typecodes['AllInteger'])
val = stats.randint.rvs(15, 46)
assert_((val >= 15) & (val < 46))
assert_(isinstance(val, numpy.ScalarType), msg=repr(type(val)))
val = stats.randint(15, 46).rvs(3)
assert_(val.dtype.char in typecodes['AllInteger'])
示例6: numpy_fallback_array_equal
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ScalarType [as 別名]
def numpy_fallback_array_equal(name='xp'):
"""
Decorator that checks fallback_mode results are equal to NumPy ones.
Checks ndarrays.
Args:
name(str): Argument name whose value is either
``numpy`` or ``cupy`` module.
"""
def decorator(impl):
@functools.wraps(impl)
def test_func(self, *args, **kwargs):
kwargs[name] = fallback_mode.numpy
fallback_result = impl(self, *args, **kwargs)
kwargs[name] = numpy
numpy_result = impl(self, *args, **kwargs)
if isinstance(numpy_result, numpy.ndarray):
# if numpy returns ndarray, cupy must return ndarray
assert isinstance(fallback_result, fallback.ndarray)
fallback_mode.numpy.testing.assert_array_equal(
numpy_result, fallback_result)
assert fallback_result.dtype == numpy_result.dtype
elif isinstance(numpy_result, numpy.ScalarType):
# if numpy returns scalar
# cupy may return 0-dim array
assert numpy_result == fallback_result._cupy_array.item() or \
(numpy_result == fallback_result._numpy_array).all()
else:
assert False
return test_func
return decorator
示例7: _record_kv
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ScalarType [as 別名]
def _record_kv(self, key, value, step):
if isinstance(value, np.ScalarType):
self._writer.add_scalar(key, value, step)
elif isinstance(value, plt.Figure):
self._writer.add_figure(key, value, step)
elif isinstance(value, scipy.stats._distn_infrastructure.rv_frozen):
shape = (self._histogram_samples,) + value.mean().shape
self._writer.add_histogram(key, value.rvs(shape), step)
elif isinstance(value, scipy.stats._multivariate.multi_rv_frozen):
self._writer.add_histogram(key, value.rvs(self._histogram_samples), step)
elif isinstance(value, Histogram):
self._writer.add_histogram(key, value, step)
示例8: _record_kv
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ScalarType [as 別名]
def _record_kv(self, key, value, step):
if isinstance(value, np.ScalarType):
self._writer.add_scalar(key, value, step)
elif isinstance(value, plt.Figure):
self._writer.add_figure(key, value, step)
elif isinstance(value, scipy.stats._distn_infrastructure.rv_frozen):
shape = (self._histogram_samples, ) + value.mean().shape
self._writer.add_histogram(key, value.rvs(shape), step)
elif isinstance(value, scipy.stats._multivariate.multi_rv_frozen):
self._writer.add_histogram(key, value.rvs(self._histogram_samples),
step)
elif isinstance(value, Histogram):
self._writer.add_histogram(key, value, step)
示例9: __call__
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ScalarType [as 別名]
def __call__(self, lhs, rhs):
if lhs is rhs:
return 2*lhs
if isinstance(lhs, np.ScalarType) and lhs == 0:
return rhs
if isinstance(rhs, np.ScalarType) and rhs == 0:
return lhs
return super(Add, self).__call__(lhs, rhs)
示例10: __getitem__
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ScalarType [as 別名]
def __getitem__(self, key):
if isinstance(key, str):
raise MAError("Unavailable for masked array.")
if not isinstance(key, tuple):
key = (key,)
objs = []
scalars = []
final_dtypedescr = None
for k in range(len(key)):
scalar = False
if isinstance(key[k], slice):
step = key[k].step
start = key[k].start
stop = key[k].stop
if start is None:
start = 0
if step is None:
step = 1
if isinstance(step, complex):
size = int(abs(step))
newobj = np.linspace(start, stop, num=size)
else:
newobj = np.arange(start, stop, step)
elif isinstance(key[k], str):
if (key[k] in 'rc'):
self.matrix = True
self.col = (key[k] == 'c')
continue
try:
self.axis = int(key[k])
continue
except (ValueError, TypeError):
raise ValueError("Unknown special directive")
elif type(key[k]) in np.ScalarType:
newobj = asarray([key[k]])
scalars.append(k)
scalar = True
else:
newobj = key[k]
objs.append(newobj)
if isinstance(newobj, ndarray) and not scalar:
if final_dtypedescr is None:
final_dtypedescr = newobj.dtype
elif newobj.dtype > final_dtypedescr:
final_dtypedescr = newobj.dtype
if final_dtypedescr is not None:
for k in scalars:
objs[k] = objs[k].astype(final_dtypedescr)
res = concatenate(tuple(objs), axis=self.axis)
return self._retval(res)
示例11: __getitem__
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import ScalarType [as 別名]
def __getitem__(self, key):
trans1d = self.trans1d
ndmin = self.ndmin
objs = []
scalars = []
arraytypes = []
scalartypes = []
if isinstance(key, str):
raise NotImplementedError
if not isinstance(key, tuple):
key = (key,)
for i, k in enumerate(key):
scalar = False
if isinstance(k, slice):
raise NotImplementedError
elif isinstance(k, str):
if i != 0:
raise ValueError(
'special directives must be the first entry.')
raise NotImplementedError
elif type(k) in numpy.ScalarType:
newobj = from_data.array(k, ndmin=ndmin)
scalars.append(i)
scalar = True
scalartypes.append(newobj.dtype)
else:
newobj = from_data.array(k, copy=False, ndmin=ndmin)
if ndmin > 1:
ndim = from_data.array(k, copy=False).ndim
if trans1d != -1 and ndim < ndmin:
newobj = self._output_obj(newobj, ndim, ndmin, trans1d)
objs.append(newobj)
if not scalar and isinstance(newobj, core.ndarray):
arraytypes.append(newobj.dtype)
final_dtype = numpy.find_common_type(arraytypes, scalartypes)
if final_dtype is not None:
for k in scalars:
objs[k] = objs[k].astype(final_dtype)
return join.concatenate(tuple(objs), axis=self.axis)