def resample(curr_samples,prob,afnv):
	dt = numpy.dtype('f8');
	nsamples = MA(curr_samples.shape).item(0)
	if prob.sum() ==0 :
		map_afnv = MA(numpy.ones(nsamples),dt)*afnv
		count = MA(numpy.zeros((prob.shape),dt))
	else:
		prob = prob/(prob.sum())
		count = MA(numpy.ceil(nsamples*prob),int)
		count = count.T
		map_afnv = MA(numpy.zeros((1,6)),dt);
	
		for i in range(nsamples):
			for j in range(count[i]):
				map_afnv = MA(numpy.concatenate((map_afnv,curr_samples[i,:]),axis=0),dt)
		
		K = map_afnv.shape[0];
		map_afnv = map_afnv[1:K,:];
		ns = count.sum()
		if nsamples > ns:
			map_afnv = MA(numpy.concatenate((map_afnv,MA(numpy.ones(((nsamples-ns),1))*afnv,dt)),axis=0),dt)
		
		map_afnv = map_afnv[0:nsamples,:]
	
	return map_afnv,count
Beispiel #2
0
def nipals1(x: np.matrix):

    max_it = 3
    # max_it = matrix_rank(x)
    # print("rank(x) = " + str(max_it))
    p = np.zeros(shape=(x.shape[1], max_it))
    t = np.zeros(shape=(x.shape[0], max_it))

    for j in range(0, max_it):
        # a = norm(x, axis=0)
        # idx = a.argmax()
        t_j = x[:, j]

        # # verify if the column is a zero vector
        # zero_col = np.zeros(shape=t_j.shape)
        # if norm(t_j - zero_col) <= epsilon:
        #     j += 1
        #     max_it += 1
        #     continue

        t_j1 = t_j - t_j
        while norm(t_j1 - t_j) > epsilon:
            p_j = x.transpose().dot(t_j)
            p_j /= norm(p_j)
            t_j1 = t_j
            t_j = x.dot(p_j)

        p[:, j] = p_j
        t[:, j] = t_j
        j += 1
        x = x - np.dot(t_j[:, None], p_j[None, :])

    return t, p
Beispiel #3
0
def eigen(matx: np.matrix, eps=1e-3):
    """
    функція знаходження власного числа і вектора методом скалярних добутків
    """
    tr_matrix = matx.transpose()
    y = np.zeros(matx.shape[0])
    z = np.zeros(matx.shape[0])
    y[0] = START_Y
    z[0] = y[0]
    eigenvalue = 0
    for j in range(ITERATION_LIMIT):
        if y.shape[0] == 1:
            y = y.transpose()
        if z.shape[0] == 1:
            z = z.transpose()
        next_y = np.array(matx.dot(y))
        next_z = np.array(tr_matrix.dot(z))

        tmp1 = sum1(next_y * next_z)
        tmp2 = sum1(y * next_z)

        tmp_res = tmp1 / tmp2
        if j == 0:
            eigenvalue = tmp_res
        elif abs(eigenvalue - tmp_res) < eps:
            break
        else:
            eigenvalue = tmp_res
        y = next_y
        z = next_z

    eigenvector = y / norm(y)
    return eigenvalue, eigenvector
def dump_dataset(dataset: np.matrix, description: dict) -> None:
    np.savetxt('caloria_dataset.csv', dataset, delimiter=',')

    with open('caloria_description.json', 'w') as desc_file:
        json.dump(description, desc_file)

    dataset.dump('caloria_xy_matrix.np')
Beispiel #5
0
def resample(curr_samples, prob, afnv):
    dt = numpy.dtype('f8')
    nsamples = MA(curr_samples.shape).item(0)
    if prob.sum() == 0:
        map_afnv = MA(numpy.ones(nsamples), dt) * afnv
        count = MA(numpy.zeros((prob.shape), dt))
    else:
        prob = prob / (prob.sum())
        count = MA(numpy.ceil(nsamples * prob), int)
        count = count.T
        map_afnv = MA(numpy.zeros((1, 6)), dt)
        for i in range(nsamples):
            for j in range(count[i]):
                map_afnv = MA(
                    numpy.concatenate((map_afnv, curr_samples[i, :]), axis=0),
                    dt)
        K = map_afnv.shape[0]
        map_afnv = map_afnv[1:K, :]
        ns = count.sum()
        if nsamples > ns:
            map_afnv = MA(
                numpy.concatenate(
                    (map_afnv, MA(numpy.ones(
                        ((nsamples - ns), 1)) * afnv, dt)),
                    axis=0), dt)
        map_afnv = map_afnv[0:nsamples, :]
    return map_afnv, count
Beispiel #6
0
    def _agg_apply(self, matrix: np.matrix, agg: str, axis: int) -> np.ndarray:
        """
        Apply aggregate function onto matrix along specified axis. By default,
        COO matrices support sum, mean, min, max methods ("built-in"). For other
        operations("derived"), formula out of built-in methods is used.
        """

        agg_axis = abs(1 - axis)  # to aggregate opposite axis

        if agg in self._SUPPORTED_AGG_FUNCS["built-in"]:
            agg_func = getattr(matrix, agg)
            rna_agg_out = agg_func(axis=agg_axis)
        elif agg in self._SUPPORTED_AGG_FUNCS["derived"]:
            if agg == "nonzero":
                rna_agg_out = (matrix > 0).sum(agg_axis)
            else:
                # TODO: might run out of memory because there is conversion to numpy matrix in agg funcs
                rna_var = matrix.power(2).mean(axis=agg_axis) - np.power(
                    matrix.mean(axis=agg_axis), 2)
                rna_agg_out = np.sqrt(
                    rna_var) if agg == "std" else rna_var  # std is sqrt(var)
        else:
            raise NotImplementedError(
                f'aggregation function "{agg}" not supported, valid options are: {self._SUPPORTED_AGG_FUNCS["all"]}'
            )
        rna_agg = np.ravel(rna_agg_out.sum(axis=agg_axis))  # flatten matrix
        return rna_agg
Beispiel #7
0
def affparaminv(est):
    #!!!pay attention
    dt = np.dtype('f8')
    q = MA(np.zeros(2,3),dt)
    q = linalg.pinv(MA([[est[0,2],est[0,3]],[est[0,4],est[0,5]]])) * MA([[-est[0,0],1.0,0.0],[-est[0,1],0,1.0]])
    q=q.flatten(1)
    return MA([[q[0,0],q[0,1],q[0,2],q[0,4],q[0,3],q[0,5]]])
Beispiel #8
0
def floyd_warshall(W: np.matrix, print_k: bool = False):
    W = np.copy(W)
    P = create_P(W)
    n = len(W)

    print("starting P")
    print(f"{P=}")

    for k in range(1, n + 1):
        for i in range(1, n + 1):
            for j in range(1, n + 1):
                current = W.item((i - 1, j - 1))
                candidate = W.item((i - 1, k - 1)) + W.item((k - 1, j - 1))
                if current > candidate:
                    debug and print(
                        f"Currently doing {i} -> {j} " +
                        f"for a cost of {current} " +
                        f"but we can do better going through {k} " +
                        f"for a cost of {candidate} instead. "
                        f"current value at {P.item((k - 1, j - 1))=}")
                    W.itemset((i - 1, j - 1), candidate)
                    P.itemset((i - 1, j - 1), P.item((k - 1, j - 1)))

        if print_k:
            print(f"for {k=}")
            print(f"{W=}")
            print(f"{P=}")

    return W, P
Beispiel #9
0
    def batch_update(self, x: np.matrix, chosen_arm: int, reward: float) -> None:
        """Update the information about the arms with a new batch of data.

        :param x: observed context matrix.
        :param chosen_arm: index of the chosen arm.
        :param reward: reward from the chosen arm.
        """
        self.data_size += 1
        z = x[:][:self.z_dim]
        x = x[:][self.z_dim:]

        self.counts[chosen_arm] += 1
        self.rewards += reward
        self._A_zero += self._B[chosen_arm].T.dot(self._A_inv[chosen_arm]).dot(self._B[chosen_arm])
        self._b_zero += self._B[chosen_arm].T.dot(self._A_inv[chosen_arm]).dot(self._b[chosen_arm])
        self._A_inv[chosen_arm] -= self._A_inv[chosen_arm].dot(x.dot(x.T.dot(self._A_inv[chosen_arm]))) / (1 + x.T.dot(self._A_inv[chosen_arm].dot(x)))
        self._B[chosen_arm] += x.dot(z.T)
        self._b[chosen_arm] += x * reward
        self._A_zero += z.dot(z.T) - self._B[chosen_arm].T.dot(self._A_inv[chosen_arm]).dot(self._B[chosen_arm])
        self._b_zero += z * reward - self._B[chosen_arm].T.dot(self._A_inv[chosen_arm]).dot(self._b[chosen_arm])

        if self.data_size % self.batch_size == 0:
            self.A_zero = self._A_zero[:]
            self.b_zero = self._b_zero[:]
            self.A_inv = copy.deepcopy(self._A_inv)
            self.B = copy.deepcopy(self._B)
            self.b = copy.deepcopy(self._b)
Beispiel #10
0
    def find_tfrom_between_shapes(from_shape: np.matrix, 
                                  to_shape: np.matrix
                                  ) -> Tuple[np.matrix]:
        """
        Find transform between shapes

        Parameters:
        ----------
        :param from_shape: type(numpy.matrix
            Input shape
        :param to_shape: type(numpy.matrix
            Output shape
        :return: tran_m - transformation matrix, tran_b - transformation matrix
        """
        assert from_shape.shape[0] == to_shape.shape[0] and from_shape.shape[0] % 2 == 0

        sigma_from = 0.0
        sigma_to = 0.0
        cov = np.matrix([[0.0, 0.0], [0.0, 0.0]])

        # compute the mean and cov
        from_shape_points = from_shape.reshape(from_shape.shape[0] // 2, 2)
        to_shape_points = to_shape.reshape(to_shape.shape[0] // 2, 2)
        mean_from = from_shape_points.mean(axis=0)
        mean_to = to_shape_points.mean(axis=0)

        for i in range(from_shape_points.shape[0]):
            temp_dis = np.linalg.norm(from_shape_points[i] - mean_from)
            sigma_from += temp_dis * temp_dis
            temp_dis = np.linalg.norm(to_shape_points[i] - mean_to)
            sigma_to += temp_dis * temp_dis
            cov += (to_shape_points[i].transpose() - mean_to.transpose()) * (
                from_shape_points[i] - mean_from
            )

        sigma_from = sigma_from / to_shape_points.shape[0]
        sigma_to = sigma_to / to_shape_points.shape[0]
        cov = cov / to_shape_points.shape[0]

        # compute the affine matrix
        s = np.matrix([[1.0, 0.0], [0.0, 1.0]])
        u, d, vt = np.linalg.svd(cov)

        if np.linalg.det(cov) < 0:
            if d[1] < d[0]:
                s[1, 1] = -1
            else:
                s[0, 0] = -1
        r = u * s * vt
        c = 1.0
        if sigma_from != 0:
            c = 1.0 / sigma_from * np.trace(np.diag(d) * s)

        tran_b = mean_to.transpose() - c * r * mean_from.transpose()
        tran_m = c * r

        return tran_m, tran_b
def precision_calc(resample: np.matrix):
    """
    Calculate precision score from matrix. Assume the first column to be y_true and the second column to be y_pred
    Args:
        resample: matrix with 2 columns [y_true, y_pred]

    Returns:
        precision score: float
    """
    return precision_score(resample.tolist()[0], resample.tolist()[1])
Beispiel #12
0
def multiply_vecmat(vec: vec3d, mat: np.matrix) -> vec3d:
	if mat.shape[0] == 1:
		mat=mat.tolist()[0]
	else:
		mat=mat.tolist()
	return vec3d(
		vec.x*mat[0][0]+vec.y*mat[1][0]+vec.z*mat[2][0]+vec.w*mat[3][0], 
		vec.x*mat[0][1]+vec.y*mat[1][1]+vec.z*mat[2][1]+vec.w*mat[3][1], 
		vec.x*mat[0][2]+vec.y*mat[1][2]+vec.z*mat[2][2]+vec.w*mat[3][2],
		vec.x*mat[0][3]+vec.y*mat[1][3]+vec.z*mat[2][3]+vec.w*mat[3][3])
Beispiel #13
0
def binary(img1, level):
	img1 = MA(img1)
	tempIm = img1.flatten(1)
	for i in range(tempIm.shape[1]):
		if tempIm[0,i] < level:
			tempIm[0,i] = 0;
		else:
			tempIm[0,i] = 1;
	tempIm = (numpy.reshape(tempIm,(img1.shape[1],img1.shape[0]))).T
	return tempIm
 def __init__(self, vals: np.matrix):
     self.shift_factor = np.array(vals.min(1)).astype(float)
     self.scale_factor = np.array(vals.max(1) -
                                  self.shift_factor).astype(float)
     self.scale_factor = self.scale_factor + (self.scale_factor
                                              == 0).astype(float)
     self.shift_factor = self.shift_factor.reshape(
         self.shift_factor.shape[0], 1)
     self.scale_factor = self.scale_factor.reshape(
         self.scale_factor.shape[0], 1)
     self.scale_factor = 1.0
Beispiel #15
0
def get_matrix_cost(matrix: np.matrix) -> np.matrix:
    cost_matrix = matrix.copy()
    minimal_column = matrix.min(1)[:np.newaxis]
    minimal_column[minimal_column == np.inf] = 0
    # Subtract minimal value of a row from matrix
    cost_matrix -= minimal_column
    minimal_row = cost_matrix.min(0)
    minimal_row[minimal_row == np.inf] = 0
    # Subtract minimal value of a column from matrix
    cost_matrix -= minimal_row
    return cost_matrix
Beispiel #16
0
  def get_r_squared(model: Linear_Regression, X: np.matrix, y: np.matrix) -> float:
    nominator: float = 0
    denominator: float = 0
    y_list: List[List[float]] = y.tolist()
    y_avg: float = np.average(y_list)

    for idx, x in enumerate(X.tolist()):
      nominator += (model.fit(x[:4]) - y_list[idx][0]) ** 2
      denominator += (y_avg - y_list[idx][0]) ** 2

    return 1 - (nominator/denominator)
Beispiel #17
0
def matrix_3d_to_4x4(matrix: np.matrix) -> np.matrix:
    """Calculates the 3d 4x4 affine transformation matrix from the given 3d 3x3 affine rotation matrix"""
    return np.matrix(
        [[matrix.item(0, 0),
          matrix.item(0, 1),
          matrix.item(0, 2), 0],
         [matrix.item(1, 0),
          matrix.item(1, 1),
          matrix.item(1, 2), 0],
         [matrix.item(2, 0),
          matrix.item(2, 1),
          matrix.item(2, 2), 0], [0, 0, 0, 1]])
def getStochasticMatrix(inputMatrix: matrix,
                        normZeros: bool = False) -> matrix:
    columnSums = asarray(inputMatrix.sum(0)).reshape(-1)
    if normZeros:
        for i in range(len(columnSums)):
            columnSum: float = columnSums[i]
            if columnSum == 0:
                inputMatrix[:, i] = 1
        return inputMatrix / inputMatrix.sum(0)
    else:
        columnSums[columnSums == 0] = 1
        return inputMatrix / columnSums
Beispiel #19
0
def binary(img1, level):
	img1 = MA(img1)
	#import pdb;pdb.set_trace()
	tempIm = img1.flatten(1)
	for i in range(tempIm.shape[1]):
		if tempIm[0,i] < level:
			tempIm[0,i] = 0;
		else:
			tempIm[0,i] = 1;
	tempIm = (numpy.reshape(tempIm,(img1.shape[1],img.shape[0]))).T
	print tempIm
	return tempIm
def binary(img1, level):
    img1 = MA(img1)
    #import pdb;pdb.set_trace()
    tempIm = img1.flatten(1)
    for i in range(tempIm.shape[1]):
        if tempIm[0, i] < level:
            tempIm[0, i] = 0
        else:
            tempIm[0, i] = 1
    tempIm = (numpy.reshape(tempIm, (img1.shape[1], img.shape[0]))).T
    print tempIm
    return tempIm
Beispiel #21
0
def matrix_2d_to_3d(matrix: np.matrix) -> np.matrix:
    """Calculates the 3d affine transformation matrix from the given 2d affine transformation matrix"""
    return np.matrix(
        [[matrix.item(0, 0),
          matrix.item(0, 1), 0,
          matrix.item(0, 2)],
         [matrix.item(1, 0),
          matrix.item(1, 1), 0,
          matrix.item(1, 2)], [0, 0, 1, 0],
         [matrix.item(2, 0),
          matrix.item(2, 1), 0,
          matrix.item(2, 2)]])
Beispiel #22
0
def naive_multiplication(m0: np.matrix, m1: np.matrix):
    shape_m0 = m0.shape
    shape_m1 = m1.shape
    m01 = np.zeros((shape_m0[0], shape_m1[1]))
    if shape_m0[1] != shape_m1[0]:
        raise ValueError("Dimensions not aligned : dim0(m0)=" +
                         str(shape_m0[1]) + " and dim1(m1)=" +
                         str(shape_m1[0]))
    for i in np.arange(shape_m0[0]):
        for j in np.arange(shape_m1[1]):
            for k in np.arange(shape_m1[0]):
                m01[i][j] += m0.item((i, k)) * m1.item((k, j))
    return m01
Beispiel #23
0
def get_probability_transition_matrix(A: np.matrix):
    """
    Calcule la matrice de probabilité de transition à partir de la matrice d'adjacence
    :param A: La matrice de base
    :return: La matrice de probabilités de transition
    """
    res = []
    for i in range(A.__len__()):
        res.append([0] * A.__len__())
    do = A.sum(axis=1)
    for i in range(A.__len__()):
        res[i] = (A[i] / float(do.item(i))).A1
    return np.matrix(res)
Beispiel #24
0
    def setRotation(cls, m: np.matrix, rotation: list):

        rmat = quaternion_matrix(rotation)

        m.itemset(0, 0, rmat.item(0, 0))
        m.itemset(0, 1, rmat.item(0, 1))
        m.itemset(0, 2, rmat.item(0, 2))
        m.itemset(1, 0, rmat.item(1, 0))
        m.itemset(1, 1, rmat.item(1, 1))
        m.itemset(1, 2, rmat.item(1, 2))
        m.itemset(2, 0, rmat.item(2, 0))
        m.itemset(2, 1, rmat.item(2, 1))
        m.itemset(2, 2, rmat.item(2, 2))
Beispiel #25
0
def encryptBlock(data: np.matrix, key: np.matrix) -> np.matrix:
    encoded = data.getT() ^ key.getT()
    keys = extendKey(key)
    for i in range(1, len(keys)):
        encoded = bytesSub(encoded)
        # print("{} After subst     {}".format(i, encoded.flatten()))
        encoded = shiftRows(encoded)
        # print("{} After shift row {}".format(i, encoded.flatten()))
        if i < len(keys) - 1:
            encoded = mixColumns(encoded)
            # print("{} After  mix      {}".format(i, encoded.flatten()))
        # print("Applying key {}".format(keys[i].flatten()))
        encoded = encoded ^ keys[i].getT()
        # print("{} After RoundKey  {}".format(i, encoded.flatten()))
    return encoded.getT().tobytes()[::8]
Beispiel #26
0
def get_chosen_range(grid: numpy.matrix,
                     app: PySide.QtGui.QApplication) -> list:
    # http://segmentfault.com/q/1010000003028975
    size = grid.shape
    twid = PySide.QtGui.QTableWidget(size[0], size[1])
    # 为了设置宽度方便
    for col in range(size[1]):
        twid.setColumnWidth(col, 80)
        for row in range(size[0]):
            value = grid.item(row, col)
            item = PySide.QtGui.QTableWidgetItem(str(value))
            twid.setItem(row, col, item)
    twid.resize(1020, 400)
    twid.setEditTriggers(PySide.QtGui.QAbstractItemView.NoEditTriggers)
    twid.show()

    chosen_cell = []

    @PySide.QtCore.Slot(int, int)
    def double_click_cell(row, col):
        chosen_cell.append([row, col])
        print(chosen_cell)
        if len(chosen_cell) == 2:
            app.closeAllWindows()

    twid.cellDoubleClicked.connect(double_click_cell)
    app.exec_()
    return chosen_cell
Beispiel #27
0
def funm(dat: numpy.matrix, f) -> numpy.matrix:
    '''Dirty copycat for scipy.linalg.funm'''
    ret = dat.copy()
    for x in numpy.nditer(ret, op_flags=['readwrite']):
        x[...] = f(x)

    return ret
Beispiel #28
0
        def setLocalTransformMatrix(self, m: np.matrix):
            '''
                Sets the local transform matrix of this bone.
            '''

            self.transformMatrix = np.matrix(m.copy())
            self.markGlobalTransformDirty()
Beispiel #29
0
def hotelling(x: np.matrix):
    s = x.sum(axis=1)
    alpha = s / np.amax(s)
    # print("alpha = " + str(alpha))

    alpha_old = alpha - alpha
    while (np.absolute(alpha - alpha_old)).sum() >= epsilon:
        # print("============================")
        beta = x.dot(alpha)
        # print("beta = " + str(beta))
        alpha_old = alpha
        alpha = beta / np.amax(beta)
        # print("alpha = " + str(alpha))
        # print("difference = " + str(np.absolute(alpha - alpha_old)))
        # print("diff_sum = " + str((np.absolute(alpha - alpha_old)).sum()))
    return alpha, np.amax(beta)
Beispiel #30
0
    def __init__(
        self,
        ATAC_data: np.matrix = None,
        ATAC_name: pd.DataFrame = None,
        cell_name: pd.DataFrame = None,
        delayed_populating: bool = False,
        is_filter=True,
        datatype="atac_seq",
    ):

        if ATAC_data.all() == None:
            raise Exception(
                "Invalid Input, the gene expression matrix is empty!")
        self.ATAC_data = ATAC_data
        self.ATAC_name = ATAC_name
        self.cell_name = cell_name
        self.is_filter = is_filter
        self.datatype = datatype
        self.cell_name_formulation = None
        self.atac_name_formulation = None

        if not isinstance(self.ATAC_name, pd.DataFrame):
            self.ATAC_name = pd.DataFrame(self.ATAC_name)
        if not isinstance(self.cell_name, pd.DataFrame):
            self.cell_name = pd.DataFrame(self.cell_name)

        # form data name and filename unless manual override
        logger.debug("Loading atac expression dataset")
        super().__init__()
        if not delayed_populating:
            self.populate()
Beispiel #31
0
def ComputeSVD(A: np.matrix):
    """Given a matrix A use the eigen value decomposition to compute a SVD
       decomposition. It returns a tuple (U,Sigma,V) of np.matrix objects."""
    k = np.linalg.matrix_rank(A)
    B = A.transpose() @ A
    w, V = eig(B)

    #Eigenwerte und Vektoren neu sortieren
    idx = np.argsort(w)
    w = w[idx]
    V = V[:, idx]

    #S berechnen
    S = np.zeros(A.shape)
    for i in range(S.shape[0]):
        for j in range(S.shape[1]):
            if i == j:
                S[i][j] = sqrt(w[i])

    #U berechnen
    U = np.zeros((k + 1, k + 1))
    for i in range(k):
        U[:, i] = (1 / S[i][i] * A * V[:, i]).flat
    U = np.linalg.qr(U)[0]
    return np.asmatrix(U), np.asmatrix(S), np.asmatrix(V)
Beispiel #32
0
def next_neighbor(terrain: np.matrix, position: Tuple[int,
                                                      int]) -> Tuple[int, int]:
    """
    Returns the position of the lowest neighbor.
    
    Args:
    terrain: the terrain's configuration comprised from integer elevation levels.
    position: the pair of integers representing the ball's current position.

    Output:
    The position (pair of coordinates) of the lowest neighbor.
    
    Example:
    >>> next_neighbor(np.matrix([[-2, 3, 2, 1]]), (0, 1))
    (0, 0)
    """

    x, y = position
    allowed_neighbors = []
    for delta_x in range(-1, 2):
        for delta_y in range(-1, 2):
            new_position = (x + delta_x, y + delta_y)
            if (not wall(terrain, new_position)):
                allowed_neighbors.append(
                    (terrain.item(new_position), new_position))
    return min(allowed_neighbors)[1]
Beispiel #33
0
def svd(m: np.matrix):
    """ Returns the SVD of a matrix.

    Parameters
    ----------
    m: np.matrix
        The matrix to decompose

    Returns
    -------
    np.matrix or None
        The first matrix of the decomposition. This is a 
        orthogonal matrix.
    np.matrix
        The matrix with the singular values in the diagonal.
    np.matrix
        The second matrix of the decomposition. This one is
        also a orthogonal matrix.
    """
    m = np.float64(m.copy())
    _, columns = m.shape
    svdMatrix = m @ m.T
    singValues, uMatrix = symmetricEig(svdMatrix)
    singValues, uMatrix = _sortEig(singValues, uMatrix)
    singValues = np.sqrt(np.abs(singValues))
    vMatrix = m.T @ uMatrix
    for i in range(vMatrix.shape[1]):
        vMatrix[i, :] = vMatrix[i, :] / np.linalg.norm(vMatrix[i, :])
    return uMatrix, singValues[:columns], vMatrix.T
Beispiel #34
0
    def decompose(cls, m: np.matrix):

        t = cls.getTranslation(m)
        s = cls.getScale(m)

        norm = np.identity(4)
        norm.itemset(0, 0, m.item(0, 0) / s[0])
        norm.itemset(0, 1, m.item(0, 1) / s[0])
        norm.itemset(0, 2, m.item(0, 2) / s[0])
        norm.itemset(1, 0, m.item(1, 0) / s[1])
        norm.itemset(1, 1, m.item(1, 1) / s[1])
        norm.itemset(1, 2, m.item(1, 2) / s[1])
        norm.itemset(2, 0, m.item(2, 0) / s[2])
        norm.itemset(2, 1, m.item(2, 1) / s[2])
        norm.itemset(2, 2, m.item(2, 2) / s[2])

        return s, cls.getRotation(norm), t
Beispiel #35
0
    def __init__(self, left_num_neurons, right_num_neurons, transfer_function):
        """
        Initializes a vectorneuron with a weight matrix of size (left_num_neurons, right_num_neurons), 
        a bias vector of size (right_num_neurons, 1), and a transfer function transfer_function.
        """

        print '>>> Creating VectorNeuron: (%s, %s) %s' % \
        (left_num_neurons, right_num_neurons, transfer_function)

        self.__weight_matrix = Matrix(rand(right_num_neurons, left_num_neurons))
        self.__weight_matrix_backup = self.__weight_matrix.copy()
        self.__bias_vector = Matrix(rand(right_num_neurons, 1))
        self.__delta_w_matrix = Matrix(rand(right_num_neurons, left_num_neurons))
        self.__mersenne_twister = MersenneTwister()
        self.__mersenne_twister.seed(int(1000*time.time()))
        self.__transfer_function = transfer_function
Beispiel #36
0
class VectorNeuron(object):
    """
    The VectorNeuron class represents a single weight matrix and a corresponding
    transfer function.  

    An input to a VectorNeuron is first multiplied by the weight matrix.  The 
    result is then fed through a transfer function to produce the VectorNeuron's 
    output. 

    The output is either the containing neural network's final output or the input
    to another VectorNeuron.
    """

    __weight_matrix = None
    __weight_matrix_backup = None
    __bias_vector = None
    __delta_w_matrix = None
    __result = None
    __transfer_function = ""
    __mersenne_twister = None

    def __init__(self, left_num_neurons, right_num_neurons, transfer_function):
        """
        Initializes a vectorneuron with a weight matrix of size (left_num_neurons, right_num_neurons), 
        a bias vector of size (right_num_neurons, 1), and a transfer function transfer_function.
        """

        print '>>> Creating VectorNeuron: (%s, %s) %s' % \
        (left_num_neurons, right_num_neurons, transfer_function)

        self.__weight_matrix = Matrix(rand(right_num_neurons, left_num_neurons))
        self.__weight_matrix_backup = self.__weight_matrix.copy()
        self.__bias_vector = Matrix(rand(right_num_neurons, 1))
        self.__delta_w_matrix = Matrix(rand(right_num_neurons, left_num_neurons))
        self.__mersenne_twister = MersenneTwister()
        self.__mersenne_twister.seed(int(1000*time.time()))
        self.__transfer_function = transfer_function

    def neuron_compute(self, input_matrix):
        """Computes the vectorneuron output for input_matrix"""
        self.__result = self.__weight_matrix * input_matrix
        current_value = None
        transfer_function = self.__transfer_function
        row_dim = self.__weight_matrix.shape[0]
        input_col_dim = input_matrix.shape[1]

        for i in range(0,row_dim):
            for j in range(0, input_col_dim):
                current_value = self.__result[i, j]
                self.__result[i, j]= self.__bias_vector[i, 0] + current_value
                cmd = "self.%s(current_value)" % transfer_function
                self.__result[i, j] = eval(cmd)

    def compute_delta_w(self, m, lr):
        """Computes new delta_w matrix"""
        k = 0
        delta_w = None
        delta_w_row_dim = self.__delta_w_matrix.shape[0]
        delta_w_col_dim = self.__delta_w_matrix.shape[1]

        for i in range(0, delta_w_row_dim):
            for j in range(0,delta_w_col_dim):
                k = abs(self.__mersenne_twister.randint(0,math.pow(2,32)) % m)
                if k == 0:
                    delta_w = lr
                elif k == 1:
                    delta_w = -1.0 * lr
                else:
                    delta_w = 0.0
                self.__delta_w_matrix[i, j] = delta_w

    def compute_delta_w_annealing(self, n, m, lr):
        """Computes new delta_w matrix (annealing style)"""
        k = 0
        delta_w = None
        delta_w_row_dim = self.__delta_w_matrix.shape[0]

        for i in range(0,delta_w_row_dim):
            delta_w_matrix_col = self.__delta_w_matrix.shape[1]
            for j in range(0, delta_w_matrix_col):
                k = abs(self.__mersenne_twister.randint(0,math.pow(2,32)) % m)
                if k < n:
                    if k % 2 == 0:
                        if (k == 0):
                            delta_w = lr
                        else:
                            delta_w = lr / k
                    elif k % 2 == 1:
                        delta_w = -1.0 * lr / k
                    else:
                        delta_w = 0.0
                else:
                    delta_w = 0.0
                self.__delta_w_matrix[i, j]  = delta_w

    def logsig(self, x):
        """Returns logsig of a single variable x"""
        return 1.0/(1.0 + exp(-1.0 * x))

    def purelin(self, x):
        """Returns purelin of a single variable x"""
        return x

    def tansig(self, x):
        """Returns tansig of a single variable x"""
        return 2.0/exp(1.0 + exp(-2.0 * x), -1.0)

    def linsig(self, x):
        """Returns linsig of a single variable x"""
        if x <= 1.0 and x >= -1.0:
            return x
        if x > 1:
            return 1.0
        else:
            return -1.0

    def change_weights(self):
        """Changes weight_matrix by adding delta_w_matrix"""
        #print 'weight_matrix orig'
        #print self.__weight_matrix
        self.__weight_matrix = self.__weight_matrix + self.__delta_w_matrix
        #print 'weight matrix new'
        #print self.__weight_matrix

    def rollback_weights(self):
        """Reset weight_matrix to weight_matrix_backup"""
        #print 'resetting weights'
        self.__weight_matrix = self.__weight_matrix_backup.copy()

    def weight_matrix_backup(self):
        """Copies the current weight_matrix to weight_matrix_backup"""
        self.__weight_matrix_backup = self.__weight_matrix.copy()

    def get_bias(self):
        """Returns the vectorneuron's bias vector"""
        return self.__bias_vector

    def get_delta_w(self):
        """Return the computed delta_w matrix used to alter the weights"""
        return self.__delta_w_matrix

    def get_result(self):
        """Returns the output of vectorneuron's neuron_compute function"""
        return self.__result

    def get_weight_matrix(self):
        """Returns the vectorneuron's current weight_matrix"""
        return self.__weight_matrix

    def get_weight_matrix_backup(self):
        """Returns a backup of the vectorneuron's previous weight_matrix"""
        return self.__weight_matrix_backup

    def get_transfer_function(self):
        """Returns the vectorneuron's transfer function"""
        return self.__transfer_function

    def write_weight_to_file(self, filename):
        """Write the vectorneuron's weight_matrix to filename """
        savetxt(filename, self.__weight_matrix)
        return True

    def write_bias_to_file(self, filename):
        """Write the vectorneuron's biias vector to filename"""
        savetxt(filename, self.__bias_vector)
        return True