Example #1
0
 def test_mask_rowcols(self):
     # Tests mask_rowcols.
     x = array(np.arange(9).reshape(3, 3),
               mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
     assert_equal(mask_rowcols(x).mask,
                  [[1, 1, 1], [1, 0, 0], [1, 0, 0]])
     assert_equal(mask_rowcols(x, 0).mask,
                  [[1, 1, 1], [0, 0, 0], [0, 0, 0]])
     assert_equal(mask_rowcols(x, 1).mask,
                  [[1, 0, 0], [1, 0, 0], [1, 0, 0]])
     x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
     assert_equal(mask_rowcols(x).mask,
                  [[0, 1, 0], [1, 1, 1], [0, 1, 0]])
     assert_equal(mask_rowcols(x, 0).mask,
                  [[0, 0, 0], [1, 1, 1], [0, 0, 0]])
     assert_equal(mask_rowcols(x, 1).mask,
                  [[0, 1, 0], [0, 1, 0], [0, 1, 0]])
     x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]])
     assert_equal(mask_rowcols(x).mask,
                  [[1, 1, 1], [1, 1, 1], [1, 1, 0]])
     assert_equal(mask_rowcols(x, 0).mask,
                  [[1, 1, 1], [1, 1, 1], [0, 0, 0]])
     assert_equal(mask_rowcols(x, 1,).mask,
                  [[1, 1, 0], [1, 1, 0], [1, 1, 0]])
     x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
     self.assertTrue(mask_rowcols(x).all() is masked)
     self.assertTrue(mask_rowcols(x, 0).all() is masked)
     self.assertTrue(mask_rowcols(x, 1).all() is masked)
     self.assertTrue(mask_rowcols(x).mask.all())
     self.assertTrue(mask_rowcols(x, 0).mask.all())
     self.assertTrue(mask_rowcols(x, 1).mask.all())
Example #2
0
 def test_intersect1d(self):
     # Test intersect1d
     x = array([1, 3, 3, 3], mask=[0, 0, 0, 1])
     y = array([3, 1, 1, 1], mask=[0, 0, 0, 1])
     test = intersect1d(x, y)
     control = array([1, 3, -1], mask=[0, 0, 1])
     assert_equal(test, control)
 def test_masked_1d(self):
     "test the examples given in the docstring of ma.median"
     x = array(np.arange(8), mask=[0]*4 + [1]*4)
     assert_equal(np.ma.median(x), 1.5)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     x = array(np.arange(10).reshape(2, 5), mask=[0]*6 + [1]*4)
     assert_equal(np.ma.median(x), 2.5)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
Example #4
0
 def test_stacking_arrays(self):
     a = array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
     b = array([[13,14,15,16],[17,18,19,20],[21,22,23,24]])
     c = vstack((a,b))
     numpy.testing.assert_array_equal(c, array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20],[21,22,23,24]]))
     d = hstack((a,b))
     numpy.testing.assert_array_equal(d, array([[1,2,3,4,13,14,15,16],
                                                [5,6,7,8,17,18,19,20],
                                                [9,10,11,12,21,22,23,24]]))
Example #5
0
 def test_union1d(self):
     # Test union1d
     a = array([1, 2, 5, 7, 5, -1], mask=[0, 0, 0, 0, 0, 1])
     b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
     test = union1d(a, b)
     control = array([1, 2, 3, 4, 5, 7, -1], mask=[0, 0, 0, 0, 0, 0, 1])
     assert_equal(test, control)
     #
     assert_array_equal([], union1d([], []))
Example #6
0
 def test_record_arrays(self):
     
     img = array([ [[0,1], [0,0]], [[0,0], [1,0]], [[0,0], [0,1]] ], dtype='float32')
     self.assertEqual(img.shape, (3,2,2))
     
     img = array([[(0,0,0), (1,0,0)], [(0,1,0), (0,0,1)]], [('r','float32'),('g','float32'),('b','float32')])
     img = array([[(0,0,0), (1,0,0)], [(0,1,0), (0,0,1)]], {'names': ('r','g','b'), 'formats': ('f4', 'f4', 'f4')})
     img = zeros((2,2), [('r','float32'),('g','float32'),('b','float32')])
     img.flat = [(0,0,0),(1,0,0),(0,1,0),(0,0,1)]
     print img.view(recarray)
Example #7
0
 def test_setdiff1d(self):
     # Test setdiff1d
     a = array([6, 5, 4, 7, 7, 1, 2, 1], mask=[0, 0, 0, 0, 0, 0, 0, 1])
     b = array([2, 4, 3, 3, 2, 1, 5])
     test = setdiff1d(a, b)
     assert_equal(test, array([6, 7, -1], mask=[0, 0, 1]))
     #
     a = arange(10)
     b = arange(8)
     assert_equal(setdiff1d(a, b), array([8, 9]))
Example #8
0
 def test_setxor1d(self):
     # Test setxor1d
     a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
     b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
     test = setxor1d(a, b)
     assert_equal(test, array([3, 4, 7]))
     #
     a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
     b = [1, 2, 3, 4, 5]
     test = setxor1d(a, b)
     assert_equal(test, array([3, 4, 7, -1], mask=[0, 0, 0, 1]))
     #
     a = array([1, 2, 3])
     b = array([6, 5, 4])
     test = setxor1d(a, b)
     assert_(isinstance(test, MaskedArray))
     assert_equal(test, [1, 2, 3, 4, 5, 6])
     #
     a = array([1, 8, 2, 3], mask=[0, 1, 0, 0])
     b = array([6, 5, 4, 8], mask=[0, 0, 0, 1])
     test = setxor1d(a, b)
     assert_(isinstance(test, MaskedArray))
     assert_equal(test, [1, 2, 3, 4, 5, 6])
     #
     assert_array_equal([], setxor1d([], []))
Example #9
0
 def test_indexing_with_boolean_arrays(self):
     
     a = arange(12).reshape(3,4)
     b = a > 4
     numpy.testing.assert_array_equal(b, array([[False, False, False, False],
                                                [False, True, True, True],
                                                [True, True, True, True]], dtype=bool))
     a[b] = 0
     numpy.testing.assert_array_equal(a, array([[0,1,2,3],
                                                [4,0,0,0],
                                                [0,0,0,0]]))
     
     numpy.testing.assert_array_equal(mandelbrot(4, 4, maxit=1), array([[1,1,1,1],
                                                                        [1,1,1,1],
                                                                        [1,1,1,1],
                                                                        [1,1,1,1]]))
     
     a = arange(12).reshape(3,-1)
     b1 = array([False,True,True])
     b2 = array([True,False,True,False])
     
     numpy.testing.assert_array_equal(a[b1,:], array([[4,5,6,7],
                                                     [8,9,10,11]]))
     
     numpy.testing.assert_array_equal(a[b1], array([[4,5,6,7],
                                                    [8,9,10,11]]))
     
     numpy.testing.assert_array_equal(a[:,b2], array([[0,2],
                                                     [4,6],
                                                     [8,10]]))
     
     numpy.testing.assert_array_equal(a[b1,b2], array([4,10]))
Example #10
0
    def test_in1d_invert(self):
        # Test in1d's invert parameter
        a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
        b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
        assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))

        a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1])
        b = array([1, 5, -1], mask=[0, 0, 1])
        assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))

        assert_array_equal([], in1d([], [], invert=True))
Example #11
0
 def test_in1d(self):
     # Test in1d
     a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
     b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
     test = in1d(a, b)
     assert_equal(test, [True, True, True, False, True])
     #
     a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1])
     b = array([1, 5, -1], mask=[0, 0, 1])
     test = in1d(a, b)
     assert_equal(test, [True, True, False, True, True])
     #
     assert_array_equal([], in1d([], []))
Example #12
0
 def test_shape(self):
     
     a = array([1,1,1])
     self.assertEqual(a.shape, (3,))
     a = array([[1],
                [1]])
     self.assertEqual(a.shape, (2,1))
     a = array([[[1,1],[1,1]],
                [[1,1],[1,1]]])
     self.assertEqual(a.shape, (2,2,2))
     a = array([[[[1],[1]],
                [[1],[1]]]])
     self.assertEqual(a.shape, (1,2,2,1))
Example #13
0
 def test_ediff1d_tobegin(self):
     # Test ediff1d w/ to_begin
     x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
     test = ediff1d(x, to_begin=masked)
     control = array([0, 1, 1, 1, 4], mask=[1, 1, 0, 0, 1])
     assert_equal(test, control)
     assert_equal(test.filled(0), control.filled(0))
     assert_equal(test.mask, control.mask)
     #
     test = ediff1d(x, to_begin=[1, 2, 3])
     control = array([1, 2, 3, 1, 1, 1, 4], mask=[0, 0, 0, 1, 0, 0, 1])
     assert_equal(test, control)
     assert_equal(test.filled(0), control.filled(0))
     assert_equal(test.mask, control.mask)
Example #14
0
 def test_ediff1d_tobegin_toend(self):
     # Test ediff1d w/ to_begin and to_end
     x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
     test = ediff1d(x, to_end=masked, to_begin=masked)
     control = array([0, 1, 1, 1, 4, 0], mask=[1, 1, 0, 0, 1, 1])
     assert_equal(test, control)
     assert_equal(test.data, control.data)
     assert_equal(test.mask, control.mask)
     #
     test = ediff1d(x, to_end=[1, 2, 3], to_begin=masked)
     control = array([0, 1, 1, 1, 4, 1, 2, 3], mask=[1, 1, 0, 0, 1, 0, 0, 0])
     assert_equal(test, control)
     assert_equal(test.data, control.data)
     assert_equal(test.mask, control.mask)
Example #15
0
 def test_ediff1d_toend(self):
     # Test ediff1d w/ to_end
     x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
     test = ediff1d(x, to_end=masked)
     control = array([1, 1, 1, 4, 0], mask=[1, 0, 0, 1, 1])
     assert_equal(test, control)
     assert_equal(test.filled(0), control.filled(0))
     assert_equal(test.mask, control.mask)
     #
     test = ediff1d(x, to_end=[1, 2, 3])
     control = array([1, 1, 1, 4, 1, 2, 3], mask=[1, 0, 0, 1, 0, 0, 0])
     assert_equal(test, control)
     assert_equal(test.filled(0), control.filled(0))
     assert_equal(test.mask, control.mask)
Example #16
0
 def test_docstring_examples(self):
     "test the examples given in the docstring of ma.median"
     x = array(np.arange(8), mask=[0]*4 + [1]*4)
     assert_equal(np.ma.median(x), 1.5)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is not MaskedArray)
     x = array(np.arange(10).reshape(2, 5), mask=[0]*6 + [1]*4)
     assert_equal(np.ma.median(x), 2.5)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is not MaskedArray)
     ma_x = np.ma.median(x, axis=-1, overwrite_input=True)
     assert_equal(ma_x, [2., 5.])
     assert_equal(ma_x.shape, (2,), "shape mismatch")
     assert_(type(ma_x) is MaskedArray)
Example #17
0
 def test_ediff1d_ndarray(self):
     # Test ediff1d w/ a ndarray
     x = np.arange(5)
     test = ediff1d(x)
     control = array([1, 1, 1, 1], mask=[0, 0, 0, 0])
     assert_equal(test, control)
     self.assertTrue(isinstance(test, MaskedArray))
     assert_equal(test.filled(0), control.filled(0))
     assert_equal(test.mask, control.mask)
     #
     test = ediff1d(x, to_end=masked, to_begin=masked)
     control = array([0, 1, 1, 1, 1, 0], mask=[1, 0, 0, 0, 0, 1])
     self.assertTrue(isinstance(test, MaskedArray))
     assert_equal(test.filled(0), control.filled(0))
     assert_equal(test.mask, control.mask)
    def test_get_gaussian_2d(self):
        X = asarray([-1, 1])
        X = reshape(X, (len(X), 1))
        y = asarray([+1 if x >= 0 else -1 for x in X])
        covariance = SquaredExponentialCovariance(sigma=1, scale=1)
        likelihood = LogitLikelihood()
        gp = GaussianProcess(y, X, covariance, likelihood)
        laplace = LaplaceApproximation(gp, newton_start=asarray([3, 3]))
        
        f_mode, L, steps = laplace.find_mode_newton(return_full=True)
        gaussian = laplace.get_gaussian(f_mode, L)
        F = linspace(-10, 10, 20)
        D = zeros((len(F), len(F)))
        Q = array(D, copy=True)
        for i in range(len(F)):
            for j in range(len(F)):
                f = asarray([F[i], F[j]])
                D[i, j] = gp.log_posterior_unnormalised(f)
                Q[i, j] = gaussian.log_pdf(f.reshape(1, len(f)))
        
        subplot(1, 2, 1)
        pcolor(F, F, D)
        hold(True)
        plot(steps[:, 0], steps[:, 1])
        plot(f_mode[1], f_mode[0], 'mo', markersize=10)
        hold(False)
        colorbar()
        subplot(1, 2, 2)
        pcolor(F, F, Q)
        hold(True)
        plot(f_mode[1], f_mode[0], 'mo', markersize=10)
        hold(False)
        colorbar()
#        show()
        clf()
Example #19
0
 def get_mushroom_data():
     data_dir = os.sep.join(__file__.split(os.sep)[:-3] + ["data"])
     filename = data_dir + os.sep + "agaricus-lepiota.data"
     f = open(filename)
     X = asarray(["".join(x.strip(os.linesep).split(",")) for x in f.readlines()], dtype="c")
     f.close()
     
     # first column is labels
     labels=asarray([+1. if X[i,0]=="e" else -1. for i in range(len(X))])
     
     # remove attribute eleven which contains loads of missing values
     remove_idx = 11
     indices = hstack((arange(remove_idx), arange(remove_idx + 1, X.shape[1])))
     X = X[:, indices[1:]]
     
     # generate a map of categorical to int
     cat2num={}
     U=unique(X)
     for num in range(len(U)):
         cat2num[U[num]]=num
     
     # produce an integer representation of the categorical data
     X_int=asarray([[cat2num[X[i,j]] for i in range(X.shape[0])] for j in range(X.shape[1])]).T
     
     return array(X_int, dtype=float64) ,labels
Example #20
0
 def get_usps_data():
     data_dir = os.sep.join(__file__.split(os.sep)[:-3] + ["data"])
     filename = data_dir + os.sep + "usps3vs5.mat"
     mat = scipy.io.loadmat(filename)
     data = mat['usps3vs5_data']
     lab = array([float(x) for x in mat['usps3vs5_labels'][:, 0]])
     return data, lab
Example #21
0
 def test_attributepropagation(self):
     x = array(arange(5), mask=[0]+[1]*4)
     my = masked_array(subarray(x))
     ym = msubarray(x)
     #
     z = (my+1)
     self.assertTrue(isinstance(z, MaskedArray))
     self.assertTrue(not isinstance(z, MSubArray))
     self.assertTrue(isinstance(z._data, SubArray))
     assert_equal(z._data.info, {})
     #
     z = (ym+1)
     self.assertTrue(isinstance(z, MaskedArray))
     self.assertTrue(isinstance(z, MSubArray))
     self.assertTrue(isinstance(z._data, SubArray))
     self.assertTrue(z._data.info['added'] > 0)
     # Test that inplace methods from data get used (gh-4617)
     ym += 1
     self.assertTrue(isinstance(ym, MaskedArray))
     self.assertTrue(isinstance(ym, MSubArray))
     self.assertTrue(isinstance(ym._data, SubArray))
     self.assertTrue(ym._data.info['iadded'] > 0)
     #
     ym._set_mask([1, 0, 0, 0, 1])
     assert_equal(ym._mask, [1, 0, 0, 0, 1])
     ym._series._set_mask([0, 0, 0, 0, 1])
     assert_equal(ym._mask, [0, 0, 0, 0, 1])
     #
     xsub = subarray(x, info={'name':'x'})
     mxsub = masked_array(xsub)
     self.assertTrue(hasattr(mxsub, 'info'))
     assert_equal(mxsub.info, xsub.info)
Example #22
0
 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, dtype=np.float_)
     assert_equal(average(x, axis=0), 2.5)
     assert_equal(average(x, axis=0, weights=w1), 2.5)
     y = array([arange(6, dtype=np.float_), 2.0 * arange(6)])
     assert_equal(average(y, None), np.add.reduce(np.arange(6)) * 3. / 12.)
     assert_equal(average(y, axis=0), np.arange(6) * 3. / 2.)
     assert_equal(average(y, axis=1),
                  [average(x, axis=0), average(x, axis=0) * 2.0])
     assert_equal(average(y, None, weights=w2), 20. / 6.)
     assert_equal(average(y, axis=0, weights=w2),
                  [0., 1., 2., 3., 4., 10.])
     assert_equal(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]
     assert_equal(average(masked_array(x, m1), axis=0), 2.5)
     assert_equal(average(masked_array(x, m2), axis=0), 2.5)
     assert_equal(average(masked_array(x, m4), axis=0).mask, [True])
     assert_equal(average(masked_array(x, m5), axis=0), 0.0)
     assert_equal(count(average(masked_array(x, m4), axis=0)), 0)
     z = masked_array(y, m3)
     assert_equal(average(z, None), 20. / 6.)
     assert_equal(average(z, axis=0), [0., 1., 99., 99., 4.0, 7.5])
     assert_equal(average(z, axis=1), [2.5, 5.0])
     assert_equal(average(z, axis=0, weights=w2),
                  [0., 1., 99., 99., 4.0, 10.0])
Example #23
0
 def test_ediff1d(self):
     "Tests mediff1d"
     x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
     control = array([1, 1, 1, 4], mask=[1, 0, 0, 1])
     test = ediff1d(x)
     assert_equal(test, control)
     assert_equal(test.data, control.data)
     assert_equal(test.mask, control.mask)
Example #24
0
 def test_dot_returns_maskedarray(self):
     # See gh-6611
     a = np.eye(3)
     b = array(a)
     assert_(type(dot(a, a)) is MaskedArray)
     assert_(type(dot(a, b)) is MaskedArray)
     assert_(type(dot(b, a)) is MaskedArray)
     assert_(type(dot(b, b)) is MaskedArray)
Example #25
0
 def test_ediff1d(self):
     # Tests mediff1d
     x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
     control = array([1, 1, 1, 4], mask=[1, 0, 0, 1])
     test = ediff1d(x)
     assert_equal(test, control)
     assert_equal(test.filled(0), control.filled(0))
     assert_equal(test.mask, control.mask)
Example #26
0
 def test_masked_1d(self):
     x = array(np.arange(5), mask=True)
     assert_equal(np.ma.median(x), np.ma.masked)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is np.ma.core.MaskedConstant)
     x = array(np.arange(5), mask=False)
     assert_equal(np.ma.median(x), 2.)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is not MaskedArray)
     x = array(np.arange(5), mask=[0,1,0,0,0])
     assert_equal(np.ma.median(x), 2.5)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is not MaskedArray)
     x = array(np.arange(5), mask=[0,1,1,1,1])
     assert_equal(np.ma.median(x), 0.)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is not MaskedArray)
Example #27
0
 def test_compress_rowcols(self):
     # Tests compress_rowcols
     x = array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
     assert_equal(compress_rowcols(x), [[4, 5], [7, 8]])
     assert_equal(compress_rowcols(x, 0), [[3, 4, 5], [6, 7, 8]])
     assert_equal(compress_rowcols(x, 1), [[1, 2], [4, 5], [7, 8]])
     x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
     assert_equal(compress_rowcols(x), [[0, 2], [6, 8]])
     assert_equal(compress_rowcols(x, 0), [[0, 1, 2], [6, 7, 8]])
     assert_equal(compress_rowcols(x, 1), [[0, 2], [3, 5], [6, 8]])
     x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]])
     assert_equal(compress_rowcols(x), [[8]])
     assert_equal(compress_rowcols(x, 0), [[6, 7, 8]])
     assert_equal(compress_rowcols(x, 1), [[2], [5], [8]])
     x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
     assert_equal(compress_rowcols(x).size, 0)
     assert_equal(compress_rowcols(x, 0).size, 0)
     assert_equal(compress_rowcols(x, 1).size, 0)
Example #28
0
 def test_masked_all_like(self):
     # Tests masked_all
     # Standard dtype
     base = array([1, 2], dtype=float)
     test = masked_all_like(base)
     control = array([1, 1], mask=[1, 1], dtype=float)
     assert_equal(test, control)
     # Flexible dtype
     dt = np.dtype({"names": ["a", "b"], "formats": ["f", "f"]})
     base = array([(0, 0), (0, 0)], mask=[(1, 1), (1, 1)], dtype=dt)
     test = masked_all_like(base)
     control = array([(10, 10), (10, 10)], mask=[(1, 1), (1, 1)], dtype=dt)
     assert_equal(test, control)
     # Nested dtype
     dt = np.dtype([("a", "f"), ("b", [("ba", "f"), ("bb", "f")])])
     control = array([(1, (1, 1)), (1, (1, 1))], mask=[(1, (1, 1)), (1, (1, 1))], dtype=dt)
     test = masked_all_like(control)
     assert_equal(test, control)
Example #29
0
 def test_testAverage1(self):
     # Test of average.
     ott = array([0., 1., 2., 3.], mask=[True, False, False, False])
     assert_equal(2.0, average(ott, axis=0))
     assert_equal(2.0, average(ott, weights=[1., 1., 2., 1.]))
     result, wts = average(ott, weights=[1., 1., 2., 1.], returned=1)
     assert_equal(2.0, result)
     self.assertTrue(wts == 4.0)
     ott[:] = masked
     assert_equal(average(ott, axis=0).mask, [True])
     ott = array([0., 1., 2., 3.], mask=[True, False, False, False])
     ott = ott.reshape(2, 2)
     ott[:, 1] = masked
     assert_equal(average(ott, axis=0), [2.0, 0.0])
     assert_equal(average(ott, axis=1).mask[0], [True])
     assert_equal([2., 0.], average(ott, axis=0))
     result, wts = average(ott, axis=0, returned=1)
     assert_equal(wts, [1., 0.])
Example #30
0
 def test_masked_all_like(self):
     # Tests masked_all
     # Standard dtype
     base = array([1, 2], dtype=float)
     test = masked_all_like(base)
     control = array([1, 1], mask=[1, 1], dtype=float)
     assert_equal(test, control)
     # Flexible dtype
     dt = np.dtype({'names': ['a', 'b'], 'formats': ['f', 'f']})
     base = array([(0, 0), (0, 0)], mask=[(1, 1), (1, 1)], dtype=dt)
     test = masked_all_like(base)
     control = array([(10, 10), (10, 10)], mask=[(1, 1), (1, 1)], dtype=dt)
     assert_equal(test, control)
     # Nested dtype
     dt = np.dtype([('a', 'f'), ('b', [('ba', 'f'), ('bb', 'f')])])
     control = array([(1, (1, 1)), (1, (1, 1))],
                     mask=[(1, (1, 1)), (1, (1, 1))], dtype=dt)
     test = masked_all_like(control)
     assert_equal(test, control)
Example #31
0
 def test_compress2d(self):
     # Tests compress2d
     x = array(np.arange(9).reshape(3, 3),
               mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
     assert_equal(compress_rowcols(x), [[4, 5], [7, 8]])
     assert_equal(compress_rowcols(x, 0), [[3, 4, 5], [6, 7, 8]])
     assert_equal(compress_rowcols(x, 1), [[1, 2], [4, 5], [7, 8]])
     x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
     assert_equal(compress_rowcols(x), [[0, 2], [6, 8]])
     assert_equal(compress_rowcols(x, 0), [[0, 1, 2], [6, 7, 8]])
     assert_equal(compress_rowcols(x, 1), [[0, 2], [3, 5], [6, 8]])
     x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]])
     assert_equal(compress_rowcols(x), [[8]])
     assert_equal(compress_rowcols(x, 0), [[6, 7, 8]])
     assert_equal(compress_rowcols(
         x,
         1,
     ), [[2], [5], [8]])
     x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
     assert_equal(compress_rowcols(x).size, 0)
     assert_equal(compress_rowcols(x, 0).size, 0)
     assert_equal(compress_rowcols(x, 1).size, 0)
Example #32
0
    def get_pima_data():
        data_dir = os.sep.join(__file__.split(os.sep)[:-3] + ["data"])
        filename = data_dir + os.sep + "pima-indians-diabetes.data"
        data = loadtxt(filename, delimiter=",")

        # create labelling
        lab = data[:, -1]
        lab = array([1. if x == 1 else -1.0 for x in lab])

        # cut off labeling
        data = data[:, :-1]

        return data, lab
Example #33
0
    def get_glass_data():
        data_dir = os.sep.join(__file__.split(os.sep)[:-3] + ["data"])
        filename = data_dir + os.sep + "glass.data"
        data = loadtxt(filename, delimiter=",")

        # create a binary "window glass" vs "non-window glass" labelling
        lab = data[:, -1]
        lab = array([1. if x <= 4 else -1.0 for x in lab])

        # cut off ids and labeling
        data = data[:, 1:-1]

        return data, lab
Example #34
0
def grid_score(k, grid):
    width, height = grid_dimensions(grid)

    row_penalties = list(max(0, sum(row) - k) for row in grid)
    col_penalties = list(
        max(0,
            sum(row[j] for row in grid) - k) for j in range(width))

    grid = array(grid)
    flip = flipud(grid)

    r1 = range(-(width - 1), width)
    r2 = range(-(height - 1), height)

    dia_penalties = lambda m, dia_i: max(0, int(m.trace(dia_i)) - k)

    di1_penalties = list(dia_penalties(grid, d) for d in r1)
    di2_penalties = list(dia_penalties(flip, d) for d in r2)

    return sum(row_penalties) + sum(col_penalties) + sum(di1_penalties) + sum(
        di2_penalties)
 def test_testAverage3(self):
     # Yet more tests of average!
     a = arange(6)
     b = arange(6) * 3
     r1, w1 = average([[a, b], [b, a]], axis=1, returned=1)
     assert_equal(shape(r1), shape(w1))
     assert_equal(r1.shape, w1.shape)
     r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=1)
     assert_equal(shape(w2), shape(r2))
     r2, w2 = average(ones((2, 2, 3)), returned=1)
     assert_equal(shape(w2), shape(r2))
     r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=1)
     assert_equal(shape(w2), shape(r2))
     a2d = array([[1, 2], [0, 4]], float)
     a2dm = masked_array(a2d, [[False, False], [True, False]])
     a2da = average(a2d, axis=0)
     assert_equal(a2da, [0.5, 3.0])
     a2dma = average(a2dm, axis=0)
     assert_equal(a2dma, [1.0, 3.0])
     a2dma = average(a2dm, axis=None)
     assert_equal(a2dma, 7. / 3.)
     a2dma = average(a2dm, axis=1)
     assert_equal(a2dma, [1.5, 4.0])
Example #36
0
    def test_axis_argument_errors(self):
        msg = "mask = %s, ndim = %s, axis = %s, overwrite_input = %s"
        for ndmin in range(5):
            for mask in [False, True]:
                x = array(1, ndmin=ndmin, mask=mask)

                # Valid axis values should not raise exception
                args = itertools.product(range(-ndmin, ndmin), [False, True])
                for axis, over in args:
                    try:
                        np.ma.median(x, axis=axis, overwrite_input=over)
                    except:
                        raise AssertionError(msg % (mask, ndmin, axis, over))

                # Invalid axis values should raise exception
                args = itertools.product([-(ndmin + 1), ndmin], [False, True])
                for axis, over in args:
                    try:
                        np.ma.median(x, axis=axis, overwrite_input=over)
                    except IndexError:
                        pass
                    else:
                        raise AssertionError(msg % (mask, ndmin, axis, over))
 def test_1d_shape_consistency(self):
     assert_equal(
         np.ma.median(array([1, 2, 3], mask=[0, 0, 0])).shape,
         np.ma.median(array([1, 2, 3], mask=[0, 1, 0])).shape)
 def test_dot_out(self):
     a = array(np.eye(3))
     out = array(np.zeros((3, 3)))
     res = dot(a, a, out=out)
     assert_(res is out)
     assert_equal(a, res)
    def test_compress_nd(self):
        # Tests compress_nd
        x = np.array(list(range(3 * 4 * 5))).reshape(3, 4, 5)
        m = np.zeros((3, 4, 5)).astype(bool)
        m[1, 1, 1] = True
        x = array(x, mask=m)

        # axis=None
        a = compress_nd(x)
        assert_equal(a,
                     [[[0, 2, 3, 4], [10, 12, 13, 14], [15, 17, 18, 19]],
                      [[40, 42, 43, 44], [50, 52, 53, 54], [55, 57, 58, 59]]])

        # axis=0
        a = compress_nd(x, 0)
        assert_equal(a,
                     [[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14],
                       [15, 16, 17, 18, 19]],
                      [[40, 41, 42, 43, 44], [45, 46, 47, 48, 49],
                       [50, 51, 52, 53, 54], [55, 56, 57, 58, 59]]])

        # axis=1
        a = compress_nd(x, 1)
        assert_equal(a, [
            [[0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]],
            [[20, 21, 22, 23, 24], [30, 31, 32, 33, 34], [35, 36, 37, 38, 39]],
            [[40, 41, 42, 43, 44], [50, 51, 52, 53, 54], [55, 56, 57, 58, 59]]
        ])

        a2 = compress_nd(x, (1, ))
        a3 = compress_nd(x, -2)
        a4 = compress_nd(x, (-2, ))
        assert_equal(a, a2)
        assert_equal(a, a3)
        assert_equal(a, a4)

        # axis=2
        a = compress_nd(x, 2)
        assert_equal(
            a,
            [[[0, 2, 3, 4], [5, 7, 8, 9], [10, 12, 13, 14], [15, 17, 18, 19]],
             [[20, 22, 23, 24], [25, 27, 28, 29], [30, 32, 33, 34],
              [35, 37, 38, 39]],
             [[40, 42, 43, 44], [45, 47, 48, 49], [50, 52, 53, 54],
              [55, 57, 58, 59]]])

        a2 = compress_nd(x, (2, ))
        a3 = compress_nd(x, -1)
        a4 = compress_nd(x, (-1, ))
        assert_equal(a, a2)
        assert_equal(a, a3)
        assert_equal(a, a4)

        # axis=(0, 1)
        a = compress_nd(x, (0, 1))
        assert_equal(a, [[
            [0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]
        ], [[40, 41, 42, 43, 44], [50, 51, 52, 53, 54], [55, 56, 57, 58, 59]]])
        a2 = compress_nd(x, (0, -2))
        assert_equal(a, a2)

        # axis=(1, 2)
        a = compress_nd(x, (1, 2))
        assert_equal(a,
                     [[[0, 2, 3, 4], [10, 12, 13, 14], [15, 17, 18, 19]],
                      [[20, 22, 23, 24], [30, 32, 33, 34], [35, 37, 38, 39]],
                      [[40, 42, 43, 44], [50, 52, 53, 54], [55, 57, 58, 59]]])

        a2 = compress_nd(x, (-2, 2))
        a3 = compress_nd(x, (1, -1))
        a4 = compress_nd(x, (-2, -1))
        assert_equal(a, a2)
        assert_equal(a, a3)
        assert_equal(a, a4)

        # axis=(0, 2)
        a = compress_nd(x, (0, 2))
        assert_equal(
            a,
            [[[0, 2, 3, 4], [5, 7, 8, 9], [10, 12, 13, 14], [15, 17, 18, 19]],
             [[40, 42, 43, 44], [45, 47, 48, 49], [50, 52, 53, 54],
              [55, 57, 58, 59]]])

        a2 = compress_nd(x, (0, -1))
        assert_equal(a, a2)
 def test_onintegers_with_mask(self):
     # Test average on integers with mask
     a = average(array([1, 2]))
     assert_equal(a, 1.5)
     a = average(array([1, 2, 3, 4], mask=[False, False, True, True]))
     assert_equal(a, 1.5)
Example #41
0
    # loop over parameters here

    experiment_dir = experiment_dir_base + os.sep + str(
        os.path.abspath(sys.argv[0])).split(os.sep)[-1].split(".")[0] + os.sep
    print "running experiments", n, "times at base", experiment_dir

    for i in range(n):
        distribution = Ring()

        mcmc_samplers = []

        kernel = GaussianKernel(sigma=1)
        #        mcmc_samplers.append(KameleonWindow(distribution, kernel))
        mcmc_samplers.append(KameleonWindowLearnScale(distribution, kernel))

        mean_est = array([-2.0, -2.0])
        cov_est = 0.05 * eye(2)
        #        mcmc_samplers.append(AdaptiveMetropolis(distribution, mean_est=mean_est, cov_est=cov_est))
        mcmc_samplers.append(
            AdaptiveMetropolisLearnScale(distribution,
                                         mean_est=mean_est,
                                         cov_est=cov_est))

        num_eigen = 2
        mcmc_samplers.append(
            AdaptiveMetropolisPCA(distribution,
                                  num_eigen=num_eigen,
                                  mean_est=mean_est,
                                  cov_est=cov_est))

        mcmc_samplers.append(StandardMetropolis(distribution))
Example #42
0
from numpy.lib.shape_base import tile
from numpy.ma.core import sum, array, arange
import operator


def knn(group, labels, test, k):
    tests = tile(test, (group.shape[0], 1))
    diff = group - tests
    diff_pow = diff * diff
    diff_sum = sum(diff_pow, axis=1)
    distance = diff_sum**0.5
    argsort = distance.argsort()
    print('distance:', distance)
    print('argsort:', argsort)
    classcount = {}
    for i in arange(k):
        label = labels[argsort[i]]
        print('label:' + label)
        classcount[label] = classcount.get(label, 0) + 1
    print(classcount)
    sortClasscount = sorted(classcount.items(), key=operator.itemgetter(1))
    print(sortClasscount)
    print('res:', sortClasscount[0][0])


group = array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]])
labels = ['A', 'A', 'B', 'B']
test = [0, 0]
k = 3
print(group.shape[0])
knn(group, labels, test, k)
Example #43
0
 def test_masked_0d(self):
     # Check values
     x = array(1, mask=False)
     assert_equal(np.ma.median(x), 1)
     x = array(1, mask=True)
     assert_equal(np.ma.median(x), np.ma.masked)
 def setUp(self):
     self.data = array(np.random.rand(12))
     self.data2 = array(np.random.rand(12))
Example #45
0
 def get_hyperparameters(self):
     return array([self.sigma, self.scale])
Example #46
0
        for v in xrange(len(vol)):
            print >> f, '%.3f,' % vol[v],
            for p in xrange(len(prob)):
                print >> f, '%.3f,' % function(statistic[p, v, :]),
            print >> f, '\n',


#POLICIES
path = 'data/varredura_0-4/statistics/'
lbdMean = loadVar(path, 'lbdMean')
lbdStd = loadVar(path, 'lbdStd')
meanSquareError = loadVar(path, 'meanSquareError')
rightEstimate = loadVar(path, 'rightEstimate')
rightPrediction = loadVar(path, 'rightPrediction')
rewardedTrials = loadVar(path, 'rewardedTrials')
prob = array([.55, .65, .75, .85, .95])
vol = array([0, .005, .01, .05])
lbd = [0.01, 0.11, 0.21, 0.31, 0.41, 0.51, 0.61, 0.71, 0.81, 0.91]

generateTableofStatistics(prob, vol, lbdMean, mean, path, 'tableLbdMean')
generateTableofStatistics(prob, vol, lbdMean, std, path,
                          'tableLbdStdMean-consistence')
generateTableofStatistics(prob, vol, lbdStd, mean, path,
                          'tableLbdMeanStd-convergence')
#generateTableofStatistics(prob, vol, meanSquareError,mean, path, 'tableMeanSquareError')
#generateTableofStatistics(prob, vol, rewardedTrials, mean,path,'tableRewardedTrials')
generateTableofStatistics(prob, vol, rightEstimate, mean, path,
                          'tableRightEstimate')
#generateTableofStatistics(prob, vol, rightPrediction,mean, path,'tableRightPrediction')

##CONSTANT LAMBDA
Example #47
0
        for i in range(m):
            #每次都降低alpha的大小
            alpha = 4 / (1.0 + j + i) + 0.01
            #随机选择样本
            randIndex = int(np.random.uniform(0, len(dataIndex)))
            #随机选择一个样本计算h
            h = sigmoid(sum(dataMatrix[randIndex] * weights))
            #计算误差
            error = classLabels[randIndex] - h
            #更新回归系数
            weights = weights + alpha * error * dataMatrix[randIndex]
            #删除已使用版本
            del (dataIndex[randIndex])
    return weights


if __name__ == "__main__":
    dataArr, labelMat = loadDataSet()
    weights = gradAscent(dataArr, labelMat)
    print(weights)

    plotBestFit(weights.getA())

    weights = stocGradAscent0(array(dataArr), labelMat)
    print(weights)
    plotBestFit(weights)

    weights = stocGradAscent1(array(dataArr), labelMat, 500)
    print(weights)
    plotBestFit(weights)