Calculates similarity matrix by use of selected kernel function<br>
An n-by-m matrix is given and an n-by-n matrix is returned
555652
protected function calculateKernelMatrix(array $data, int $numRows): array
    {
        $kernelFunc = $this->getKernel();

        $matrix = [];
        for ($i = 0; $i < $numRows; ++$i) {
            for ($k = 0; $k < $numRows; ++$k) {
                if ($i <= $k) {
                    $matrix[$i][$k] = $kernelFunc($data[$i], $data[$k]);
                } else {
                    $matrix[$i][$k] = $matrix[$k][$i];
                }
            }
        }

        return $matrix;
    }
771452
def show_matrix(es, fs, t, a):
    '''
    
    '''

    max_a = viterbi_alignment(es, fs, t, a).items()
    m = len(es)
    n = len(fs)
    return matrix(m, n, max_a, es, fs)
1337119
def get_matrix(self):
        
        return np.array([ self.get_row_list(i) for i in range(self.row_count()) ])
985302
def _energy_distance_from_distance_matrices(
        distance_xx, distance_yy, distance_xy):
    """"""
    return (2 * np.mean(distance_xy) - np.mean(distance_xx) -
            np.mean(distance_yy))
881677
def modularity(matrix, clusters):
    
    matrix = convert_to_adjacency_matrix(matrix)
    m = matrix.sum()

    if isspmatrix(matrix):
        matrix_2 = matrix.tocsr(copy=True)
    else :
        matrix_2 = matrix

    if is_undirected(matrix):
        expected = lambda i,j : (( matrix_2[i,:].sum() + matrix[:,i].sum() )*
                                 ( matrix[:,j].sum() + matrix_2[j,:].sum() ))
    else:
        expected = lambda i,j : ( matrix_2[i,:].sum()*matrix[:,j].sum() )
    
    delta   = delta_matrix(matrix, clusters)
    indices = np.array(delta.nonzero())
    
    Q = sum( matrix[i, j] - expected(i, j)/m for i, j in indices.T )/m
    
    return Q
547836
def _safe_split(estimator, X, y, indices, train_indices=None):
    """"""
    if hasattr(estimator, 'kernel') and isinstance(estimator.kernel, collections.Callable):
        # cannot compute the kernel values with custom function
        raise ValueError("Cannot use a custom kernel function. "
                         "Precompute the kernel matrix instead.")

    if not hasattr(X, "shape"):
        if getattr(estimator, "_pairwise", False):
            raise ValueError("Precomputed kernels or affinity matrices have "
                             "to be passed as arrays or sparse matrices.")
        X_subset = [X[idx] for idx in indices]
    else:
        if getattr(estimator, "_pairwise", False):
            # X is a precomputed square kernel matrix
            if X.shape[0] != X.shape[1]:
                raise ValueError("X should be a square kernel matrix")
            if train_indices is None:
                X_subset = X[np.ix_(indices, indices)]
            else:
                X_subset = X[np.ix_(indices, train_indices)]
        else:
            X_subset = safe_indexing(X, indices)

    if y is not None:
        y_subset = safe_indexing(y, indices)
    else:
        y_subset = None

    return X_subset, y_subset
415181
def gaussian_kernel(sigma, truncate=4.0):
    

    sigma = float(sigma)
    radius = int(truncate * sigma + 0.5)

    x, y = np.mgrid[-radius:radius + 1, -radius:radius + 1]
    sigma = sigma ** 2

    k = 2 * np.exp(-0.5 * (x ** 2 + y ** 2) / sigma)
    k = k / np.sum(k)

    return k
350379
def K(self, X, X2=None):        
    # model :   -a d^2y/dx^2  + b dy/dt + c * y = U
    # kernel Kyy rbf spatiol temporal
    # vyt Y temporal variance  vyx Y spatiol variance   lyt Y temporal lengthscale   lyx Y spatiol lengthscale
    # kernel Kuu doper( doper(Kyy))
    # a   b    c    lyt   lyx    vyx*vyt
        """"""        
        X,slices = X[:,:-1],index_to_slices(X[:,-1])
        if X2 is None:
            X2,slices2 = X,slices
            K = np.zeros((X.shape[0], X.shape[0]))
        else:
            X2,slices2 = X2[:,:-1],index_to_slices(X2[:,-1])
            K = np.zeros((X.shape[0], X2.shape[0]))


        tdist = (X[:,0][:,None] - X2[:,0][None,:])**2
        xdist = (X[:,1][:,None] - X2[:,1][None,:])**2

        ttdist = (X[:,0][:,None] - X2[:,0][None,:])
        #rdist = [tdist,xdist]
        #dist = np.abs(X - X2.T)
        vyt = self.variance_Yt
        vyx = self.variance_Yx
        
        lyt=1/(2*self.lengthscale_Yt)
        lyx=1/(2*self.lengthscale_Yx)

        a = self.a ## -a is used in the model, negtive diffusion
        b = self.b
        c = self.c

        kyy = lambda tdist,xdist: np.exp(-lyt*(tdist) -lyx*(xdist))

        k1 = lambda tdist: (2*lyt - 4*lyt**2 * (tdist) )

        k2 = lambda xdist: ( 4*lyx**2 * (xdist)  - 2*lyx )

        k3 = lambda xdist: ( 3*4*lyx**2 - 6*8*xdist*lyx**3 + 16*xdist**2*lyx**4 )

        k4 = lambda ttdist: 2*lyt*(ttdist)

        for i, s1 in enumerate(slices):
            for j, s2 in enumerate(slices2):
                for ss1 in s1:
                    for ss2 in s2:
                        if i==0 and j==0:
                            K[ss1,ss2] = vyt*vyx*kyy(tdist[ss1,ss2],xdist[ss1,ss2])
                        elif i==0 and j==1:
                            K[ss1,ss2] = (-a*k2(xdist[ss1,ss2]) + b*k4(ttdist[ss1,ss2]) + c)*vyt*vyx*kyy(tdist[ss1,ss2],xdist[ss1,ss2])
                            #K[ss1,ss2]=  np.where(  rdist[ss1,ss2]>0 , kuyp(np.abs(rdist[ss1,ss2])), kuyn(np.abs(rdist[ss1,ss2]) )   )
                            #K[ss1,ss2]=  np.where(  rdist[ss1,ss2]>0 , kuyp(rdist[ss1,ss2]), kuyn(rdist[ss1,ss2] )   )
                        elif i==1 and j==1:
                            K[ss1,ss2] = ( b**2*k1(tdist[ss1,ss2]) - 2*a*c*k2(xdist[ss1,ss2]) + a**2*k3(xdist[ss1,ss2]) + c**2 )* vyt*vyx* kyy(tdist[ss1,ss2],xdist[ss1,ss2])
                        else:
                            K[ss1,ss2] = (-a*k2(xdist[ss1,ss2]) - b*k4(ttdist[ss1,ss2]) + c)*vyt*vyx*kyy(tdist[ss1,ss2],xdist[ss1,ss2])
                            #K[ss1,ss2]= np.where(  rdist[ss1,ss2]>0 , kyup(np.abs(rdist[ss1,ss2])), kyun(np.abs(rdist[ss1,ss2]) )   )
                            #K[ss1,ss2] = np.where(  rdist[ss1,ss2]>0 , kyup(rdist[ss1,ss2]), kyun(rdist[ss1,ss2] )   )
        
        #stop
        return K
610388
def asML(self):
        
        return newlinalg.DenseMatrix(self.numRows, self.numCols, self.values, self.isTransposed)
1361461
def eighCovarianceMatrix(cfile):
    
    # precompute eigenvalue decomposition
    K = np.loadtxt(cfile+'.cov')
    K+= 1e-4*sp.eye(K.shape[0])
    S,U = la.eigh(K); S=S[::-1]; U=U[:,::-1]
    np.savetxt(cfile+'.cov.eval',S,fmt='%.6f')
    np.savetxt(cfile+'.cov.evec',U,fmt='%.6f')
1302149
def weight_matrices(nn):
    

    if isinstance(nn, ndarray):
        return nn

    try:
        return weight_matrices(nn.connections)
    except:
        pass

    try:
        return weight_matrices(nn.module)
    except:
        pass

    # Network objects are ParameterContainer's too, but won't reshape into a single matrix,
    # so this must come after try nn.connections
    if isinstance(nn, (ParameterContainer, Connection)):
        return reshape(nn.params, (nn.outdim, nn.indim))

    if isinstance(nn, basestring):
        try:
            fn = nn
            nn = NetworkReader(fn, newfile=False)
            return weight_matrices(nn.readFrom(fn))
        except:
            pass
    # FIXME: what does NetworkReader output? (Module? Layer?) need to handle it's type here

    try:
        return [weight_matrices(v) for (k, v) in nn.iteritems()]
    except:
        try:
            connections = nn.module.connections.values()
            nn = []
            for conlist in connections:
                nn += conlist
            return weight_matrices(nn)
        except:
            return [weight_matrices(v) for v in nn]
738816
def lensNMF(data, k, ks=1):
    
    # TODO: why is this not working
    n_rounds = k/ks
    R_i = data.copy()
    nmf = NMF(ks)
    nmf2 = NMF(ks, init='custom')
    w_is = []
    h_is = []
    rs = []
    w_i = np.zeros((data.shape[0], ks))
    h_i = np.zeros((ks, data.shape[1]))
    for i in range(n_rounds):
        R_i = R_i - w_i.dot(h_i)
        R_i[R_i < 0] = 0
        """
        P_r = R_i.sum(1)/R_i.sum()
        print(P_r.shape)
        P_c = R_i.sum(0)/R_i.sum()
        print(P_c.shape)
        row_choice = np.random.choice(range(len(P_r)), p=P_r)
        print(row_choice)
        col_choice = np.random.choice(range(len(P_c)), p=P_c)
        print(col_choice)
        D_r = cosine_similarity(data[row_choice:row_choice+1,:], data)
        D_c = cosine_similarity(data[:,col_choice:col_choice+1].T, data.T)
        D_r = np.diag(D_r.flatten())
        D_c = np.diag(D_c.flatten())
        R_L = D_r.dot(R_i).dot(D_c)
        w_i = nmf.fit_transform(R_L)
        """
        w_i = nmf.fit_transform(R_i)
        h_i = nmf.components_
        #nmf2.fit_transform(R_i, W=w_i, H=nmf.components_)
        #h_i = nmf2.components_
        #h_i[h_i < 0] = 0
        w_is.append(w_i)
        h_is.append(h_i)
        rs.append(R_i)
    return np.hstack(w_is), np.vstack(h_is), rs
15440
def __similarity(s1, s2, ngrams_fn, n=3):
    
    ngrams1, ngrams2 = set(ngrams_fn(s1, n=n)), set(ngrams_fn(s2, n=n))
    matches = ngrams1.intersection(ngrams2)
    return 2 * len(matches) / (len(ngrams1) + len(ngrams2))
747371
def _get_kriging_matrix(self, n):
        """"""

        if self.coordinates_type == 'euclidean':
            xy = np.concatenate((self.X_ADJUSTED[:, np.newaxis],
                                 self.Y_ADJUSTED[:, np.newaxis]), axis=1)
            d = cdist(xy, xy, 'euclidean')
        elif self.coordinates_type == 'geographic':
            d = core.great_circle_distance(self.X_ADJUSTED[:,np.newaxis],
                                           self.Y_ADJUSTED[:,np.newaxis],
                                           self.X_ADJUSTED, self.Y_ADJUSTED)
        a = np.zeros((n+1, n+1))
        a[:n, :n] = - self.variogram_function(self.variogram_model_parameters,
                                              d)
        np.fill_diagonal(a, 0.)
        a[n, :] = 1.0
        a[:, n] = 1.0
        a[n, n] = 0.0

        return a
557344
def _get_zoom_mat(sw:float, sh:float, c:float, r:float)->AffineMatrix:
    ""
    return [[sw, 0,  c],
            [0, sh,  r],
            [0,  0, 1.]]
473852
def similarity_matrix(self, x_subjects=None, y_subjects=None, symmetric=False):
        
        if x_subjects is None:
            x_subjects = []
        if y_subjects is None:
            y_subjects = []

        xset = set(x_subjects)
        yset = set(y_subjects)
        zset = xset.union(yset)

        # first built map of gene->termClosure.
        # this could be calculated ahead of time for all g,
        # but this may be space-expensive. TODO: benchmark
        gmap={}
        for z in zset:
            gmap[z] = self.inferred_types(z)
        ilist = []
        for x in x_subjects:
            for y in y_subjects:
                if not symmetric or x<y:
                    shared = gmap[x].intersection(gmap[y])
                    union = gmap[x].union(gmap[y])
                    j = 0
                    if len(union)>0:
                        j = len(shared) / len(union)
                    ilist.append({'x':x,'y':y,'shared':shared, 'c':len(shared), 'j':j})
        return self.intersectionlist_to_matrix(ilist, x_subjects, y_subjects)
1000303
def get_matrices():
    
    with open('hwrt/misc/is_one_symbol_classifier.pickle', 'rb') as f:
        a = pickle.load(f)

    arrays = []
    for el1 in a.input_storage:
        for el2 in el1.__dict__['storage']:
            if isinstance(el2, cuda.CudaNdarray):
                arrays.append({'storage': numpy.asarray(el2),
                               'name': el1.name})
            else:
                logging.warning("was type %s. Do nothing." % type(el2))
                logging.debug(el1.name)
    return arrays