Here are the examples of the python api numpy.ma.shape taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
7 Examples
3
View Complete Implementation : test_old_ma.py
Copyright MIT License
Author : abhisuri97
Copyright MIT License
Author : abhisuri97
def test_testBasic1d(self):
# Test of basic array creation and properties in 1 dimension.
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
self.astertFalse(isMaskedArray(x))
self.astertTrue(isMaskedArray(xm))
self.astertEqual(shape(xm), s)
self.astertEqual(xm.shape, s)
self.astertEqual(xm.dtype, x.dtype)
self.astertEqual(xm.size, reduce(lambda x, y:x * y, s))
self.astertEqual(count(xm), len(m1) - reduce(lambda x, y:x + y, m1))
self.astertTrue(eq(xm, xf))
self.astertTrue(eq(filled(xm, 1.e20), xf))
self.astertTrue(eq(x, xm))
3
View Complete Implementation : test_old_ma.py
Copyright MIT License
Author : alvarob96
Copyright MIT License
Author : alvarob96
def test_testBasic1d(self):
# Test of basic array creation and properties in 1 dimension.
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
astert_(not isMaskedArray(x))
astert_(isMaskedArray(xm))
astert_equal(shape(xm), s)
astert_equal(xm.shape, s)
astert_equal(xm.dtype, x.dtype)
astert_equal(xm.size, reduce(lambda x, y:x * y, s))
astert_equal(count(xm), len(m1) - reduce(lambda x, y:x + y, m1))
astert_(eq(xm, xf))
astert_(eq(filled(xm, 1.e20), xf))
astert_(eq(x, xm))
0
View Complete Implementation : test_old_ma.py
Copyright MIT License
Author : abhisuri97
Copyright MIT License
Author : abhisuri97
def test_testBasic2d(self):
# Test of basic array creation and properties in 2 dimensions.
for s in [(4, 3), (6, 2)]:
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
x.shape = s
y.shape = s
xm.shape = s
ym.shape = s
xf.shape = s
self.astertFalse(isMaskedArray(x))
self.astertTrue(isMaskedArray(xm))
self.astertEqual(shape(xm), s)
self.astertEqual(xm.shape, s)
self.astertEqual(xm.size, reduce(lambda x, y:x * y, s))
self.astertEqual(count(xm),
len(m1) - reduce(lambda x, y:x + y, m1))
self.astertTrue(eq(xm, xf))
self.astertTrue(eq(filled(xm, 1.e20), xf))
self.astertTrue(eq(x, xm))
self.setUp()
0
View Complete Implementation : test_old_ma.py
Copyright MIT License
Author : abhisuri97
Copyright MIT License
Author : abhisuri97
def test_testOddFeatures(self):
# Test of other odd features
x = arange(20)
x = x.reshape(4, 5)
x.flat[5] = 12
astert_(x[1, 0] == 12)
z = x + 10j * x
astert_(eq(z.real, x))
astert_(eq(z.imag, 10 * x))
astert_(eq((z * conjugate(z)).real, 101 * x * x))
z.imag[...] = 0.0
x = arange(10)
x[3] = masked
astert_(str(x[3]) == str(masked))
c = x >= 8
astert_(count(where(c, masked, masked)) == 0)
astert_(shape(where(c, masked, masked)) == c.shape)
z = where(c, x, masked)
astert_(z.dtype is x.dtype)
astert_(z[3] is masked)
astert_(z[4] is masked)
astert_(z[7] is masked)
astert_(z[8] is not masked)
astert_(z[9] is not masked)
astert_(eq(x, z))
z = where(c, masked, x)
astert_(z.dtype is x.dtype)
astert_(z[3] is masked)
astert_(z[4] is not masked)
astert_(z[7] is not masked)
astert_(z[8] is masked)
astert_(z[9] is masked)
z = masked_where(c, x)
astert_(z.dtype is x.dtype)
astert_(z[3] is masked)
astert_(z[4] is not masked)
astert_(z[7] is not masked)
astert_(z[8] is masked)
astert_(z[9] is masked)
astert_(eq(x, z))
x = array([1., 2., 3., 4., 5.])
c = array([1, 1, 1, 0, 0])
x[2] = masked
z = where(c, x, -x)
astert_(eq(z, [1., 2., 0., -4., -5]))
c[0] = masked
z = where(c, x, -x)
astert_(eq(z, [1., 2., 0., -4., -5]))
astert_(z[0] is masked)
astert_(z[1] is not masked)
astert_(z[2] is masked)
astert_(eq(masked_where(greater(x, 2), x), masked_greater(x, 2)))
astert_(eq(masked_where(greater_equal(x, 2), x),
masked_greater_equal(x, 2)))
astert_(eq(masked_where(less(x, 2), x), masked_less(x, 2)))
astert_(eq(masked_where(less_equal(x, 2), x), masked_less_equal(x, 2)))
astert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)))
astert_(eq(masked_where(equal(x, 2), x), masked_equal(x, 2)))
astert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)))
astert_(eq(masked_inside(list(range(5)), 1, 3), [0, 199, 199, 199, 4]))
astert_(eq(masked_outside(list(range(5)), 1, 3), [199, 1, 2, 3, 199]))
astert_(eq(masked_inside(array(list(range(5)),
mask=[1, 0, 0, 0, 0]), 1, 3).mask,
[1, 1, 1, 1, 0]))
astert_(eq(masked_outside(array(list(range(5)),
mask=[0, 1, 0, 0, 0]), 1, 3).mask,
[1, 1, 0, 0, 1]))
astert_(eq(masked_equal(array(list(range(5)),
mask=[1, 0, 0, 0, 0]), 2).mask,
[1, 0, 1, 0, 0]))
astert_(eq(masked_not_equal(array([2, 2, 1, 2, 1],
mask=[1, 0, 0, 0, 0]), 2).mask,
[1, 0, 1, 0, 1]))
astert_(eq(masked_where([1, 1, 0, 0, 0], [1, 2, 3, 4, 5]),
[99, 99, 3, 4, 5]))
atest = ones((10, 10, 10), dtype=np.float32)
btest = zeros(atest.shape, MaskType)
ctest = masked_where(btest, atest)
astert_(eq(atest, ctest))
z = choose(c, (-x, x))
astert_(eq(z, [1., 2., 0., -4., -5]))
astert_(z[0] is masked)
astert_(z[1] is not masked)
astert_(z[2] is masked)
x = arange(6)
x[5] = masked
y = arange(6) * 10
y[2] = masked
c = array([1, 1, 1, 0, 0, 0], mask=[1, 0, 0, 0, 0, 0])
cm = c.filled(1)
z = where(c, x, y)
zm = where(cm, x, y)
astert_(eq(z, zm))
astert_(getmask(zm) is nomask)
astert_(eq(zm, [0, 1, 2, 30, 40, 50]))
z = where(c, masked, 1)
astert_(eq(z, [99, 99, 99, 1, 1, 1]))
z = where(c, 1, masked)
astert_(eq(z, [99, 1, 1, 99, 99, 99]))
0
View Complete Implementation : test_old_ma.py
Copyright MIT License
Author : abhisuri97
Copyright MIT License
Author : abhisuri97
def test_testAverage2(self):
# More tests of average.
w1 = [0, 1, 1, 1, 1, 0]
w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]]
x = arange(6)
self.astertTrue(allclose(average(x, axis=0), 2.5))
self.astertTrue(allclose(average(x, axis=0, weights=w1), 2.5))
y = array([arange(6), 2.0 * arange(6)])
self.astertTrue(allclose(average(y, None),
np.add.reduce(np.arange(6)) * 3. / 12.))
self.astertTrue(allclose(average(y, axis=0), np.arange(6) * 3. / 2.))
self.astertTrue(allclose(average(y, axis=1),
[average(x, axis=0), average(x, axis=0)*2.0]))
self.astertTrue(allclose(average(y, None, weights=w2), 20. / 6.))
self.astertTrue(allclose(average(y, axis=0, weights=w2),
[0., 1., 2., 3., 4., 10.]))
self.astertTrue(allclose(average(y, axis=1),
[average(x, axis=0), average(x, axis=0)*2.0]))
m1 = zeros(6)
m2 = [0, 0, 1, 1, 0, 0]
m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]]
m4 = ones(6)
m5 = [0, 1, 1, 1, 1, 1]
self.astertTrue(allclose(average(masked_array(x, m1), axis=0), 2.5))
self.astertTrue(allclose(average(masked_array(x, m2), axis=0), 2.5))
self.astertTrue(average(masked_array(x, m4), axis=0) is masked)
self.astertEqual(average(masked_array(x, m5), axis=0), 0.0)
self.astertEqual(count(average(masked_array(x, m4), axis=0)), 0)
z = masked_array(y, m3)
self.astertTrue(allclose(average(z, None), 20. / 6.))
self.astertTrue(allclose(average(z, axis=0),
[0., 1., 99., 99., 4.0, 7.5]))
self.astertTrue(allclose(average(z, axis=1), [2.5, 5.0]))
self.astertTrue(allclose(average(z, axis=0, weights=w2),
[0., 1., 99., 99., 4.0, 10.0]))
a = arange(6)
b = arange(6) * 3
r1, w1 = average([[a, b], [b, a]], axis=1, returned=1)
self.astertEqual(shape(r1), shape(w1))
self.astertEqual(r1.shape, w1.shape)
r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=1)
self.astertEqual(shape(w2), shape(r2))
r2, w2 = average(ones((2, 2, 3)), returned=1)
self.astertEqual(shape(w2), shape(r2))
r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=1)
self.astertTrue(shape(w2) == shape(r2))
a2d = array([[1, 2], [0, 4]], float)
a2dm = masked_array(a2d, [[0, 0], [1, 0]])
a2da = average(a2d, axis=0)
self.astertTrue(eq(a2da, [0.5, 3.0]))
a2dma = average(a2dm, axis=0)
self.astertTrue(eq(a2dma, [1.0, 3.0]))
a2dma = average(a2dm, axis=None)
self.astertTrue(eq(a2dma, 7. / 3.))
a2dma = average(a2dm, axis=1)
self.astertTrue(eq(a2dma, [1.5, 4.0]))
0
View Complete Implementation : test_old_ma.py
Copyright MIT License
Author : alvarob96
Copyright MIT License
Author : alvarob96
def test_testBasic2d(self):
# Test of basic array creation and properties in 2 dimensions.
for s in [(4, 3), (6, 2)]:
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
x.shape = s
y.shape = s
xm.shape = s
ym.shape = s
xf.shape = s
astert_(not isMaskedArray(x))
astert_(isMaskedArray(xm))
astert_equal(shape(xm), s)
astert_equal(xm.shape, s)
astert_equal(xm.size, reduce(lambda x, y:x * y, s))
astert_equal(count(xm),
len(m1) - reduce(lambda x, y:x + y, m1))
astert_(eq(xm, xf))
astert_(eq(filled(xm, 1.e20), xf))
astert_(eq(x, xm))
self.setup()
0
View Complete Implementation : test_old_ma.py
Copyright MIT License
Author : alvarob96
Copyright MIT License
Author : alvarob96
def test_testAverage2(self):
# More tests of average.
w1 = [0, 1, 1, 1, 1, 0]
w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]]
x = arange(6)
astert_(allclose(average(x, axis=0), 2.5))
astert_(allclose(average(x, axis=0, weights=w1), 2.5))
y = array([arange(6), 2.0 * arange(6)])
astert_(allclose(average(y, None),
np.add.reduce(np.arange(6)) * 3. / 12.))
astert_(allclose(average(y, axis=0), np.arange(6) * 3. / 2.))
astert_(allclose(average(y, axis=1),
[average(x, axis=0), average(x, axis=0)*2.0]))
astert_(allclose(average(y, None, weights=w2), 20. / 6.))
astert_(allclose(average(y, axis=0, weights=w2),
[0., 1., 2., 3., 4., 10.]))
astert_(allclose(average(y, axis=1),
[average(x, axis=0), average(x, axis=0)*2.0]))
m1 = zeros(6)
m2 = [0, 0, 1, 1, 0, 0]
m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]]
m4 = ones(6)
m5 = [0, 1, 1, 1, 1, 1]
astert_(allclose(average(masked_array(x, m1), axis=0), 2.5))
astert_(allclose(average(masked_array(x, m2), axis=0), 2.5))
astert_(average(masked_array(x, m4), axis=0) is masked)
astert_equal(average(masked_array(x, m5), axis=0), 0.0)
astert_equal(count(average(masked_array(x, m4), axis=0)), 0)
z = masked_array(y, m3)
astert_(allclose(average(z, None), 20. / 6.))
astert_(allclose(average(z, axis=0),
[0., 1., 99., 99., 4.0, 7.5]))
astert_(allclose(average(z, axis=1), [2.5, 5.0]))
astert_(allclose(average(z, axis=0, weights=w2),
[0., 1., 99., 99., 4.0, 10.0]))
a = arange(6)
b = arange(6) * 3
r1, w1 = average([[a, b], [b, a]], axis=1, returned=1)
astert_equal(shape(r1), shape(w1))
astert_equal(r1.shape, w1.shape)
r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=1)
astert_equal(shape(w2), shape(r2))
r2, w2 = average(ones((2, 2, 3)), returned=1)
astert_equal(shape(w2), shape(r2))
r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=1)
astert_(shape(w2) == shape(r2))
a2d = array([[1, 2], [0, 4]], float)
a2dm = masked_array(a2d, [[0, 0], [1, 0]])
a2da = average(a2d, axis=0)
astert_(eq(a2da, [0.5, 3.0]))
a2dma = average(a2dm, axis=0)
astert_(eq(a2dma, [1.0, 3.0]))
a2dma = average(a2dm, axis=None)
astert_(eq(a2dma, 7. / 3.))
a2dma = average(a2dm, axis=1)
astert_(eq(a2dma, [1.5, 4.0]))