Exemple #1
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)
 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)
Exemple #3
0
def gmmEM(data, K, it,show=False,usekmeans=True):
    #data += finfo(float128).eps*100
    centroid = kmeans2(data, K)[0] if usekmeans else ((max(data) - min(data))*random_sample((K,data.shape[1])) + min(data))
    N = data.shape[0]
    gmm = GaussianMM(centroid)
    if show: gmm.draw(data)
    while it > 0:
        print it," iterations remaining"
        it = it - 1
        # e-step
        gausses = zeros((K, N), dtype = data.dtype)
        for k in range(0, K):
            gausses[k] = gmm.c[k]*mulnormpdf(data, gmm.mean[k], gmm.covm[k])
        sums = sum(gausses, axis=0)
        if count_nonzero(sums) != sums.size:
            raise "Divide by Zero"
        gausses /= sums
        # m step
        sg = sum(gausses, axis=1)
        if count_nonzero(sg) != sg.size:
            raise "Divide by Zero"
        gmm.c = ones(sg.shape) / N * sg
        for k in range(0, K):
            gmm.mean[k] = sum(data * gausses[k].reshape((-1,1)), axis=0) / sg[k]
            d = data - gmm.mean[k]
            d1 = d.transpose()*gausses[k]
            gmm.covm[k]=dot(d1,d)/sg[k]
        if show: gmm.draw(data)
    return gmm
Exemple #4
0
 def train(self, data, labels):
     l = labels.reshape((-1,1))
     xy = data * l
     H = dot(xy,transpose(xy))
     f = -1.0*ones(labels.shape)
     lb = zeros(labels.shape)
     ub = self.C * ones(labels.shape)
     Aeq = labels
     beq = 0.0
     p = QP(matrix(H),f.tolist(),lb=lb.tolist(),ub=ub.tolist(),Aeq=Aeq.tolist(),beq=beq)
     r = p.solve('cvxopt_qp')
     r.xf[where(r.xf<1e-3)] = 0
     self.w = dot(r.xf*labels,data)
     nonzeroindexes = where(r.xf>1e-4)[0]
     l1 = nonzeroindexes[0]
     self.w0 = 1.0/labels[l1]-dot(self.w,data[l1])
     self.numSupportVectors = len(nonzeroindexes)
 def test_matrix_product(self):
     A = array( [[1,1],
                 [0,1]] )
     B = array( [[2,0],
                 [3,4]] )
     
     C = dot(A,B)
     numpy.testing.assert_array_equal(C,array([[5, 4],
                                               [3, 4]]))
Exemple #6
0
 def __train__(self, data, labels):
     l = labels.reshape((-1,1))
     xy = data * l
     H = dot(xy,transpose(xy))
     f = -1.0*ones(labels.shape)
     lb = zeros(labels.shape)
     ub = self.C * ones(labels.shape)
     Aeq = labels
     beq = 0.0
     devnull = open('/dev/null', 'w')
     oldstdout_fno = os.dup(sys.stdout.fileno())
     os.dup2(devnull.fileno(), 1)
     p = QP(matrix(H),f.tolist(),lb=lb.tolist(),ub=ub.tolist(),Aeq=Aeq.tolist(),beq=beq)
     r = p.solve('cvxopt_qp')
     os.dup2(oldstdout_fno, 1)
     lim = 1e-4
     r.xf[where(r.xf<lim)] = 0
     self.w = dot(r.xf*labels,data)
     nonzeroindexes = where(r.xf>lim)[0]
     l1 = nonzeroindexes[0]
     self.w0 = 1.0/labels[l1]-dot(self.w,data[l1])
     self.numSupportVectors = len(nonzeroindexes)
 def test_simple_array_operations(self):
     a = array([[1.,2.],
                [3.,4.]])
     numpy.testing.assert_array_equal(a.transpose(), array([[1.,3.],
                                                            [2.,4.]]))
     
     numpy.testing.assert_array_almost_equal(trace(a), 5)
     
     inv_a = inv(a)
     b = array([[-2.,1.],
                [1.5,-.5]])
     
     self.assertTrue(numpy.allclose(inv_a,b))
     
     i = dot(a,inv_a)
     numpy.testing.assert_array_almost_equal(i, eye(2), 1)
     
     numpy.testing.assert_array_almost_equal(inv_a, b)
     # system of linear equations
     a = array([[3,2,-1],
                [2,-2,4],
                [-1,0.5,-1]])
     b = array([1,-2,0])
     
     c = solve(a,b)
     
     d = dot(a,c)
     numpy.testing.assert_array_almost_equal(b, d, 1)
     
     a = array([[.8,.3],
                [.2,.7]])
     eigen_values, eigen_vectors = eig(a)
     lambda_1 = eigen_values[0]
     x_1 = eigen_vectors[:,0]
     
     lambda_2 = eigen_values[1]
     x_2 = eigen_vectors[:,1]
 def test_dot(self):
     # Tests dot product
     n = np.arange(1, 7)
     #
     m = [1, 0, 0, 0, 0, 0]
     a = masked_array(n, mask=m).reshape(2, 3)
     b = masked_array(n, mask=m).reshape(3, 2)
     c = dot(a, b, strict=True)
     assert_equal(c.mask, [[1, 1], [1, 0]])
     c = dot(b, a, strict=True)
     assert_equal(c.mask, [[1, 1, 1], [1, 0, 0], [1, 0, 0]])
     c = dot(a, b, strict=False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, strict=False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     m = [0, 0, 0, 0, 0, 1]
     a = masked_array(n, mask=m).reshape(2, 3)
     b = masked_array(n, mask=m).reshape(3, 2)
     c = dot(a, b, strict=True)
     assert_equal(c.mask, [[0, 1], [1, 1]])
     c = dot(b, a, strict=True)
     assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [1, 1, 1]])
     c = dot(a, b, strict=False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     assert_equal(c, dot(a, b))
     c = dot(b, a, strict=False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     m = [0, 0, 0, 0, 0, 0]
     a = masked_array(n, mask=m).reshape(2, 3)
     b = masked_array(n, mask=m).reshape(3, 2)
     c = dot(a, b)
     assert_equal(c.mask, nomask)
     c = dot(b, a)
     assert_equal(c.mask, nomask)
     #
     a = masked_array(n, mask=[1, 0, 0, 0, 0, 0]).reshape(2, 3)
     b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2)
     c = dot(a, b, strict=True)
     assert_equal(c.mask, [[1, 1], [0, 0]])
     c = dot(a, b, strict=False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, strict=True)
     assert_equal(c.mask, [[1, 0, 0], [1, 0, 0], [1, 0, 0]])
     c = dot(b, a, strict=False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3)
     b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2)
     c = dot(a, b, strict=True)
     assert_equal(c.mask, [[0, 0], [1, 1]])
     c = dot(a, b)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, strict=True)
     assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [0, 0, 1]])
     c = dot(b, a, strict=False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3)
     b = masked_array(n, mask=[0, 0, 1, 0, 0, 0]).reshape(3, 2)
     c = dot(a, b, strict=True)
     assert_equal(c.mask, [[1, 0], [1, 1]])
     c = dot(a, b, strict=False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, strict=True)
     assert_equal(c.mask, [[0, 0, 1], [1, 1, 1], [0, 0, 1]])
     c = dot(b, a, strict=False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
Exemple #9
0
 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)
Exemple #10
0
 def test_dot(self):
     # Tests dot product
     n = np.arange(1, 7)
     #
     m = [1, 0, 0, 0, 0, 0]
     a = masked_array(n, mask=m).reshape(2, 3)
     b = masked_array(n, mask=m).reshape(3, 2)
     c = dot(a, b, strict=True)
     assert_equal(c.mask, [[1, 1], [1, 0]])
     c = dot(b, a, strict=True)
     assert_equal(c.mask, [[1, 1, 1], [1, 0, 0], [1, 0, 0]])
     c = dot(a, b, strict=False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, strict=False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     m = [0, 0, 0, 0, 0, 1]
     a = masked_array(n, mask=m).reshape(2, 3)
     b = masked_array(n, mask=m).reshape(3, 2)
     c = dot(a, b, strict=True)
     assert_equal(c.mask, [[0, 1], [1, 1]])
     c = dot(b, a, strict=True)
     assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [1, 1, 1]])
     c = dot(a, b, strict=False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     assert_equal(c, dot(a, b))
     c = dot(b, a, strict=False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     m = [0, 0, 0, 0, 0, 0]
     a = masked_array(n, mask=m).reshape(2, 3)
     b = masked_array(n, mask=m).reshape(3, 2)
     c = dot(a, b)
     assert_equal(c.mask, nomask)
     c = dot(b, a)
     assert_equal(c.mask, nomask)
     #
     a = masked_array(n, mask=[1, 0, 0, 0, 0, 0]).reshape(2, 3)
     b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2)
     c = dot(a, b, strict=True)
     assert_equal(c.mask, [[1, 1], [0, 0]])
     c = dot(a, b, strict=False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, strict=True)
     assert_equal(c.mask, [[1, 0, 0], [1, 0, 0], [1, 0, 0]])
     c = dot(b, a, strict=False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3)
     b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2)
     c = dot(a, b, strict=True)
     assert_equal(c.mask, [[0, 0], [1, 1]])
     c = dot(a, b)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, strict=True)
     assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [0, 0, 1]])
     c = dot(b, a, strict=False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
     #
     a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3)
     b = masked_array(n, mask=[0, 0, 1, 0, 0, 0]).reshape(3, 2)
     c = dot(a, b, strict=True)
     assert_equal(c.mask, [[1, 0], [1, 1]])
     c = dot(a, b, strict=False)
     assert_equal(c, np.dot(a.filled(0), b.filled(0)))
     c = dot(b, a, strict=True)
     assert_equal(c.mask, [[0, 0, 1], [1, 1, 1], [0, 0, 1]])
     c = dot(b, a, strict=False)
     assert_equal(c, np.dot(b.filled(0), a.filled(0)))
Exemple #11
0
 def __Mstep__(self):
     for k in range(0,self.K):
         self.c[k,:] = 1.0/self.N * sum(self.p,axis=1)
         self.mean[k,:] = sum(self.p*self.data, axis=1)/sum(self.p,axis=1)
         self.covm[k,:,:] = sum(self.p*dot(self.data - self.mean[k,:],transpose(self.data - self.mean[k,:])),axis=1)/sum(self.p,axis=1)
Exemple #12
0
 def classify(self,data,labels):
     res = dot(data,self.w)
     return getRootMeanSquaredErrors(res, labels)
Exemple #13
0
'''
Created on Sep 23, 2012

@author: will
'''
from numpy.core.numeric import arange, outer
from numpy.ma.extras import dot

if __name__ == '__main__':
    x = arange(10)
    print x
    print dot(x,x)
    print outer(x,x)
Exemple #14
0
 def __classify__(self, data):
     t = dot(data,self.w.reshape((-1,1))) + self.w0
     return t > 0
Exemple #15
0
 def __train__(self, data, labels):
     o=ones((data.shape[0],1))
     h=hstack((data, o))
     pseudoX=pinv(h)
     self.w=dot(pseudoX, labels.reshape((-1,1)))
Exemple #16
0
 def __classify__(self, data):
     t=dot(hstack((data, ones((data.shape[0],1)))),self.w)
     return t>0
 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)
Exemple #18
0
 def train(self,data,labels):
     self.w = dot(pinv(data.newbyteorder('=')),labels)
     self.trainingError = self.classify(data, labels)