本文整理汇总了Python中numpy.ma.array方法的典型用法代码示例。如果您正苦于以下问题:Python ma.array方法的具体用法?Python ma.array怎么用?Python ma.array使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.ma
的用法示例。
在下文中一共展示了ma.array方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: eval_batch
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def eval_batch(table_batch, label_batch, mask_batch):
# reshap (table_batch * table_size * features)
for f_g in table_batch:
table_batch[f_g] = table_batch[f_g].view(batch_size * MAX_COL_COUNT, -1)
emissions = classifier(table_batch).view(batch_size, MAX_COL_COUNT, -1)
pred = model.decode(emissions, mask_batch)
pred = np.concatenate(pred)
labels = label_batch.view(-1).cpu().numpy()
masks = mask_batch.view(-1).cpu().numpy()
invert_masks = np.invert(masks==1)
return pred, ma.array(labels, mask=invert_masks).compressed()
# randomly shuffle the orders of columns in a table batch
示例2: _keep_fields
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def _keep_fields(base, keep_names, usemask=True, asrecarray=False):
"""
Return a new array keeping only the fields in `keep_names`,
and preserving the order of those fields.
Parameters
----------
base : array
Input array
keep_names : string or sequence
String or sequence of strings corresponding to the names of the
fields to keep. Order of the names will be preserved.
usemask : {False, True}, optional
Whether to return a masked array or not.
asrecarray : string or sequence, optional
Whether to return a recarray or a mrecarray (`asrecarray=True`) or
a plain ndarray or masked array with flexible dtype. The default
is False.
"""
newdtype = [(n, base.dtype[n]) for n in keep_names]
output = np.empty(base.shape, dtype=newdtype)
output = recursive_fill_fields(base, output)
return _fix_output(output, usemask=usemask, asrecarray=asrecarray)
示例3: test_multiple_axes
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def test_multiple_axes(self):
a = np.array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
assert_equal(np.flip(a, axis=()), a)
b = np.array([[[5, 4],
[7, 6]],
[[1, 0],
[3, 2]]])
assert_equal(np.flip(a, axis=(0, 2)), b)
c = np.array([[[3, 2],
[1, 0]],
[[7, 6],
[5, 4]]])
assert_equal(np.flip(a, axis=(1, 2)), c)
示例4: test_order
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def test_order(self):
# It turns out that people rely on np.copy() preserving order by
# default; changing this broke scikit-learn:
# github.com/scikit-learn/scikit-learn/commit/7842748cf777412c506a8c0ed28090711d3a3783 # noqa
a = np.array([[1, 2], [3, 4]])
assert_(a.flags.c_contiguous)
assert_(not a.flags.f_contiguous)
a_fort = np.array([[1, 2], [3, 4]], order="F")
assert_(not a_fort.flags.c_contiguous)
assert_(a_fort.flags.f_contiguous)
a_copy = np.copy(a)
assert_(a_copy.flags.c_contiguous)
assert_(not a_copy.flags.f_contiguous)
a_fort_copy = np.copy(a_fort)
assert_(not a_fort_copy.flags.c_contiguous)
assert_(a_fort_copy.flags.f_contiguous)
示例5: test_returned
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def test_returned(self):
y = np.array([[1, 2, 3], [4, 5, 6]])
# No weights
avg, scl = average(y, returned=True)
assert_equal(scl, 6.)
avg, scl = average(y, 0, returned=True)
assert_array_equal(scl, np.array([2., 2., 2.]))
avg, scl = average(y, 1, returned=True)
assert_array_equal(scl, np.array([3., 3.]))
# With weights
w0 = [1, 2]
avg, scl = average(y, weights=w0, axis=0, returned=True)
assert_array_equal(scl, np.array([3., 3., 3.]))
w1 = [1, 2, 3]
avg, scl = average(y, weights=w1, axis=1, returned=True)
assert_array_equal(scl, np.array([6., 6.]))
w2 = [[0, 0, 1], [1, 2, 3]]
avg, scl = average(y, weights=w2, axis=1, returned=True)
assert_array_equal(scl, np.array([1., 6.]))
示例6: test_basic
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def test_basic(self):
a = [1, 2, 3]
assert_equal(insert(a, 0, 1), [1, 1, 2, 3])
assert_equal(insert(a, 3, 1), [1, 2, 3, 1])
assert_equal(insert(a, [1, 1, 1], [1, 2, 3]), [1, 1, 2, 3, 2, 3])
assert_equal(insert(a, 1, [1, 2, 3]), [1, 1, 2, 3, 2, 3])
assert_equal(insert(a, [1, -1, 3], 9), [1, 9, 2, 9, 3, 9])
assert_equal(insert(a, slice(-1, None, -1), 9), [9, 1, 9, 2, 9, 3])
assert_equal(insert(a, [-1, 1, 3], [7, 8, 9]), [1, 8, 2, 7, 3, 9])
b = np.array([0, 1], dtype=np.float64)
assert_equal(insert(b, 0, b[0]), [0., 0., 1.])
assert_equal(insert(b, [], []), b)
# Bools will be treated differently in the future:
# assert_equal(insert(a, np.array([True]*4), 9), [9, 1, 9, 2, 9, 3, 9])
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', FutureWarning)
assert_equal(
insert(a, np.array([True] * 4), 9), [1, 9, 9, 9, 9, 2, 3])
assert_(w[0].category is FutureWarning)
示例7: test_args
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def test_args(self):
dx = np.cumsum(np.ones(5))
dx_uneven = [1., 2., 5., 9., 11.]
f_2d = np.arange(25).reshape(5, 5)
# distances must be scalars or have size equal to gradient[axis]
gradient(np.arange(5), 3.)
gradient(np.arange(5), np.array(3.))
gradient(np.arange(5), dx)
# dy is set equal to dx because scalar
gradient(f_2d, 1.5)
gradient(f_2d, np.array(1.5))
gradient(f_2d, dx_uneven, dx_uneven)
# mix between even and uneven spaces and
# mix between scalar and vector
gradient(f_2d, dx, 2)
# 2D but axis specified
gradient(f_2d, dx, axis=1)
# 2d coordinate arguments are not yet allowed
assert_raises_regex(ValueError, '.*scalars or 1d',
gradient, f_2d, np.stack([dx]*2, axis=-1), 1)
示例8: test_specific_axes
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def test_specific_axes(self):
# Testing that gradient can work on a given axis only
v = [[1, 1], [3, 4]]
x = np.array(v)
dx = [np.array([[2., 3.], [2., 3.]]),
np.array([[0., 0.], [1., 1.]])]
assert_array_equal(gradient(x, axis=0), dx[0])
assert_array_equal(gradient(x, axis=1), dx[1])
assert_array_equal(gradient(x, axis=-1), dx[1])
assert_array_equal(gradient(x, axis=(1, 0)), [dx[1], dx[0]])
# test axis=None which means all axes
assert_almost_equal(gradient(x, axis=None), [dx[0], dx[1]])
# and is the same as no axis keyword given
assert_almost_equal(gradient(x, axis=None), gradient(x))
# test vararg order
assert_array_equal(gradient(x, 2, 3, axis=(1, 0)),
[dx[1]/2.0, dx[0]/3.0])
# test maximal number of varargs
assert_raises(TypeError, gradient, x, 1, 2, axis=1)
assert_raises(np.AxisError, gradient, x, axis=3)
assert_raises(np.AxisError, gradient, x, axis=-3)
# assert_raises(TypeError, gradient, x, axis=[1,])
示例9: test_place
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def test_place(self):
# Make sure that non-np.ndarray objects
# raise an error instead of doing nothing
assert_raises(TypeError, place, [1, 2, 3], [True, False], [0, 1])
a = np.array([1, 4, 3, 2, 5, 8, 7])
place(a, [0, 1, 0, 1, 0, 1, 0], [2, 4, 6])
assert_array_equal(a, [1, 2, 3, 4, 5, 6, 7])
place(a, np.zeros(7), [])
assert_array_equal(a, np.arange(1, 8))
place(a, [1, 0, 1, 0, 1, 0, 1], [8, 9])
assert_array_equal(a, [8, 2, 9, 4, 8, 6, 9])
assert_raises_regex(ValueError, "Cannot insert from an empty array",
lambda: place(a, [0, 0, 0, 0, 0, 1, 0], []))
# See Issue #6974
a = np.array(['12', '34'])
place(a, [0, 1], '9')
assert_array_equal(a, ['12', '9'])
示例10: test_keywords2_ticket_2100
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def test_keywords2_ticket_2100(self):
# Test kwarg support: enhancement ticket 2100
def foo(a, b=1):
return a + b
f = vectorize(foo)
args = np.array([1, 2, 3])
r1 = f(a=args)
r2 = np.array([2, 3, 4])
assert_array_equal(r1, r2)
r1 = f(b=1, a=args)
assert_array_equal(r1, r2)
r1 = f(args, b=2)
r2 = np.array([3, 4, 5])
assert_array_equal(r1, r2)
示例11: test_simple
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def test_simple(self):
assert_almost_equal(
i0(0.5),
np.array(1.0634833707413234))
A = np.array([0.49842636, 0.6969809, 0.22011976, 0.0155549])
assert_almost_equal(
i0(A),
np.array([1.06307822, 1.12518299, 1.01214991, 1.00006049]))
B = np.array([[0.827002, 0.99959078],
[0.89694769, 0.39298162],
[0.37954418, 0.05206293],
[0.36465447, 0.72446427],
[0.48164949, 0.50324519]])
assert_almost_equal(
i0(B),
np.array([[1.17843223, 1.26583466],
[1.21147086, 1.03898290],
[1.03633899, 1.00067775],
[1.03352052, 1.13557954],
[1.05884290, 1.06432317]]))
示例12: test_indexing
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def test_indexing(self):
x = [1, 2, 3]
y = [4, 5, 6, 7]
[X, Y] = meshgrid(x, y, indexing='ij')
assert_array_equal(X, np.array([[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]]))
assert_array_equal(Y, np.array([[4, 5, 6, 7],
[4, 5, 6, 7],
[4, 5, 6, 7]]))
# Test expected shapes:
z = [8, 9]
assert_(meshgrid(x, y)[0].shape == (4, 3))
assert_(meshgrid(x, y, indexing='ij')[0].shape == (3, 4))
assert_(meshgrid(x, y, z)[0].shape == (4, 3, 2))
assert_(meshgrid(x, y, z, indexing='ij')[0].shape == (3, 4, 2))
assert_raises(ValueError, meshgrid, x, y, indexing='notvalid')
示例13: test_with_incorrect_minlength
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def test_with_incorrect_minlength(self):
x = np.array([], dtype=int)
assert_raises_regex(TypeError,
"'str' object cannot be interpreted",
lambda: np.bincount(x, minlength="foobar"))
assert_raises_regex(ValueError,
"must not be negative",
lambda: np.bincount(x, minlength=-1))
x = np.arange(5)
assert_raises_regex(TypeError,
"'str' object cannot be interpreted",
lambda: np.bincount(x, minlength="foobar"))
assert_raises_regex(ValueError,
"must not be negative",
lambda: np.bincount(x, minlength=-1))
示例14: addApexLong
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def addApexLong(inst, *arg, **kwarg):
magCoords = geo2mag(np.array([inst.data.edmaxlat, inst.data.edmaxlon]))
idx, = np.where(magCoords[1, :] < 0)
magCoords[1, idx] += 360.
return(['mlat', 'apex_long'], [magCoords[0, :], magCoords[1, :]])
示例15: eval_batch
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import array [as 别名]
def eval_batch(classifier, model, val_dataset, batch_size, device, n_worker, MAX_COL_COUNT):
validation = datasets.generate_batches(val_dataset,
batch_size=batch_size,
shuffle=False,
drop_last=True,
device=device,
n_workers=n_worker)
y_pred, y_true = [], []
for table_batch, label_batch, mask_batch in tqdm(validation):
#pred, labels = eval_batch(table_batch, label_batch, mask_batch)
# reshap (table_batch * table_size * features)
for f_g in table_batch:
table_batch[f_g] = table_batch[f_g].view(batch_size * MAX_COL_COUNT, -1)
emissions = classifier(table_batch).view(batch_size, MAX_COL_COUNT, -1)
pred = model.decode(emissions, mask_batch)
pred = np.concatenate(pred)
labels = label_batch.view(-1).cpu().numpy()
masks = mask_batch.view(-1).cpu().numpy()
invert_masks = np.invert(masks==1)
y_pred.extend(pred)
y_true.extend(ma.array(labels, mask=invert_masks).compressed())
val_acc = classification_report(y_true, y_pred, output_dict=True)
return val_acc