python类triu()的实例源码

data_operations.py 文件源码 项目:genomedisco 作者: kundajelab 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def sqrtvc(m):
    mup=m
    mdown=mup.transpose()
    mdown.setdiag(0)
    mtogether=mup+mdown
    sums_sq=np.sqrt(mtogether.sum(axis=1)) 
    D_sq = sps.spdiags(1.0/sums_sq.flatten(), [0], mtogether.get_shape()[0], mtogether.get_shape()[1], format='csr')
    return sps.triu(D_sq.dot(mtogether.dot(D_sq)))
data_operations.py 文件源码 项目:genomedisco 作者: kundajelab 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def hichip_add_diagonal(m):
    mup=m
    mdown=mup.transpose()
    mdown.setdiag(0)
    mtogether=mup+mdown
    sums=mtogether.sum(axis=1)
    max_sum=np.max(sums)
    to_add=1.0*max_sum-1.0*sums
    to_add_values=[]
    for i in range(m.shape[0]):
        to_add_values.append(to_add[i,0])
    mtogether.setdiag(np.array(to_add_values))
    D = sps.spdiags(1.0/sums.flatten(), [0], mtogether.get_shape()[0], mtogether.get_shape()[1], format='csr')
    return sps.triu(D.dot(mtogether))
data_operations.py 文件源码 项目:genomedisco 作者: kundajelab 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def coverage_norm(m):
    mup=m
    mdown=mup.transpose()
    mdown.setdiag(0)
    mtogether=mup+mdown
    sums=mtogether.sum(axis=1)
    D = sps.spdiags(1.0/sums.flatten(), [0], mtogether.get_shape()[0], mtogether.get_shape()[1], format='csr')
    return sps.triu(D.dot(mtogether.dot(D)))

#assumes matrix is upper triangular
data_operations.py 文件源码 项目:genomedisco 作者: kundajelab 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
def array_2_coverageVector(m):
    assert np.allclose(m, np.triu(m))
    m_sym=m+m.T-m.diagonal()
    return m_sym.sum(axis=0)
data_operations.py 文件源码 项目:genomedisco 作者: kundajelab 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def subsample_to_depth_array_upperTri(m,seq_depth):
    m=np.triu(m)
    subsampled_data=np.zeros(m.shape)
    depthm=m.sum()
    assert seq_depth<=depthm
    subsampling_prob=seq_depth/depthm
    for i in range(m.shape[0]):
        for j in range(m.shape[1]):
            if j<=i:
                continue
            n=m[i,j]
            subsampled_data[i,j]=np.random.binomial(n,subsampling_prob,1)[0]
    return subsampled_data
disco_random_walks_binarized_matrices.py 文件源码 项目:genomedisco 作者: kundajelab 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def binarize_top(m,q):
    threshold=mquantiles(np.triu(m).flatten(),q)
    new_m=copy.deepcopy(m)
    new_m[new_m<threshold]=0
    new_m[new_m>=threshold]=1    
    return get_sqrtvc(new_m)
reward.py 文件源码 项目:NMT-RDPG 作者: MultiPath 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def compute_discount(gamma, maxlen):
    c = numpy.ones((maxlen,)) * gamma
    c[0] = 1.
    c = c.cumprod()

    C = numpy.triu(numpy.repeat(c[None, :], repeats=maxlen, axis=0))
    C /= c[:, None]
    return C
utils.py 文件源码 项目:torch_light 作者: ne7ermore 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def get_attn_subsequent_mask(seq):
    assert seq.dim() == 2
    attn_shape = (seq.size(0), seq.size(1), seq.size(1))
    subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
    subsequent_mask = torch.from_numpy(subsequent_mask)
    if seq.is_cuda:
        subsequent_mask = subsequent_mask.cuda()
    return subsequent_mask
test_twodim_base.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def test_tril_triu_ndim2():
    for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
        a = np.ones((2, 2), dtype=dtype)
        b = np.tril(a)
        c = np.triu(a)
        yield assert_array_equal, b, [[1, 0], [1, 1]]
        yield assert_array_equal, c, b.T
        # should return the same dtype as the original array
        yield assert_equal, b.dtype, a.dtype
        yield assert_equal, c.dtype, a.dtype
test_twodim_base.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def test_tril_triu_with_inf():
    # Issue 4859
    arr = np.array([[1, 1, np.inf],
                    [1, 1, 1],
                    [np.inf, 1, 1]])
    out_tril = np.array([[1, 0, 0],
                         [1, 1, 0],
                         [np.inf, 1, 1]])
    out_triu = out_tril.T
    assert_array_equal(np.triu(arr), out_triu)
    assert_array_equal(np.tril(arr), out_tril)
test_twodim_base.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_mask_indices():
    # simple test without offset
    iu = mask_indices(3, np.triu)
    a = np.arange(9).reshape(3, 3)
    yield (assert_array_equal, a[iu], array([0, 1, 2, 4, 5, 8]))
    # Now with an offset
    iu1 = mask_indices(3, np.triu, 1)
    yield (assert_array_equal, a[iu1], array([1, 2, 5]))
test_linalg.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def test_dynamic_programming_logic(self):
        # Test for the dynamic programming part
        # This test is directly taken from Cormen page 376.
        arrays = [np.random.random((30, 35)),
                  np.random.random((35, 15)),
                  np.random.random((15, 5)),
                  np.random.random((5, 10)),
                  np.random.random((10, 20)),
                  np.random.random((20, 25))]
        m_expected = np.array([[0., 15750., 7875., 9375., 11875., 15125.],
                               [0.,     0., 2625., 4375.,  7125., 10500.],
                               [0.,     0.,    0.,  750.,  2500.,  5375.],
                               [0.,     0.,    0.,    0.,  1000.,  3500.],
                               [0.,     0.,    0.,    0.,     0.,  5000.],
                               [0.,     0.,    0.,    0.,     0.,     0.]])
        s_expected = np.array([[0,  1,  1,  3,  3,  3],
                               [0,  0,  2,  3,  3,  3],
                               [0,  0,  0,  3,  3,  3],
                               [0,  0,  0,  0,  4,  5],
                               [0,  0,  0,  0,  0,  5],
                               [0,  0,  0,  0,  0,  0]], dtype=np.int)
        s_expected -= 1  # Cormen uses 1-based index, python does not.

        s, m = _multi_dot_matrix_chain_order(arrays, return_costs=True)

        # Only the upper triangular part (without the diagonal) is interesting.
        assert_almost_equal(np.triu(s[:-1, 1:]),
                            np.triu(s_expected[:-1, 1:]))
        assert_almost_equal(np.triu(m), np.triu(m_expected))
numpy_impl.py 文件源码 项目:numba-examples 作者: numba 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def potential_numpy(cluster):
    d = distances_numpy(cluster)
    dtri = np.triu(d)
    energy = lj_numpy(dtri[dtri > 1e-6]).sum()
    return energy
#### END: numpy
sip.py 文件源码 项目:psp 作者: cmap 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def extract_test_vals(query, target, query_field, target_field, test_df, is_test_df_sym):
    """ Extract values that has query in the columns and target in the rows.

    Args:
        query (string)
        target (string)
        query_field (string): name of multiindex level in which to find query
        target_field (string): name of multiindex level in which to find target
        test_df (pandas multi-index df)
        is_test_df_sym (bool): only matters if query == target; set to True to
            avoid double-counting in the case of a symmetric matrix

    Returns:
        vals (numpy array)

    """
    assert query in test_df.columns.get_level_values(query_field), (
        "query {} is not in the {} level of the columns of test_df.".format(
            query, query_field))

    assert target in test_df.index.get_level_values(target_field), (
        "target {} is not in the {} level of the index of test_df.".format(
            target, target_field))

    # Extract elements where query is in columns and target is in rows
    target_in_rows_query_in_cols_df = test_df.loc[
            test_df.index.get_level_values(target_field) == target,
            test_df.columns.get_level_values(query_field) == query]

    # If query == target AND the matrix is symmetric, need to take only triu
    # of the extracted values in order to avoid double-counting
    if query == target and is_test_df_sym:
        mask = np.triu(np.ones(target_in_rows_query_in_cols_df.shape), k=1).astype(np.bool)
        vals_with_nans = target_in_rows_query_in_cols_df.where(mask).values.flatten()
        vals = vals_with_nans[~np.isnan(vals_with_nans)]

    else:
        vals = target_in_rows_query_in_cols_df.values.flatten()

    return vals
Models.py 文件源码 项目:attention-is-all-you-need-pytorch 作者: jadore801120 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def get_attn_subsequent_mask(seq):
    ''' Get an attention mask to avoid using the subsequent info.'''
    assert seq.dim() == 2
    attn_shape = (seq.size(0), seq.size(1), seq.size(1))
    subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
    subsequent_mask = torch.from_numpy(subsequent_mask)
    if seq.is_cuda:
        subsequent_mask = subsequent_mask.cuda()
    return subsequent_mask
cmaes.py 文件源码 项目:bolero 作者: rock-learning 项目源码 文件源码 阅读 43 收藏 0 点赞 0 评论 0
def _update_covariance(self, it):
        self.eigen_decomp_updated = it
        self.cov[:, :] = np.triu(self.cov) + np.triu(self.cov, 1).T
        D, B = np.linalg.eigh(self.cov)
        # HACK: avoid numerical problems
        D = np.maximum(D, np.finfo(np.float).eps)
        D = np.diag(np.sqrt(1.0 / D))
        self.invsqrtC = B.dot(D).dot(B.T)
transformer.py 文件源码 项目:sockeye 作者: awslabs 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def get_bias(length: int):
        # matrix with lower triangle and main diagonal set to 0, upper triangle set to 1
        upper_triangle = np.triu(np.ones((length, length)), k=1)
        # (1, length, length)
        bias = -99999999. * np.reshape(upper_triangle, (1, length, length))
        return mx.nd.array(bias)
test_twodim_base.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def test_tril_triu_ndim2():
    for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
        a = np.ones((2, 2), dtype=dtype)
        b = np.tril(a)
        c = np.triu(a)
        yield assert_array_equal, b, [[1, 0], [1, 1]]
        yield assert_array_equal, c, b.T
        # should return the same dtype as the original array
        yield assert_equal, b.dtype, a.dtype
        yield assert_equal, c.dtype, a.dtype
test_twodim_base.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def test_tril_triu_with_inf():
    # Issue 4859
    arr = np.array([[1, 1, np.inf],
                    [1, 1, 1],
                    [np.inf, 1, 1]])
    out_tril = np.array([[1, 0, 0],
                         [1, 1, 0],
                         [np.inf, 1, 1]])
    out_triu = out_tril.T
    assert_array_equal(np.triu(arr), out_triu)
    assert_array_equal(np.tril(arr), out_tril)
test_twodim_base.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_mask_indices():
    # simple test without offset
    iu = mask_indices(3, np.triu)
    a = np.arange(9).reshape(3, 3)
    yield (assert_array_equal, a[iu], array([0, 1, 2, 4, 5, 8]))
    # Now with an offset
    iu1 = mask_indices(3, np.triu, 1)
    yield (assert_array_equal, a[iu1], array([1, 2, 5]))


问题


面经


文章

微信
公众号

扫码关注公众号