numpy simple usage 2

a = np.arange(12)
b = a
print (b is a)
b.shape = 3,4
print (a.shape)
print (id(a))
print (id(b))

>>>
True
(3, 4)
2052537612608
2052537612608

c = a.view() # latent copy 
print (c is a)
c.shape = 2,6
print (a.shape)
c[0,4] = 1234
print (a)
print (id(a))
print (id(c))

>>>
False
(3, 4)
[[   0    1    2    3]
 [1234    5    6    7]
 [   8    9   10   11]]
1974659569504
1974659621312


d = a.copy() #deep copy print 
( d is a)
d[0,0] = 9999
print (d) 
print (a)
>>>
False
[[9999    1    2    3]
 [   4    5    6    7]
 [   8    9   10   11]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
copy
a = np.array([[4, 3, 5], [1, 2, 1]])
print (a)
print ('--------')
b = np.sort(a, axis=1) #Sort method from small to large 
print (b)
a.sort(axis =1) #Sort method 2 from small to large 
print ( ' -------- ' )
 print (a)

>>>
[[4 3 5]
 [1 2 1]]
--------
[[3 4 5]
 [1 1 2]]
--------
[[3 4 5]
 [1 1 2]]
a = np.array([4, 3, 1, 2])
j = np.argsort(a)
print ('--------')
print (j)
print ('--------')
print (a[j])

>>>
--------
[2 3 1 0]
--------
[1 2 3 4]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325425012&siteId=291194637