本文整理汇总了Python中numpy.core.ndarray方法的典型用法代码示例。如果您正苦于以下问题:Python core.ndarray方法的具体用法?Python core.ndarray怎么用?Python core.ndarray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.core
的用法示例。
在下文中一共展示了core.ndarray方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_fix_with_subclass
# 需要导入模块: from numpy import core [as 别名]
# 或者: from numpy.core import ndarray [as 别名]
def test_fix_with_subclass(self):
class MyArray(nx.ndarray):
def __new__(cls, data, metadata=None):
res = nx.array(data, copy=True).view(cls)
res.metadata = metadata
return res
def __array_wrap__(self, obj, context=None):
obj.metadata = self.metadata
return obj
a = nx.array([1.1, -1.1])
m = MyArray(a, metadata='foo')
f = ufl.fix(m)
assert_array_equal(f, nx.array([1, -1]))
assert_(isinstance(f, MyArray))
assert_equal(f.metadata, 'foo')
示例2: test_fix_with_subclass
# 需要导入模块: from numpy import core [as 别名]
# 或者: from numpy.core import ndarray [as 别名]
def test_fix_with_subclass(self):
class MyArray(nx.ndarray):
def __new__(cls, data, metadata=None):
res = nx.array(data, copy=True).view(cls)
res.metadata = metadata
return res
def __array_wrap__(self, obj, context=None):
obj.metadata = self.metadata
return obj
a = nx.array([1.1, -1.1])
m = MyArray(a, metadata='foo')
f = ufl.fix(m)
assert_array_equal(f, nx.array([1, -1]))
assert_(isinstance(f, MyArray))
assert_equal(f.metadata, 'foo')
# check 0d arrays don't decay to scalars
m0d = m[0,...]
m0d.metadata = 'bar'
f0d = ufl.fix(m0d)
assert_(isinstance(f0d, MyArray))
assert_equal(f0d.metadata, 'bar')
示例3: test_fix_with_subclass
# 需要导入模块: from numpy import core [as 别名]
# 或者: from numpy.core import ndarray [as 别名]
def test_fix_with_subclass(self):
class MyArray(nx.ndarray):
def __new__(cls, data, metadata=None):
res = nx.array(data, copy=True).view(cls)
res.metadata = metadata
return res
def __array_wrap__(self, obj, context=None):
obj.metadata = self.metadata
return obj
a = nx.array([1.1, -1.1])
m = MyArray(a, metadata='foo')
f = ufl.fix(m)
assert_array_equal(f, nx.array([1, -1]))
assert_(isinstance(f, MyArray))
assert_equal(f.metadata, 'foo')
示例4: test_fix_with_subclass
# 需要导入模块: from numpy import core [as 别名]
# 或者: from numpy.core import ndarray [as 别名]
def test_fix_with_subclass(self):
class MyArray(nx.ndarray):
def __new__(cls, data, metadata=None):
res = nx.array(data, copy=True).view(cls)
res.metadata = metadata
return res
def __array_wrap__(self, obj, context=None):
if isinstance(obj, MyArray):
obj.metadata = self.metadata
return obj
def __array_finalize__(self, obj):
self.metadata = getattr(obj, 'metadata', None)
return self
a = nx.array([1.1, -1.1])
m = MyArray(a, metadata='foo')
f = ufl.fix(m)
assert_array_equal(f, nx.array([1, -1]))
assert_(isinstance(f, MyArray))
assert_equal(f.metadata, 'foo')
# check 0d arrays don't decay to scalars
m0d = m[0,...]
m0d.metadata = 'bar'
f0d = ufl.fix(m0d)
assert_(isinstance(f0d, MyArray))
assert_equal(f0d.metadata, 'bar')
示例5: build_err_msg
# 需要导入模块: from numpy import core [as 别名]
# 或者: from numpy.core import ndarray [as 别名]
def build_err_msg(arrays, err_msg, header='Items are not equal:',
verbose=True, names=('ACTUAL', 'DESIRED'), precision=8):
msg = ['\n' + header]
if err_msg:
if err_msg.find('\n') == -1 and len(err_msg) < 79-len(header):
msg = [msg[0] + ' ' + err_msg]
else:
msg.append(err_msg)
if verbose:
for i, a in enumerate(arrays):
if isinstance(a, ndarray):
# precision argument is only needed if the objects are ndarrays
r_func = partial(array_repr, precision=precision)
else:
r_func = repr
try:
r = r_func(a)
except Exception as exc:
r = '[repr failed for <{}>: {}]'.format(type(a).__name__, exc)
if r.count('\n') > 3:
r = '\n'.join(r.splitlines()[:3])
r += '...'
msg.append(' %s: %s' % (names[i], r))
return '\n'.join(msg)
示例6: build_err_msg
# 需要导入模块: from numpy import core [as 别名]
# 或者: from numpy.core import ndarray [as 别名]
def build_err_msg(arrays, err_msg, header='Items are not equal:',
verbose=True, names=('ACTUAL', 'DESIRED'), precision=8):
msg = ['\n' + header]
if err_msg:
if err_msg.find('\n') == -1 and len(err_msg) < 79-len(header):
msg = [msg[0] + ' ' + err_msg]
else:
msg.append(err_msg)
if verbose:
for i, a in enumerate(arrays):
if isinstance(a, ndarray):
# precision argument is only needed if the objects are ndarrays
r_func = partial(array_repr, precision=precision)
else:
r_func = repr
try:
r = r_func(a)
except:
r = '[repr failed]'
if r.count('\n') > 3:
r = '\n'.join(r.splitlines()[:3])
r += '...'
msg.append(' %s: %s' % (names[i], r))
return '\n'.join(msg)
示例7: test_fix_with_subclass
# 需要导入模块: from numpy import core [as 别名]
# 或者: from numpy.core import ndarray [as 别名]
def test_fix_with_subclass(self):
class MyArray(nx.ndarray):
def __new__(cls, data, metadata=None):
res = nx.array(data, copy=True).view(cls)
res.metadata = metadata
return res
def __array_wrap__(self, obj, context=None):
obj.metadata = self.metadata
return obj
def __array_finalize__(self, obj):
self.metadata = getattr(obj, 'metadata', None)
return self
a = nx.array([1.1, -1.1])
m = MyArray(a, metadata='foo')
f = ufl.fix(m)
assert_array_equal(f, nx.array([1, -1]))
assert_(isinstance(f, MyArray))
assert_equal(f.metadata, 'foo')
# check 0d arrays don't decay to scalars
m0d = m[0,...]
m0d.metadata = 'bar'
f0d = ufl.fix(m0d)
assert_(isinstance(f0d, MyArray))
assert_equal(f0d.metadata, 'bar')
示例8: byte_bounds
# 需要导入模块: from numpy import core [as 别名]
# 或者: from numpy.core import ndarray [as 别名]
def byte_bounds(a):
"""
Returns pointers to the end-points of an array.
Parameters
----------
a : ndarray
Input array. It must conform to the Python-side of the array
interface.
Returns
-------
(low, high) : tuple of 2 integers
The first integer is the first byte of the array, the second
integer is just past the last byte of the array. If `a` is not
contiguous it will not use every byte between the (`low`, `high`)
values.
Examples
--------
>>> I = np.eye(2, dtype='f'); I.dtype
dtype('float32')
>>> low, high = np.byte_bounds(I)
>>> high - low == I.size*I.itemsize
True
>>> I = np.eye(2, dtype='G'); I.dtype
dtype('complex192')
>>> low, high = np.byte_bounds(I)
>>> high - low == I.size*I.itemsize
True
"""
ai = a.__array_interface__
a_data = ai['data'][0]
astrides = ai['strides']
ashape = ai['shape']
bytes_a = asarray(a).dtype.itemsize
a_low = a_high = a_data
if astrides is None:
# contiguous case
a_high += a.size * bytes_a
else:
for shape, stride in zip(ashape, astrides):
if stride < 0:
a_low += (shape-1)*stride
else:
a_high += (shape-1)*stride
a_high += bytes_a
return a_low, a_high
#-----------------------------------------------------------------------------
# Function for output and information on the variables used.
#-----------------------------------------------------------------------------
示例9: _info
# 需要导入模块: from numpy import core [as 别名]
# 或者: from numpy.core import ndarray [as 别名]
def _info(obj, output=sys.stdout):
"""Provide information about ndarray obj.
Parameters
----------
obj : ndarray
Must be ndarray, not checked.
output
Where printed output goes.
Notes
-----
Copied over from the numarray module prior to its removal.
Adapted somewhat as only numpy is an option now.
Called by info.
"""
extra = ""
tic = ""
bp = lambda x: x
cls = getattr(obj, '__class__', type(obj))
nm = getattr(cls, '__name__', cls)
strides = obj.strides
endian = obj.dtype.byteorder
print("class: ", nm, file=output)
print("shape: ", obj.shape, file=output)
print("strides: ", strides, file=output)
print("itemsize: ", obj.itemsize, file=output)
print("aligned: ", bp(obj.flags.aligned), file=output)
print("contiguous: ", bp(obj.flags.contiguous), file=output)
print("fortran: ", obj.flags.fortran, file=output)
print(
"data pointer: %s%s" % (hex(obj.ctypes._as_parameter_.value), extra),
file=output
)
print("byteorder: ", end=' ', file=output)
if endian in ['|', '=']:
print("%s%s%s" % (tic, sys.byteorder, tic), file=output)
byteswap = False
elif endian == '>':
print("%sbig%s" % (tic, tic), file=output)
byteswap = sys.byteorder != "big"
else:
print("%slittle%s" % (tic, tic), file=output)
byteswap = sys.byteorder != "little"
print("byteswap: ", bp(byteswap), file=output)
print("type: %s" % obj.dtype, file=output)