简体   繁体   中英

Symmetric matrices in numpy?

I wish to initiate a symmetric matrix in python and populate it with zeros.

At the moment, I have initiated an array of known dimensions but this is unsuitable for subsequent input into R as a distance matrix.

Are there any 'simple' methods in numpy to create a symmetric matrix?

Edit

I should clarify - creating the 'symmetric' matrix is fine. However I am interested in only generating the lower triangular form, ie.,

ar = numpy.zeros((3, 3))

array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

I want:

array([[ 0],
       [ 0, 0 ],
       [ 0.,  0.,  0.]])

Is this possible?

I don't think it's feasible to try work with that kind of triangular arrays.

So here is for example a straightforward implementation of (squared) pairwise Euclidean distances:

def pdista(X):
    """Squared pairwise distances between all columns of X."""
    B= np.dot(X.T, X)
    q= np.diag(B)[:, None]
    return q+ q.T- 2* B

For performance wise it's hard to beat it (in Python level). What would be the main advantage of not using this approach?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM