How can i view the entire matrix in python? [duplicate] - python

This question already has answers here:
How do I print the full NumPy array, without truncation?
(22 answers)
Closed 9 years ago.
I exported a matrix of 127x127 values as a txt, but the output appears as
answer:[[ 44. 1. 0. ..., 12. 13. 2.]
[ 51. 7. 0. ..., 5. 14. 4.]
[ 0. 1. 4. ..., 0. 0. 1.]
...,
[ 22. 110. 70. ..., 5. 0. 0.]
[ 12. 36. 12. ..., 0. 0. 2.]
[ 0. 0. 0. ..., 24. 177. 53.]]
I need access to all values, as input on Support Vector Machines
Thank You

Use numpy.set_printoptions to change the number of items returned when the array is printed.
>>> import numpy as np
>>> a = np.arange(127*127).reshape(127, 127)
>>> np.set_printoptions(edgeitems=127)
>>> print a
As expected this will flood your screen.

I'm guessing you're using numpy. If that's the case, I suggest you use the savetxt() function (http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html).

Related

Using slice notation to set 1 dimension of numpy array

From what I understand of python's slice notation, using the slice notation shallow-copies the array in question.
However, what happens if you set a slice of an array equal to a certain value?
For example:
import numpy as np
a=np.zeros(shape=(3,2))
b=np.zeros(shape=(3,2))
for i in range(0,2):
a[:,i]=i+1
for i in range(0,2):
for x in range(0,3):
b[x,i]=i+1
print a
print b
Here a and b are identical.
Is there a reason I should not use the slice notation in this way? (I have never seen anyone use the slice notation in this way, so I feel like there might be)
I see no reason you cannot use your code. below is a speed test of the sample above expanded a bit to ensure that it would take enough time to register.
test of numpy code on https://www.tutorialspoint.com/online_python_ide.php
import numpy as np
import time
a=np.zeros(shape=(4,3))
b=np.zeros(shape=(4,3))
print "--before--"
print a
print ""
print b
start_time = time.time()
for i in range(0,3):
a[:,i]=i+1
time1 = time.time()
for i in range(0,3):
for x in range(0,4):
b[x,i]=i+1
time2 = time.time()
print "--after--"
print a
print ("this took %s seconds\n"% (time1-start_time))
print b
print ("this took %s seconds\n"% (time2-time1))
print "--done--\n"
----- output ----
--before--
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
--after--
[[ 1. 2. 3.]
[ 1. 2. 3.]
[ 1. 2. 3.]
[ 1. 2. 3.]]
this took 1.21593475342e-05 seconds
[[ 1. 2. 3.]
[ 1. 2. 3.]
[ 1. 2. 3.]
[ 1. 2. 3.]]
this took 7.86781311035e-06 seconds
--done--

Python creating matrix using if condition on indices : incorrect result

I have the following code where I have been trying to create a tridiagonal matrix x using if-conditions.
#!/usr/bin/env python
# import useful modules
import numpy as np
N=5
x=np.identity(N)
#x=np.zeros((N,N))
print x
# Construct NxN matrix
for i in range(N):
for j in range(N):
if i-j==1:
x[i][j]=1
elif j-1==1:
x[i][j]=-1
else:
x[i][j]=0
print "i= ",i," j= ",j
print x
I desire to get
[[ 0. -1. 0. 0. 0.]
[ 1. 0. -1. 0. 0.]
[ 0. 1. 0. -1 0.]
[ 0. 0. 1. 0. -1.]
[ 0. 0. 0. 1. 0.]]
However, I obtain
[[ 0. 0. -1. 0. 0.]
[ 1. 0. -1. 0. 0.]
[ 0. 1. -1. 0. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 0. -1. 1. 0.]]
What's going wrong?
Bonus question : Can I forcefully index from 1 to 5 instead of 0 to 4 in this example, or Python never allows that?
elif j-1==1: should be elif j-i==1:.
And no, lists/arrays etc. are always indexed from 0.
As for the bonus question, the first element of a sequence in Python has always the index 0. However, if for some particular reason (for example to prevent off-by-one errors) you wish to count the elements of a sequence from a value other than 0, you could use the built-in function enumerate() and set the value of the optional parameter start to fit your needs:
>>> seq = ['a', 'b', 'c']
>>> for count, item in enumerate(seq, start=1):
... print(count, item)
...
1 a
2 b
3 c

Numpy - Modal matrix and diagonal Eigenvalues

I wrote a simple Linear Algebra code in Python Numpy to calculate the Diagonal of EigenValues by calculating $M^{-1}.A.M$ (M is the Modal Matrix) and it's working strange.
Here's the Code :
import numpy as np
array = np.arange(16)
array = array.reshape(4, -1)
print(array)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
eigenvalues, eigenvectors = np.linalg.eig(array)
print eigenvalues
[ 3.24642492e+01 -2.46424920e+00 1.92979794e-15 -4.09576009e-16]
print eigenvectors
[[-0.11417645 -0.7327781 0.54500164 0.00135151]
[-0.3300046 -0.28974835 -0.68602671 0.40644504]
[-0.54583275 0.15328139 -0.2629515 -0.8169446 ]
[-0.76166089 0.59631113 0.40397657 0.40914805]]
inverseEigenVectors = np.linalg.inv(eigenvectors) #M^(-1)
diagonal= inverseEigenVectors.dot(array).dot(eigenvectors) #M^(-1).A.M
print(diagonal)
[[ 3.24642492e+01 -1.06581410e-14 5.32907052e-15 0.00000000e+00]
[ 7.54951657e-15 -2.46424920e+00 -1.72084569e-15 -2.22044605e-16]
[ -2.80737213e-15 1.46768503e-15 2.33547852e-16 7.25592561e-16]
[ -6.22319863e-15 -9.69656080e-16 -1.38050658e-30 1.97215226e-31]]
the final 'diagonal' matrix should be a diagonal matrix with EigenValues on the main diagonal and zeros elsewhere. but it's not... the two first main diagonal values ARE eigenvalues but the two second aren't (although just like the two second eigenvalues, they are nearly zero).
and by the way a number like $-1.06581410e-14$ is literally zero so how can I make numpy show them as zero?
What am I doing wrong?
Thanks...
Just round the final result to the desired digits :
print(diagonal.round(5))
array([[ 32.46425, 0. , 0. , 0. ],
[ 0. , -2.46425, 0. , 0. ],
[ 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. ]])
Don't confuse precision of computation and printing policies.
>>> diagonal[np.abs(diagonal)<0.0000000001]=0
>>> print diagonal
[[ 32.4642492 0. 0. 0. ]
[ 0. -2.4642492 0. 0. ]
[ 0. 0. 0. 0. ]
[ 0. 0. 0. 0. ]]
>>>

Index of element in Numpy array 1

I am doing work on parallelization .and I have numpy array in between.
I want to check the index of numpy array
for example :
pos is a ndarray and its value is
pos :
ndarray[[ 0. 44. 2367.]
[ 0. 73. 2301.]
[ 0. 38. 2304.]
[ 0. 35. 2349.]
[ 0. 6. 0.]
[ 0. 43. 2319.]
[ 0. 95. 2381.]
[ 0. 189. 3.]
[ 0. 0. 2339.]
[ 0. 22. 2335.]
[ 0. 44. 2345.]
[ 0. 44. 2345.]
[ 0. 52. 2348.]
[ 0. 50. 2348.]]
dtype :float64
max : 2381.0
min :0.0
Shape = {tuple}(14, 3)
size :42
I want to consider for my calculation only 2nd position of pos ndarray for example 2367,2301,2304...etc.
so how can I indexing them and how can I sort 2nd position value only? means if 2nd position of array is 3,4,2,1 then I want 2nd position is 1,2,3,4 like that..
give me suggestion..
If you want to refer to the column with index 2 (i.e. the rightmost column) of the array, you can just do
pos[:,2]
If you want to sort (in place) the pos array by the second column, you can do:
pos[:,2].sort()

numpy save/load corrupting an array

I am trying to save a large numpy array and reload it. Using numpy.save and numpy.load, the array values are corrupted/change. The shape and data type of the array pre-saving, and post-loading, are the same, but the post-loading array has the vast majority of the values zeroed.
The array is (22915,22915), values are float64's, takes 3.94 gb's as a .npy file, and the data entries average about .1 (not tiny floats that might reasonably get converted to zeroes). I am using numpy 1.5.1.
Any help on why this corruption is occurring would be greatly appreciated because I am at a loss. Below is some code providing evidence of the claims above.
In [7]: m
Out[7]:
array([[ 0. , 0.02023, 0.00703, ..., 0.02362, 0.02939, 0.03656],
[ 0.02023, 0. , 0.0135 , ..., 0.04357, 0.04934, 0.05651],
[ 0.00703, 0.0135 , 0. , ..., 0.03037, 0.03614, 0.04331],
...,
[ 0.02362, 0.04357, 0.03037, ..., 0. , 0.01797, 0.02514],
[ 0.02939, 0.04934, 0.03614, ..., 0.01797, 0. , 0.01919],
[ 0.03656, 0.05651, 0.04331, ..., 0.02514, 0.01919, 0. ]])
In [8]: m.shape
Out[8]: (22195, 22195)
In [12]: save('/Users/will/Desktop/m.npy',m)
In [14]: lm = load('/Users/will/Desktop/m.npy')
In [15]: lm
Out[15]:
array([[ 0. , 0.02023, 0.00703, ..., 0. , 0. , 0. ],
[ 0. , 0. , 0. , ..., 0. , 0. , 0. ],
[ 0. , 0. , 0. , ..., 0. , 0. , 0. ],
...,
[ 0. , 0. , 0. , ..., 0. , 0. , 0. ],
[ 0. , 0. , 0. , ..., 0. , 0. , 0. ],
[ 0. , 0. , 0. , ..., 0. , 0. , 0. ]])
In [17]: type(lm[0][0])
Out[17]: numpy.float64
In [18]: type(m[0][0])
Out[18]: numpy.float64
In [19]: lm.shape
Out[19]: (22195, 22195)
This is a known issue (note that that links against numpy 1.4). If you really can't upgrade, my advice would be to try to save in a different way (savez, savetxt). If getbuffer is available you can try to write the bytes directly. If all else fails (and you can't upgrade), you can write your own save function pretty easily.

Categories

Resources