Example #1
0
def mcosine(u, v):
    r"""
    Computes the Cosine distance between two n-vectors u and v, which
    is defined as

    .. math::

       \frac{1-uv^T}
            {||u||_2 ||v||_2}.

    Parameters
    ----------
    u : ndarray
        An :math:`n`-dimensional vector.
    v : ndarray
        An :math:`n`-dimensional vector.

    Returns
    -------
    d : double
        The Cosine distance between vectors ``u`` and ``v``.
    """
    u = ma.asarray(u, order='c')
    v = ma.asarray(v, order='c')
    return (1.0 - (ma.dot(u, v.T) / \
                   (ma.sqrt(ma.dot(u, u.T)) * ma.sqrt(ma.dot(v, v.T)))))
Example #2
0
def mmahalanobis(u, v, VI):
    r"""
    Computes the Mahalanobis distance between two n-vectors ``u`` and ``v``,
    which is defiend as

    .. math::

       (u-v)V^{-1}(u-v)^T

    where ``VI`` is the inverse covariance matrix :math:`V^{-1}`.

    Parameters
    ----------
    u : ndarray
        An :math:`n`-dimensional vector.
    v : ndarray
        An :math:`n`-dimensional vector.

    Returns
    -------
    d : double
        The Mahalanobis distance between vectors ``u`` and ``v``.
    """
    u = ma.asarray(u, order='c')
    v = ma.asarray(v, order='c')
    VI = ma.asarray(VI, order='c')
    return ma.sqrt(ma.dot(ma.dot((u - v), VI), (u - v).T).sum())
Example #3
0
    def train_batch(self, progressCallback=None):
        self.unit_distances = self.map.unit_distances()
        self.constant_matrix = 2 * ma.dot(numpy.eye(self.data.shape[1]),
                                          numpy.transpose(self.data))
        self.dist_cons = numpy.transpose(
            ma.dot(self.data**2, numpy.ones(self.data.shape[1])))
        self.weight_matrix = numpy.ones(
            (self.data.shape[1], self.data.shape[0]))
        self.vectors = self.map.vectors()

        ##        from pylab import plot, show, draw, ion
        ##        ion()
        ##        plot(self.data[:, 0], self.data[:, 1], "ro")
        ##        vec_plot = plot(self.vectors[:, 0], self.vectors[:, 1], "bo")[0]

        for epoch in range(self.epochs):
            self.train_step_batch(epoch)
            if progressCallback:
                progressCallback(100.0 * epoch / self.epochs)
            if False and epoch > 5 and numpy.mean(
                    numpy.abs(
                        numpy.array(self.qerror[-5:-1]) -
                        self.qerror[-1])) <= self.eps:
                break


##            vec_plot.set_xdata(self.vectors[:, 0])
##            vec_plot.set_ydata(self.vectors[:, 1])
##            draw()
##        show()

        for node, vector in zip(self.map, self.vectors):
            node.vector = vector
Example #4
0
def pca(data, nPCs = -1):
    domain = None
    
    suma = data.sum(axis=0)/float(len(data))
    data -= suma       # substract average value to get zero mean
    data /= MA.std(data, axis=0)
    covMatrix = MA.dot(data.T, data) / len(data)

    eigVals, eigVectors = linalg.eigh(covMatrix)
    eigVals = list(eigVals)
    
    if nPCs == -1:
        nPCs = len(eigVals)
    nPCs = min(nPCs, len(eigVals))
    
    pairs = [(val, i) for i, val in enumerate(eigVals)]
    pairs.sort()
    pairs.reverse()
    indices = [pair[1] for pair in pairs[:nPCs]]  # take indices of the wanted number of principal components

    vectors = MA.take(eigVectors, indices, axis = 1)
    values = [eigVals[i] for i in indices]
    projectedData = MA.dot(data, vectors)
    
    return projectedData, vectors, values
Example #5
0
    def _bin_column(self, weights, obs_name, err_name):

        from astropy.io.fits import Column

        data = self.data[obs_name]

        # spurious zero error replaced by the minimum error.  For
        # GRAVITY it only seems that's for flagged data anyway...
        error = self.data[err_name]
        error[error == 0] = error[error != 0].min()

        datamask = self.FLAG | np.isnan(data)
        data = ma.masked_array(data, mask=datamask)
        inverror2 = ma.masked_array(error**-2, mask=datamask)

        wsum = ma.dot(inverror2, weights)
        dsum = ma.dot(data * inverror2, weights)

        new_data = dsum / wsum
        new_error = wsum**-0.5

        nchan = new_data.shape[1]
        col = self.columns[obs_name]
        unit = col.unit
        new_fmt = f"{nchan}{col.format[-1]}"

        new_data = Column(obs_name, new_fmt, unit, array=new_data)
        new_error = Column(err_name, new_fmt, unit, array=new_error)

        return [new_data, new_error]
Example #6
0
 def __init__(self, gamma, k):
     LearningAlgorithm.__init__(self, gamma)
     self.kMeans = KMeans(n_clusters=k, init='random').fit(self.X).cluster_centers_
     self.Z = np.apply_along_axis(self.transformX, 1, self.X)
     self.w = dot(
                 dot(inv(dot(self.Z.T, self.Z)), self.Z.T),
                 self.y)
Example #7
0
def pca(data, nPCs=-1):
    domain = None

    suma = data.sum(axis=0) / float(len(data))
    data -= suma  # substract average value to get zero mean
    data /= MA.std(data, axis=0)
    covMatrix = MA.dot(data.T, data) / len(data)

    eigVals, eigVectors = linalg.eigh(covMatrix)
    eigVals = list(eigVals)

    if nPCs == -1:
        nPCs = len(eigVals)
    nPCs = min(nPCs, len(eigVals))

    pairs = [(val, i) for i, val in enumerate(eigVals)]
    pairs.sort()
    pairs.reverse()
    indices = [pair[1] for pair in pairs[:nPCs]
               ]  # take indices of the wanted number of principal components

    vectors = MA.take(eigVectors, indices, axis=1)
    values = [eigVals[i] for i in indices]
    projectedData = MA.dot(data, vectors)

    return projectedData, vectors, values
Example #8
0
File: som.py Project: Zekom/orange
    def train_batch(self, progress_callback=None):
        """Batch training algorithm.
        """
        
        self.unit_distances = self.map.unit_distances()
        self.constant_matrix = 2 * ma.dot(numpy.eye(self.data.shape[1]), numpy.transpose(self.data))
        self.dist_cons = numpy.transpose(ma.dot(self.data**2, numpy.ones(self.data.shape[1])))
        self.weight_matrix = numpy.ones((self.data.shape[1], self.data.shape[0]))
        self.vectors = self.map.vectors()
        
##        from pylab import plot, show, draw, ion
##        ion()
##        plot(self.data[:, 0], self.data[:, 1], "ro")
##        vec_plot = plot(self.vectors[:, 0], self.vectors[:, 1], "bo")[0]
        
        for epoch in range(self.epochs):
            self.train_step_batch(epoch)
            if progress_callback:
                progress_callback(100.0*epoch/self.epochs)
            if False and epoch > 5 and numpy.mean(numpy.abs(numpy.array(self.qerror[-5:-1]) - self.qerror[-1])) <= self.eps:
                break
##            vec_plot.set_xdata(self.vectors[:, 0])
##            vec_plot.set_ydata(self.vectors[:, 1])
##            draw()
##        show()
        
        for node, vector in zip(self.map, self.vectors):
            node.vector = vector
Example #9
0
def mcorrelation(u, v):
    r"""
    Computes the correlation distance between two n-vectors ``u`` and
    ``v``, which is defined as

    .. math::

       \frac{1 - (u - \bar{u}){(v - \bar{v})}^T}
            {{||(u - \bar{u})||}_2 {||(v - \bar{v})||}_2^T}

    where :math:`\bar{u}` is the mean of a vectors elements and ``n``
    is the common dimensionality of ``u`` and ``v``.

    Parameters
    ----------
    u : ndarray
        An :math:`n`-dimensional vector.
    v : ndarray
        An :math:`n`-dimensional vector.

    Returns
    -------
    d : double
        The correlation distance between vectors ``u`` and ``v``.
    """
    umu = u.mean()
    vmu = v.mean()
    um = u - umu
    vm = v - vmu
    return 1.0 - (ma.dot(um, vm) /
                  (ma.sqrt(ma.dot(um, um)) \
                   * ma.sqrt(ma.dot(vm, vm))))
Example #10
0
def cosine_similarity(v, w):
    # dot(v, v) : cos 1 = 1
    #             각 요소의 합성곱
    #             1 만으로 이루어진 벡터일 경우 벡터 자신의 1의 갯수 반환
    #             count(1)
    # 분모 = normalize factor : math.sqrt(dot(v, v) * dot(w, w))
    # 분자 = 같은 것의 갯수...단....여기서 벡터가 서로 다를 경우 cos factor < 1 포함
    # 유사할 수록 1에 근접하고, 멀어질 수록 0에 가까워 지므로....
    # 서로 다를 수록 0에 빠르게 수렴
    return dot(v, w) / math.sqrt(dot(v, v) * dot(w, w))
    def train(self, fitness_requirement):
        mean_solution = randn(self.problem_dimension)
        i = 0
        while self.fitness_metric.get_fitness(
                mean_solution) < fitness_requirement:
            print("Generation", i)
            print("mean solution's accuracy: %s" %
                  (str(self.fitness_metric.get_fitness(mean_solution))))
            print('\n\n===================================\n')
            sample_candidates = randn(self.sample_population_size,
                                      self.problem_dimension)
            jittered_samples_rewards = zeros(self.sample_population_size)

            for sample_index in range(self.sample_population_size):
                jittered_sample_candidate = mean_solution + (
                    self.noise_factor * sample_candidates[sample_index])
                jittered_samples_rewards[
                    sample_index] = self.fitness_metric.get_fitness(
                        jittered_sample_candidate)

            standardised_rewards = (jittered_samples_rewards - mean(jittered_samples_rewards)) \
                                   / std(jittered_samples_rewards)

            mean_solution = (
                mean_solution + self.learning_rate /
                (self.sample_population_size * self.noise_factor) *
                dot(sample_candidates.T, standardised_rewards))
            i += 1
        print(mean_solution)
        return mean_solution
Example #12
0
def correlation_map(pcs, field):
    """Correlation maps for a set of PCs and a spatial-temporal field.

    Given an array where the columns are PCs (e.g., as output from
    :py:meth:`eof2.EofSolve.pcs`) and an array containing a
    spatial-temporal where time is the first dimension, one correlation
    map per PC is computed.

    The field must have the same temporal dimension as the PCs. Any
    number of spatial dimensions (including zero) are allowed in the
    field and there can be any number of PCs.

    **Arguments:**

    *pcs*
        PCs as the columns of an array.

    *field*
        Spatial-temporal field with time as the first dimension.

    """
    # Check PCs and fields for validity, flatten the arrays ready for the
    # computation and remove the mean along the leading dimension.
    pcs_cent, field_cent, out_shape = _check_flat_center(pcs, field)
    # Compute the standard deviation of the PCs and the fields along the time
    # dimension (the leading dimension).
    pcs_std = pcs_cent.std(axis=0)
    field_std = field_cent.std(axis=0)
    # Set the divisor.
    div = np.float64(pcs_cent.shape[0])
    # Compute the correlation map.
    cor = ma.dot(field_cent.T, pcs_cent).T / div
    cor /= ma.outer(pcs_std, field_std)
    # Return the correlation with the appropriate shape.
    return cor.reshape(out_shape)
    def test_rotated_squares(self):
        poly1 = matrix([(0, 0), (10, 0), (10, 10), (0, 10), (0, 0), ])

        rot_mat = getRotationMatrix2D((5, 5), 45, 1.0)
        homogeneous_poly1 = vstack((transpose(poly1), ones((1, poly1.shape[0]))))

        poly2 = transpose(dot(rot_mat, homogeneous_poly1))

        new_poly = find_overlapping_polygon(poly1, poly2)

        ## plot the result
        # import matplotlib.pyplot as plt
        # from matplotlib.path import Path
        # import matplotlib.patches as patches
        # path = Path(new_poly)
        # fig = plt.figure()
        # ax = fig.add_subplot(111)
        # patch = patches.PathPatch(path, facecolor='orange', lw=2)
        # ax.add_patch(patch)
        # ax.set_xlim(-5,15)
        # ax.set_ylim(-5,15)
        # plt.show()

        # should be an octagon
        self.assertEqual(new_poly.shape[0], 9)
Example #14
0
def correlation_map(pcs, field):
    """Correlation maps for a set of PCs and a spatial-temporal field.

    Given an array where the columns are PCs (e.g., as output from
    :py:meth:`eof2.EofSolve.pcs`) and an array containing a
    spatial-temporal where time is the first dimension, one correlation
    map per PC is computed.

    The field must have the same temporal dimension as the PCs. Any
    number of spatial dimensions (including zero) are allowed in the
    field and there can be any number of PCs.

    **Arguments:**

    *pcs*
        PCs as the columns of an array.

    *field*
        Spatial-temporal field with time as the first dimension.

    """
    # Check PCs and fields for validity, flatten the arrays ready for the
    # computation and remove the mean along the leading dimension.
    pcs_cent, field_cent, out_shape = _check_flat_center(pcs, field)
    # Compute the standard deviation of the PCs and the fields along the time
    # dimension (the leading dimension).
    pcs_std = pcs_cent.std(axis=0)
    field_std = field_cent.std(axis=0)
    # Set the divisor.
    div = np.float64(pcs_cent.shape[0])
    # Compute the correlation map.
    cor = ma.dot(field_cent.T, pcs_cent).T / div
    cor /= ma.outer(pcs_std, field_std)
    # Return the correlation with the appropriate shape.
    return cor.reshape(out_shape)
Example #15
0
    def get_hand_util_table(self, board):
        '''
        
        Parameters
        ----------
        board : board cards specified as list of length 5 in range(52)

        Returns
        -------
        utils : 1326x1326 masked array of utils (int8)
        util specifies whether hand i wins, loses, or ties vs hand j. 
        invalid hands (where a hand is blocked by board cards, or the two 
        hands block each other) are masked

        '''
        converted = self._convert_board(board)
        board_eval = self.lookup[sum(converted[0][0])][converted[0].tobytes()]
        expanded_eval = ma.dot(
            board_eval,
            self.translate[len(board_eval)][converted[1]].toarray())
        utils = self._evals_to_util(expanded_eval)

        self.hm.mask_blocked_hands(utils)
        for card in board:
            utils[self.hm.card_removal[card]] = ma.masked
            utils[:, self.hm.card_removal[card]] = ma.masked
        return utils
Example #16
0
    def compute(self):
        if self.data == None:
            return
        if type(self.eigVectors) == MA.MaskedArray and type(
                self.eigValues) == MA.MaskedArray:
            return

        if type(self.data) == orange.ExampleTable:
            data, classes = self.data.toNumpyMA("a/c")
        elif type(self.data) == tuple:
            data, classes = self.data

        data = self.center(data)
        data = self.normalize(data)
        self.normalizedData = data
        exampleCount, attrCount = data.shape
        classCount = len(set(classes))
        # special case when we have two classes
        if classCount == 2:
            data1 = MA.take(data,
                            numpy.argwhere(classes == 0).flatten(),
                            axis=0)
            data2 = MA.take(data,
                            numpy.argwhere(classes != 0).flatten(),
                            axis=0)
            miDiff = MA.average(data1, axis=1) - MA.average(data2, axis=1)
            covMatrix = (MA.dot(data1.T, data1) +
                         MA.dot(data2.T, data2)) / exampleCount
            self.eigVectors = linalg.inv(covMatrix) * miDiff
            self.eigValues = numpy.array([1])
        else:
            # compute means and average covariances of examples in each class group
            Sw = MA.zeros([attrCount, attrCount])
            for v in set(classes):
                d = MA.take(data,
                            numpy.argwhere(classes == v).flatten(),
                            axis=0)
                d = self.center(d)
                Sw += MA.dot(d.T, d)
            Sw /= exampleCount
            total = MA.dot(data.T, data) / float(exampleCount)
            Sb = total - Sw

            matrix = linalg.inv(Sw) * Sb
            eigVals, eigVectors = linalg.eigh(matrix)
            self.eigValues, self.eigVectors = self.getSorted(
                eigVals, eigVectors)
Example #17
0
 def findEin(self):
     numberWrong = 0
     for index, z in enumerate(self.Z):
         y_expected = self.y[index]
         y = sign(dot(z, self.w.T))
         if y != y_expected:
             numberWrong += 1
     return numberWrong / 100.0
def newtons_minimizer(X, y, order, eps=0.000001):
    Z = polynomial_transform(X, order)
    x_temp, y_temp = array(Z[0:2, :]).transpose().shape

    theta = ones(shape=(x_temp, y_temp-1))
    theta_old = ones(shape=(x_temp, y_temp-1))
    theta_diff = ones(shape=(x_temp, y_temp-1))

    i = 0
    while not all(ele <= eps for ele in theta_diff):
        temp = dot(Z, theta) - y
        theta = theta_old - dot(pinv(Z), temp)
        theta_diff = abs(theta - theta_old)
        theta_old = theta
        i += 1
    # print "No. of Iterations : " + str(i)
    return theta
def relative_squared_error(y_predict, y_original):
    m, n = y_original.shape
    error = mean_squared_errors(y_predict, y_original)
    avg = ones(shape=y_original.shape) * average(y_original)
    temp = (y_original - avg)
    error /= dot(temp.transpose(), temp)
    error *= m
    return error
Example #20
0
def covariance(x, y):
    """
    协方差(covariance), 衡量两个变量对均值的串联偏离程度
    :param x:
    :param y:
    :return:
    """
    n = len(x)
    return dot(de_mean(x), de_mean(y)) / (n - 1)
Example #21
0
 def findEout(self, numSamples = 1000):
     e_out = 0
     dataSamples = [self.createDataPoint() for _ in range(numSamples)]
     for x,y_expected in dataSamples:
         z = np.asarray(self.transformX(x))
         y = sign(dot(self.w.T,z))
         if y != y_expected:
             e_out += 1
     e_out /= float(numSamples)
     return e_out
Example #22
0
	def _grad(self, maskedX ):
	# Gradient of objective function of alpha at unmasked values of X
	#
	# P dot alpha - q 		(calculated only at alpha - so full P not needed)
	#
	# @param maskedX	# an array of X masked at unneeded values
		
		P = self._P( np.vstack( [maskedX,]*self.kappa) ,np.vstack( [self.X,]*self.kappa ) )
		q =  self._q( np.vstack( [maskedX,]*self.kappa ) )
		
		return ma.dot( P, self.alpha ) - q
Example #23
0
	def _q( self, x ):
	# this is used to produce a [1xN] array
	
		#if self.q[x]:
		#	return self.q[x]
		
		if not x.shape[1] == self.d:
			raise StandardError, 'Arguments have incorrect shape - should be Nxd'
		
		#K = self._K( np.vstack([self.X,]*self.kappa) , x, self.Gamma )
		K = self._K( x, np.vstack([self.X,]*self.kappa), self.Gamma )
		
		print K.shape
		print ma.dot( K, np.vstack( [self.Y,]*self.kappa) ).shape
		
		#ret = ( self._Omega(self.Gamma) - ( ma.dot( K.T, np.vstack( [self.Y,]*self.kappa) ) ) )
		#NOTE: this MUST be fixed at some point
		ret = (  - ( ma.dot( K, np.vstack( [self.Y,]*self.kappa) ) ) ).T
		#self.q[x] = ret
		
		return ret
Example #24
0
File: som.py Project: Zekom/orange
    def train_step_batch(self, epoch):
        """A single step of batch training algorithm.
        """
        D1 = ma.dot(self.vectors**2, self.weight_matrix)
        D2 = ma.dot(self.vectors, self.constant_matrix)
        Dist = D1 - D2

        best_nodes = ma.argmin(Dist, 0)
        distances = ma.min(Dist, 0)
##        print "q error:", ma.mean(ma.sqrt(distances + self.dist_cons)), self.radius(epoch)
        self.qerror.append(ma.mean(ma.sqrt(distances + self.dist_cons)))

        if self.neighbourhood == Map.NeighbourhoodGaussian:        
            H = numpy.exp(-self.unit_distances**2/(2*self.radius(epoch)**2)) * (self.unit_distances**2 <= self.radius(epoch)**2)
        elif self.neighbourhood == Map.NeighbourhoodEpanechicov:
            H = 1.0 - (self.unit_distances/self.radius(epoch))**2
            H = H * (H >= 0.0)
        else:
            H = 1.0*(self.unit_distances <= self.radius(epoch))

        P =  numpy.zeros((self.vectors.shape[0], self.data.shape[0]))
        
        P[(best_nodes, range(len(best_nodes)))] = numpy.ones(len(best_nodes))
        
        S = ma.dot(H, ma.dot(P, self.data))
        
        A = ma.dot(H, ma.dot(P, ~self.data._mask))

##        nonzero = (range(epoch%2, len(self.vectors), 2), )
        nonzero = (numpy.array(sorted(set(ma.nonzero(A)[0]))), )
        
        self.vectors[nonzero] = S[nonzero] / A[nonzero]
Example #25
0
    def train_step_batch(self, epoch):
        D1 = ma.dot(self.vectors**2, self.weight_matrix)
        D2 = ma.dot(self.vectors, self.constant_matrix)
        Dist = D1 - D2

        best_nodes = ma.argmin(Dist, 0)
        distances = ma.min(Dist, 0)
        ##        print "q error:", ma.mean(ma.sqrt(distances + self.dist_cons)), self.radius(epoch)
        self.qerror.append(ma.mean(ma.sqrt(distances + self.dist_cons)))

        if self.neighbourhood == Map.NeighbourhoodGaussian:
            H = numpy.exp(-self.unit_distances / (2 * self.radius(epoch))) * (
                self.unit_distances <= self.radius(epoch))
        elif self.neighbourhood == Map.NeighbourhoodEpanechicov:
            H = 1.0 - (self.unit_distances / self.radius(epoch))**2
            H = H * (H >= 0.0)
        else:
            H = 1.0 * (self.unit_distances <= self.radius(epoch))

        P = numpy.zeros((self.vectors.shape[0], self.data.shape[0]))

        P[(best_nodes,
           list(range(len(best_nodes))))] = numpy.ones(len(best_nodes))

        S = ma.dot(H, ma.dot(P, self.data))

        A = ma.dot(H, ma.dot(P, ~self.data._mask))

        ##        nonzero = (range(epoch%2, len(self.vectors), 2), )
        nonzero = (numpy.array(sorted(set(ma.nonzero(A)[0]))), )

        self.vectors[nonzero] = S[nonzero] / A[nonzero]
    def test_two_overlapping_squares(self):
        poly1 = matrix([(0, 0), (10, 0), (10, 10), (0, 10), (0, 0), ])
        dx, dy = (5, 5)
        trans_mat = getTranslationMatrix2d(dx, dy)
        homogeneous_trans_mat = dot(trans_mat, vstack((transpose(poly1), ones((1, poly1.shape[0])))))
        poly2 = transpose(homogeneous_trans_mat[0:2, :])

        intersection = matrix([(5, 5), (5, 10), (10, 10), (10, 5), (5, 5), ])
        new_poly = find_overlapping_polygon(poly1, poly2)
        self.assertNotEqual(new_poly, None)
        self.assertTupleEqual(new_poly.shape, (5, 2))
        result = check_polygon_equality(new_poly, intersection)
        self.assertTrue(result)
Example #27
0
def dotd(A, B, out=None):
    """Diagonal of :math:`\mathrm A\mathrm B^\intercal`.
    If ``A`` is :math:`n\times p` and ``B`` is :math:`p\times n`, it is done in :math:`O(pn)`.
    Args:
        A (array_like): Left matrix.
        B (array_like): Right matrix.
        out (:class:`numpy.ndarray`, optional): copy result to.
    Returns:
        :class:`numpy.ndarray`: Resulting diagonal.
    """
    A = ma.asarray(A, float)
    B = ma.asarray(B, float)
    if A.ndim == 1 and B.ndim == 1:
        if out is None:
            return ma.dot(A, B)
        return ma.dot(A, B, out)

    if out is None:
        out = ma.empty((A.shape[0], ), float)

    out[:] = ma.sum(A * B.T, axis=1)
    return out
Example #28
0
    def compute(self):
        if self.data == None:
            return
        if type(self.eigVectors) == MA.MaskedArray and type(self.eigValues) == MA.MaskedArray:
            return
        
        if type(self.data) == orange.ExampleTable:
            data, classes = self.data.toNumpyMA("a/c")
        elif type(self.data) == tuple:
            data, classes = self.data

        data = self.center(data)
        data = self.normalize(data)
        self.normalizedData = data
        exampleCount, attrCount = data.shape
        classCount = len(set(classes))
        # special case when we have two classes
        if classCount == 2:
            data1 = MA.take(data, numpy.argwhere(classes == 0).flatten(), axis=0)
            data2 = MA.take(data, numpy.argwhere(classes != 0).flatten(), axis=0)
            miDiff = MA.average(data1, axis=1) - MA.average(data2, axis=1)
            covMatrix = (MA.dot(data1.T, data1) + MA.dot(data2.T, data2)) / exampleCount
            self.eigVectors = linalg.inv(covMatrix) * miDiff
            self.eigValues = numpy.array([1])
        else:
            # compute means and average covariances of examples in each class group
            Sw = MA.zeros([attrCount, attrCount])
            for v in set(classes):
                d = MA.take(data, numpy.argwhere(classes == v).flatten(), axis=0)
                d = self.center(d)
                Sw += MA.dot(d.T, d)
            Sw /= exampleCount
            total = MA.dot(data.T, data)/float(exampleCount)
            Sb = total - Sw
                        
            matrix = linalg.inv(Sw)*Sb
            eigVals, eigVectors = linalg.eigh(matrix)
            self.eigValues, self.eigVectors = self.getSorted(eigVals, eigVectors)
Example #29
0
def covariance_map(pcs, field, ddof=1):
    """Covariance maps for a set of PCs and a spatial-temporal field.

    Given an array where the columns are PCs (e.g., as output from
    `eofs.standard.Eof.pcs`) and an array containing spatial-temporal
    data where the first dimension represents time, one covariance map
    per PC is computed.

    The field must have the same temporal dimension as the PCs. Any
    number of spatial dimensions (including zero) are allowed in the
    field and there can be any number of PCs.

    **Arguments:**

    *pcs*
        PCs as the columns of an array.

    *field*
        Spatial-temporal field with time as the first dimension.

    **Optional arguments:**

    *ddof*
        'Delta degrees of freedom'. The divisor used to normalize
        the covariance matrix is *N - ddof* where *N* is the
        number of samples. Defaults to *1*.

    **Returns:**

    *covariance_maps*
        An array with the covariance maps along the first dimension.

    **Example:**

    Compute covariance maps for each PC::

        pcs = solver.pcs(pcscaling=1)
        covariance_maps = covariance_maps(pcs, field)

    """
    # Check PCs and fields for validity, flatten the arrays ready for the
    # computation and remove the mean along the leading dimension.
    pcs_cent, field_cent, out_shape = _check_flat_center(pcs, field)
    # Set the divisor according to the specified delta-degrees of freedom.
    div = np.float64(pcs_cent.shape[0] - ddof)
    # Compute the covariance map, making sure it has the appropriate shape.
    cov = (ma.dot(field_cent.T, pcs_cent).T / div).reshape(out_shape)
    cov = ma.masked_invalid(cov)
    return cov
Example #30
0
def covariance_map(pcs, field, ddof=1):
    """Covariance maps for a set of PCs and a spatial-temporal field.

    Given an array where the columns are PCs (e.g., as output from
    `eofs.standard.Eof.pcs`) and an array containing spatial-temporal
    data where the first dimension represents time, one covariance map
    per PC is computed.

    The field must have the same temporal dimension as the PCs. Any
    number of spatial dimensions (including zero) are allowed in the
    field and there can be any number of PCs.

    **Arguments:**

    *pcs*
        PCs as the columns of an array.

    *field*
        Spatial-temporal field with time as the first dimension.

    **Optional arguments:**

    *ddof*
        'Delta degrees of freedom'. The divisor used to normalize
        the covariance matrix is *N - ddof* where *N* is the
        number of samples. Defaults to *1*.

    **Returns:**

    *covariance_maps*
        An array with the covariance maps along the first dimension.

    **Example:**

    Compute covariance maps for each PC::

        pcs = solver.pcs(pcscaling=1)
        covariance_maps = covariance_maps(pcs, field)

    """
    # Check PCs and fields for validity, flatten the arrays ready for the
    # computation and remove the mean along the leading dimension.
    pcs_cent, field_cent, out_shape = _check_flat_center(pcs, field)
    # Set the divisor according to the specified delta-degrees of freedom.
    div = np.float64(pcs_cent.shape[0] - ddof)
    # Compute the covariance map, making sure it has the appropriate shape.
    cov = (ma.dot(field_cent.T, pcs_cent).T / div).reshape(out_shape)
    cov = ma.masked_invalid(cov)
    return cov
    def test_two_non_overlapping_squares(self):
        poly1 = matrix([(0, 0), (10, 0), (10, 10), (0, 10), (0, 0), ])
        dxs = [-10, 10, 0]
        dys = [-10, 10, 0]
        for dx in dxs:
            for dy in dys:
                if (dx, dy) == (0, 0):
                    continue

                trans_mat = getTranslationMatrix2d(dx, dy)
                homogeneous_trans_mat = dot(trans_mat, vstack((transpose(poly1), ones((1, poly1.shape[0])))))
                poly2 = transpose(homogeneous_trans_mat[0:2, :])

                new_poly = find_overlapping_polygon(poly1, poly2)
                self.assertEqual(new_poly, None)
    def test_two_non_overlapping_squares(self):
        poly1 = matrix([(0, 0), (10, 0), (10, 10), (0, 10), (0, 0), ])
        dxys = [(-8, 0), (0, 8), (8, 0), (0, -8)]

        for dx, dy in dxys:
            if (dx, dy) == (0, 0):
                continue

            trans_mat = getTranslationMatrix2d(dx, dy)
            homogeneous_trans_mat = dot(trans_mat, vstack((transpose(poly1), ones((1, poly1.shape[0])))))
            poly2 = transpose(homogeneous_trans_mat[0:2, :])

            new_poly = find_overlapping_polygon(poly1, poly2)
            area = find_polygon_area(new_poly)
            self.assertAlmostEqual(area, 2 * 10)
Example #33
0
def correlation_map(pcs, var_list):
    """DIFFERENT METHOD FOR CORRELATION MAPS - NO PVALUE
    Correlation maps for a set of PCs and a spatial-temporal field.
    Given an array where the columns are PCs and an array containing spatial-temporal
    data where the first dimension represents time, one correlation map
    per PC is computed.
    The field must have the same temporal dimension as the PCs. Any
    number of spatial dimensions (including zero) are allowed in the
    field and there can be any number of PCs.
    **Arguments:**
    *pcs*
        PCs as the columns of an array.
    *var_list*
        list of Spatial-temporal fields with time as the first dimension.
    
    **Returns:**
    *correlation_maps*
        An array with the correlation maps reshaped to the data array size.

    """
    ntime, neofs = pcs.shape
    ntim, nlat, nlon = var_list[0].shape

    ## Flatten data to [time x space]
    flat_var = flatten_array(var_list)

    field = flat_var[0]
    # remove the mean along the leading dimension.
    pcs_cent = pcs - pcs.mean(axis=0)
    field_cent = field - field.mean(axis=0)

    # Compute the standard deviation of the PCs and the fields along the time
    # dimension (the leading dimension).
    pcs_std = pcs_cent.std(axis=0)
    field_std = field_cent.std(axis=0)
    # Set the divisor.
    div = np.float64(pcs_cent.shape[0])
    # Compute the correlation map.
    cor = ma.dot(field_cent.T, pcs_cent).T / div
    cor = ma.masked_invalid(cor)
    # divide by std dev of pc * std dev of field
    cor /= ma.outer(pcs_std, field_std)

    # Reshape correlation results
    # Reshape spatial dim back to 2D map
    cormap = np.reshape(cor, (neofs, nlat, nlon))

    return cormap
Example #34
0
 def compute(self):
     if not self.data:
         return
     if type(self.eigVectors) == MA.MaskedArray and type(self.eigValues) == MA.MaskedArray:
         return
     
     if type(self.data) == orange.ExampleTable:
         data = self.data.toNumpyMA("a")[0]
     else:
         data = self.data
         
     data = self.center(data)
     data = self.normalize(data)
     self.normalizedData = data      # 
     
     covMatrix = MA.dot(data.T, data) / len(data)
     eigVals, eigVectors = linalg.eigh(covMatrix)
     self.eigValues, self.eigVectors = self.getSorted(eigVals, eigVectors)
Example #35
0
def write_word_occur_mat(AMC_TEST_INPUT_DIT, WORD_COOCCUR_DIR='./word_cooccur/'):
    '''
    从测试集中找出word共现矩阵并存档
    :param AMC_TEST_INPUT_DIT:
    :return:
    '''
    domain_filenames = listdir(AMC_TEST_INPUT_DIT)
    # print(domain_filenames)
    domain_filedirs = [path.join(AMC_TEST_INPUT_DIT, domain_filename) for domain_filename in domain_filenames]

    for domain_filedir, domain_filename in zip(domain_filedirs, domain_filenames):
        word_id = get_word_id(path.join(domain_filedir, domain_filename) + '.vocab')
        word_num = len(word_id)
        # print('word_num : ', word_num)  #first doc 103

        # 获取每个domain中doc_word的对应矩阵
        # print(join(domain_filedir, domain_filename + '.docs'))
        domain_file = open(path.join(domain_filedir, domain_filename + '.docs'))
        doc_num = len(domain_file.readlines())
        domain_file.seek(0)
        doc_word_mat = zeros([doc_num, word_num], dtype='line')  # int32
        for doc_id, line in enumerate(domain_file):
            # print(doc_id, domain_filenames[doc_id], line.strip().split(' '))
            for word in line.strip().split(' '):
                doc_word_mat[doc_id][int(word)] = 1
                # doc_word_mat[doc_id][int(word)] += 1
                # print(doc_id, int(word), doc_word_mat[doc_id][int(word)])
        domain_file.close()
        # print(doc_word_mat)

        # 将doc_word矩阵存入文件
        if not path.exists('./doc_word_mat'):
            makedirs('./doc_word_mat')
        doc_word_mat_file = open('./doc_word_mat/' + domain_filename + '_doc_word_mat_file.txt', 'wb')
        savetxt(doc_word_mat_file, doc_word_mat, fmt='%d')
        doc_word_mat_file.close()

        # 计算两个词之间的共现并写入文件
        word_cooccur = dot(doc_word_mat.transpose(), doc_word_mat)
        if not path.exists('./word_cooccur'):
            makedirs('./word_cooccur')
        word_cooccur_file = open(WORD_COOCCUR_DIR + domain_filename + '_word_cooccur_file.txt', 'wb')
        savetxt(word_cooccur_file, word_cooccur, fmt='%d')
        word_cooccur_file.close()
Example #36
0
    def compute(self):
        if not self.data:
            return
        if type(self.eigVectors) == MA.MaskedArray and type(
                self.eigValues) == MA.MaskedArray:
            return

        if type(self.data) == orange.ExampleTable:
            data = self.data.toNumpyMA("a")[0]
        else:
            data = self.data

        data = self.center(data)
        data = self.normalize(data)
        self.normalizedData = data  #

        covMatrix = MA.dot(data.T, data) / len(data)
        eigVals, eigVectors = linalg.eigh(covMatrix)
        self.eigValues, self.eigVectors = self.getSorted(eigVals, eigVectors)
def word2vec_distance(word1, word2, use_model=False, path="~/Data/googlenews-vectors.bin"):
    global model
    if not model:
        if use_model:
            print("Loading google news vectors")
            model = Word2Vec.load_word2vec_format(path, binary=True)
            print("Model loaded")
        else:
            model = json.load(open("../data/word_vectors.json"))

    if word1 not in model:
        # print word1, "not in model"
        return 0
    if word2 not in model:
        # print word2, "not in model"
        return 0
    vec1 = numpy.array(model[word1])
    vec2 = numpy.array(model[word2])
    return dot(gensim.matutils.unitvec(vec1), gensim.matutils.unitvec(vec2)).data
Example #38
0
def test_dotd_masked():
    random = RandomState(958)
    A = random.randn(3, 2)
    B = random.randn(2, 3)

    A = masked_array(A, mask=[[0, 0], [1, 0], [0, 0]])
    B = masked_array(B, mask=[[1, 0], [0, 0], [0, 1]])

    want = [-0.0047906181652413345, -0.30497335055401054, 0.10254886261295763]
    assert_allclose(dot(A, B).diagonal(), want)
    assert_allclose(dotd(A, B), want)

    a = random.randn(2)
    b = random.randn(2)

    a = masked_array(a, mask=[0, 1])
    b = masked_array(b, mask=[0, 1])

    assert_allclose(dotd(a, b), -0.06600230202137543)
Example #39
0
    def __init__(self, trajectory, vtheta=None, v1=1, v2=2):
        self.trajectory = trajectory
        self.t = self.trajectory.t
        self.badFrames = self.trajectory.badFrames
        self.posture = self.trajectory.getMaskedPosture(self.trajectory.posture)
        self.X = self.trajectory.getMaskedCentroid(self.trajectory.X)
        self.skeleton = ma.array(self.trajectory.skeleton[:, 1:-1, :])
        self.skeleton[~self.trajectory.orientationFixed, :, :] = ma.masked
        self.frameRate = self.trajectory.frameRate
        self.pixelsPerMicron = self.trajectory.pixelsPerMicron
        if vtheta is None:
            self.vtheta = self.trajectory.vtheta
        else:
            self.vtheta = vtheta
        self.v1 = v1
        self.v2 = v2
        self.postureProj = ma.dot(self.posture, self.vtheta[:, [v1,v2]])

        self.nPast = 115*6
        self.showBadWormImages = False
Example #40
0
def sqeuclidean(u, v, w=None):
    """
    """
    # Preserve float dtypes, but convert everything else to np.float64
    # for stability.
    utype, vtype = None, None
    if not (hasattr(u, "dtype") and np.issubdtype(u.dtype, np.inexact)):
        utype = np.float64
    if not (hasattr(v, "dtype") and np.issubdtype(v.dtype, np.inexact)):
        vtype = np.float64

    u = _validate_and_mask(u, dtype=utype)
    v = _validate_and_mask(v, dtype=vtype)

    u_v = u - v
    u_v_w = u_v  # only want weights applied once
    if w is not None:
        w = _validate_weights(w)
        u_v_w = w * u_v
    return ma.dot(u_v, u_v_w).data
Example #41
0
	def _P( self, A, B ):
	# this is used to compute one of two options: 
	# a 2x2 array defined by the working set
	# or a 2xN-2 array defined by the working set and all observations
	#
	# @param A 			[2xd] Array of observations
	# @param B			[Mxd] Array of observations
	
		#if self.P[A[0]][A[1]]:
		#	return self.Q[A[0]][A[1]]
		
		if not A.shape[1] == self.d or not B.shape[1] == self.d:
			raise StandardError, 'Arguments have incorrect shape - should be 2xd, Nxd'
			
		K = self._K( A, B, self.Gamma )
		
		ret = ma.dot(K.T,K)
		#self.P[A[0]][A[1]] = ret
		
		return ret
Example #42
0
    def __init__(self, trajectory, vtheta=None, v1=1, v2=2):
        self.trajectory = trajectory
        self.t = self.trajectory.t
        self.badFrames = self.trajectory.badFrames
        self.posture = self.trajectory.getMaskedPosture(
            self.trajectory.posture)
        self.X = self.trajectory.getMaskedCentroid(self.trajectory.X)
        self.skeleton = ma.array(self.trajectory.skeleton[:, 1:-1, :])
        self.skeleton[~self.trajectory.orientationFixed, :, :] = ma.masked
        self.frameRate = self.trajectory.frameRate
        self.pixelsPerMicron = self.trajectory.pixelsPerMicron
        if vtheta is None:
            self.vtheta = self.trajectory.vtheta
        else:
            self.vtheta = vtheta
        self.v1 = v1
        self.v2 = v2
        self.postureProj = ma.dot(self.posture, self.vtheta[:, [v1, v2]])

        self.nPast = 115 * 6
        self.showBadWormImages = False
Example #43
0
    def test_corrcoef(self):

        r = ma.masked_equal(np.load("data/ml-1m/rating.npy"), 0)
        # sim = ma.corrcoef(r[0], r[2412])
        # print(sim)

        # print(np.corrcoef(r[0].filled(0), r[2412].filled(0)))

        sim2 = ma.corrcoef(ma.vstack([r[0], r[2412]]))
        print(sim2)

        print(ma.dot(r[0], r[2412])/math.sqrt(ma.dot(r[0],r[0]))/math.sqrt(ma.dot(r[2412],r[2412])))

        r0_m = r[0] - ma.mean(r[0])
        r1_m = r[2412] - ma.mean(r[2412])
        print(ma.dot(r0_m, r1_m)/math.sqrt(ma.dot(r0_m,r0_m))/math.sqrt(ma.dot(r1_m,r1_m)))
Example #44
0
    def train(self, training_set_inputs, training_set_outputs,
              number_of_training_iterations):
        for iteration in range(number_of_training_iterations):
            # Pass the training set through our neural network (a single neuron).
            output = self.think(training_set_inputs)

            # Calculate the error (The difference between the desired output
            # and the predicted output).
            # print(training_set_outputs)
            # print(output)
            error = training_set_outputs - output
            # print(error)
            # print('----')

            # Multiply the error by the input and again by the gradient of the Sigmoid curve.
            # This means less confident weights are adjusted more.
            # This means inputs, which are zero, do not cause changes to the weights.
            adjustment = dot(training_set_inputs.T,
                             error * self.__sigmoid_derivative(output))

            # Adjust the weights.
            self.synaptic_weights += adjustment
Example #45
0
def dotd(A, B):
    r"""Diagonal of :math:`\mathrm A\mathrm B^\intercal`.

    If ``A`` is :math:`n\times p` and ``B`` is :math:`p\times n`, it is done in
    :math:`O(pn)`.

    Args:
        A (array_like): Left matrix.
        B (array_like): Right matrix.

    Returns:
        :class:`numpy.ndarray`: Resulting diagonal.
    """
    A = asarray(A, float)
    B = asarray(B, float)
    if A.ndim == 1 and B.ndim == 1:
        return dot(A, B)

    out = empty((A.shape[0],), float)
    out[:] = sum(A * B.T, axis=1)

    return out
Example #46
0
 def __cosine(a, b, norm_a, norm_b):
     return float(dot(a, b) / (norm_a * norm_b))
Example #47
0
def Beta_matrix_for_given_stage(sim_params, z, vz, stage):
    """constructs the B matrix, whose columns are
    the beta vectors (shape = N x 1) for each value
    of the generalized velocity for the advecting
    variable z.

    See DECSKS-09 part 1 for details of this calculation

    inputs:
    sim_params -- (dict) simulation parameters
    z.CFL.frac -- (ndarray, ndim=2) contains the fractional CFL numbers
                  for every [i,j]


    outputs:

    B -- (ndarray, ndim=2), shape = (N, vz.N)

            for x-advection: B.shape = (N, vx.N)
            for v-advection: B.shape = (N, x.N), note that vz.N = ax.N = x.N here
    """

    # for broadcasting operations below to an N x vz.N array
    # we require an identically dimensioned matrix. Hence, we slice the rows up to N values.
    # There is no loss of information here given that every row entry in the z.CFL.frac matrix
    # is constant, given z.CFL.frac = z.CFL.frac (vz.prepointvaluemesh)

    # Naming per DECSKS-09 notebook:
    #
    #        alpha = z.CFL.frac, shape: (z.N, vz.N)
    #        alpha_hat = truncated (or extended) z.CFL.frac[:N, :vz.N] up to N rows
    #                    note that all rows are identical so copying extra rows
    #                    should N > z.N (hence, z.CFL.frac.shape[0] = z.N) is
    #                    performed at no consequence or change in physics

    #        alpha_tilde : alpha_tilde[q,j] = alpha_hat ** q,q = 0, 1, ... N-1
    #

    # catch cases where N > z.N, need to append extra (N - z.N)
    # copies of any row (all the same) to the remaining rows of alpha_hat
    if z.N < sim_params['N']: # extend the alpha_hat matrix by (N - z.N) rows
        alpha_hat = np.zeros([sim_params['N'], z.CFL.frac[stage, :, :].shape[1]])

        # insert all z.CFL.frac[stage,:,:] rows into alpha_hat;
        alpha_hat[:z.N,:] = z.CFL.frac[stage, :, :]

        # since N > z.N, there are extra rows that need to be filled, store number
        # of extra rows beyond the extent of z.CFL.frac
        N_extra_rows = sim_params['N'] - z.CFL.frac[stage, :, :].shape[0]

        # generate the object of the required size to fit the remaining
        # (N_extra_rows by vz.N) submatrix of alpha_hat that needs to be
        # filled.

        # To do so, compute an outer product with any row of z.CFL.frac[stage,:,:]
        # (all rows i, z.CFL.frac[stage,i,:] are the same as the velocity spans
        # the columns j in z.CFL.frac[stage,i,j] and the CFL numbers depends on
        # the velocity, not on configuration. We copy the first row for
        # obviousness
        alpha_hat_extras = np.outer( np.ones(N_extra_rows), z.CFL.frac[stage, 0, :])
        alpha_hat[z.N:z.N + N_extra_rows, :] = alpha_hat_extras

    else: # z.N >= N, keep N rows for alpha_hat
        alpha_hat = z.CFL.frac[stage, :sim_params['N'], :vz.N]


    # see DECSKS-09 part 1, each vector beta depends on the sign of the CFL:
    #    beta_pos = A_pos.dot(z.CFL.frac[z.CFL.frac >= 0])
    #    beta_neg = A_neg.dot(z.CFL.frac[z.CFL.frac > 0])
    #
    # where A_pos and A_neg are stored arrays constructed in lib.read before
    # the simulation starts:

    # local copies of A matrices
    A_neg = sim_params['A_matrix']['-1']
    A_pos = sim_params['A_matrix']['1']

    # assembling alpha_tilde
    #     alpha_tilde[i,j] = alpha[i,j] ** (q+1) / (q+1)!,
    # where q = 0, 1, ..., N-1
    #
    # we perform the arithmetic operations point-wise via ufuncs
    # by using an array N_arr containg the values of q
    N_arr = np.outer(np.arange(1,sim_params['N']+1), np.ones([1,vz.N]))
    alpha_tilde = ma.array(alpha_hat ** N_arr / scipy.misc.factorial(N_arr))


    # create masks, note we must check the sign on alpha_hat = truncated or
    # expanded z.CFL.frac, we cannot check the sign on alpha_tilde as we have
    # modified the values by raising them to powers, which affects the sign
    mask_neg = (alpha_hat < 0)
    mask_pos = np.logical_not(mask_neg)

    # mask out negative values, leave only positives (>= 0)
    alpha_tilde.mask = mask_neg

    # operate on only positive values (>= 0)
    beta_pos = ma.dot(A_pos, alpha_tilde)

    # mask out positive (>= 0), leave only negatives
    alpha_tilde.mask = mask_pos

    # operate on only negative values
    beta_neg = ma.dot(A_neg, alpha_tilde)

    # consolidate all columns in a single matrix
    B = np.zeros([sim_params['N'], vz.N])

    # wherever beta_neg.mask is True (i.e. marks a negative entry), assign beta_neg, otherwise beta_pos
    B = np.where(mask_neg == True, beta_neg.data, beta_pos.data)

    return B
Example #48
0
 def getProjectedData(self, nPCs = None, varianceExplained = None):
     if not self.data: return None
     self.compute()
     
     vectors = self.getEigVectors(nPCs, varianceExplained)
     return MA.dot(self.normalizedData, vectors)
Example #49
0
def Beta_matrix(sim_params, z, vz):
    """constructs the B matrix, whose columns are
    the beta vectors (shape = N x 1) for each value
    of the generalized velocity for the advecting
    variable z.

    See DECSKS-09 part 1 for details of this calculation

    inputs:
    sim_params -- (dict) simulation parameters
    z.CFL.frac -- (ndarray, ndim=2) contains the fractional CFL numbers
                  for every [i,j]


    outputs:

    B -- (ndarray, ndim=2), shape = (N, vz.N)

            for x-advection: B.shape = (N, vx.N)
            for v-advection: B.shape = (N, x.N), note that vz.N = ax.N = x.N here
    """

    # local copies of A matrices
    A_neg = sim_params['A_matrix']['-1']
    A_pos = sim_params['A_matrix']['1']

    N_arr = np.outer(np.arange(1,sim_params['N']+1), np.ones([1,vz.N]))

    # for broadcasting operations below to an N x vz.N array
    # we require an identically dimensioned matrix. Hence, we slice the rows up to N values.
    # There is no loss of information here given that every row entry in the z.CFL.frac matrix
    # is constant, given z.CFL.frac = z.CFL.frac (vz.prepointvaluemesh)

    # Naming per DECSKS-09 notebook:
    #
    #        alpha = z.CFL.frac, shape: (z.N, vz.N)
    #        alpha_hat = truncated z.CFL.frac[:N, :vz.N], vz.N = z.CFL.frac.shape[1]
    #        alpha_tilde : alpha_tilde[q,j] = alpha_hat ** q,q = 0, 1, ... N-1
    #

    # catch cases where N > z.N, need to append extra (N - z.N)
    # copies of any row (all the same) to the remaining rows of alpha_hat
    if z.CFL.frac.shape[0] < sim_params['N']:
        alpha_hat = np.zeros([sim_params['N'], z.CFL.frac.shape[1]])
        alpha_hat[:z.CFL.frac.shape[0],:] = z.CFL.frac
        N_extra_rows = sim_params['N'] - z.CFL.frac.shape[0]

        # generate the object of the required size to fit the remaining submatrix
        # in alpha_hat not already filled, compute an outer product with any row (all same)
        # here, we take the first row for obviousness
        alpha_hat_extras = np.outer( np.ones(N_extra_rows), z.CFL.frac[0,:])
        alpha_hat[z.CFL.frac.shape[0]:z.CFL.frac.shape[0] + N_extra_rows,:] = alpha_hat_extras

    else:
        alpha_hat = z.CFL.frac[:sim_params['N'],:z.CFL.frac.shape[1]]

    alpha_tilde = ma.array(alpha_hat ** N_arr / scipy.misc.factorial(N_arr))

    mask_neg = (alpha_hat < 0)
    mask_pos = np.logical_not(mask_neg)

    # mask out negative values, leave only positives (>= 0)
    alpha_tilde.mask = mask_neg

    # operate on only positive values (>= 0)
    beta_pos = ma.dot(A_pos, alpha_tilde)

    # mask out positive (>= 0), leave only negatives
    alpha_tilde.mask = mask_pos

    # operate on only negative values
    beta_neg = ma.dot(A_neg, alpha_tilde)

    # consolidate all columns in a single matrix
    B = np.zeros([sim_params['N'], vz.N])

    # wherever beta_neg.mask is False (i.e. unmasked value), assign beta_neg, otherwise beta_pos
    B = np.where(mask_neg == True, beta_neg.data, beta_pos.data)

    return B
Example #50
0
def sum_of_squares(v):
    """ v_1 * v_1 + ... + v_n * v_n"""
    return dot(v, v)
def polynomial(w, x):
    powers = range(0,len(w))
    powers.reverse()

    powered = array(x)[:,newaxis] ** array(powers)
    return dot(powered, w)
Example #52
0
def Beta_matrix(sim_params, z, vz):
    """constructs the B matrix, whose columns are
    the beta vectors (shape = N x 1) for each value
    of the generalized velocity for the advecting
    variable z.

    See DECSKS-09 part 1 for details of this calculation

    inputs:
    sim_params -- (dict) simulation parameters
    z.CFL.frac -- (ndarray, ndim=2) contains the fractional CFL numbers
                  for every [i,j]


    outputs:

    B -- (ndarray, ndim=2), shape = (N, vz.N)

            for x-advection: B.shape = (N, vx.N)
            for v-advection: B.shape = (N, x.N), note that vz.N = ax.N = x.N here
    """

    # local copies of A matrices
    A_neg = sim_params['A_matrix']['-1']
    A_pos = sim_params['A_matrix']['1']

    N_arr = np.outer(np.arange(1,sim_params['N']+1), np.ones([1,vz.N]))

    # for broadcasting operations below to an N x vz.N array
    # we require an identically dimensioned matrix. Hence, we slice the rows up to N values.
    # There is no loss of information here given that every row entry in the z.CFL.frac matrix
    # is constant, given z.CFL.frac = z.CFL.frac (vz.prepointvaluemesh)

    # Naming per DECSKS-09 notebook:
    #
    #        alpha = z.CFL.frac, shape: (z.N, vz.N)
    #        alpha_hat = truncated z.CFL.frac[:N, :vz.N], vz.N = z.CFL.frac.shape[1]
    #        alpha_tilde : alpha_tilde[q,j] = alpha_hat ** q,q = 0, 1, ... N-1
    #

    # CRASH NOTICE: if the number of rows is not at least of size N
    # this will crash the simulation since python permits
    # indexing ranges beyond the final index in this limited case,
    # e.g. if A.shape = (5,), indexing A[:9] returns the entire
    # vector of size 5 without any error thrown. This is easily
    # corrected, but requires including conditional checks
    #
    #     if z.CFL.frac.shape[0] < N:
    #        alpha_hat = np.zeros((N, z.CFL.frac.shape[1]))
    #        alpha_hat[:z.CFL.frac.shape[0], :] = z.CFL.frac
    #
    #        # create duplicates for the extra rows needed
    #
    #        N_extra_rows = sim_params['N'] - z.CFL.frac.shape[0]
    #        alpha_hat[z.CFL.frac.shape[0]:N_extra_rows, :] = \
    #            z.CFL.frac[:N_extra_rows, :]
    #

    alpha_hat = z.CFL.frac[:sim_params['N'],:z.CFL.frac.shape[1]]
    alpha_tilde = ma.array(alpha_hat ** N_arr / scipy.misc.factorial(N_arr))

    mask_neg = (alpha_hat < 0)
    mask_pos = np.logical_not(mask_neg)

    # mask out negative values, leave only positives (>= 0)
    alpha_tilde.mask = mask_neg

    # operate on only positive vlaues (>= 0)
    beta_pos = ma.dot(A_pos, alpha_tilde)

    # mask out positive (>= 0), leave only negatives
    alpha_tilde.mask = mask_pos

    # operate on only negative values
    beta_neg = ma.dot(A_neg, alpha_tilde)

    # consolidate all columns in a single matrix
    B = np.zeros([sim_params['N'], vz.N])

    # wherever beta_neg.mask is False (i.e. unmasked value), assign beta_neg, otherwise beta_pos
    B = np.where(mask_neg == True, beta_neg.data, beta_pos.data)

    return B