[Numpy-discussion] saving the lower half of matrix
Kumar Appaiah
a.kumar@alumni.iitm.ac...
Mon Jun 17 06:14:56 CDT 2013
On Mon, Jun 17, 2013 at 01:07:34PM +0200, Bala subramanian wrote:
> Friends,
> I have to save only the lower half of a symmetric matrix to a file. I used
> numpy.tril to extract the lower half. However when i use 'numpy.savetxt',
> numpy saves the whole matrix (with upper half values replaced as zeros)
> rather than only the lower half. Any better idea to achieve this.
> as an example
> >>>import numpy as np
> >>> d=np.arange(1,26,1)
> >>> d=d.reshape(5,5)
> >>> np.tril(d)
> array([[ 1,� 0,� 0,� 0,� 0],
> ������ [ 6,� 7,� 0,� 0,� 0],
> ������ [11, 12, 13,� 0,� 0],
> ������ [16, 17, 18, 19,� 0],
> ������ [21, 22, 23, 24, 25]])
>
> >>> np.savetxt( 'test', np.tril(d))
> output test also contains the zeros, what i want is only the lower half
> like below.
> 1
> 6��� 7
> 11 12 13
> �-���� -���� -��� -� etc
How about saving numpy.concatenate([x[i,:i+1] for i in range(x.shape[0])])
instead? If you remove the concatenate, you'll get the individual
arrays that have the data you need as well.
Kumar
--
Kumar Appaiah
More information about the NumPy-Discussion
mailing list