numpy

np array

np arry to list

import numpy as np

List = arry.tolist()

Create a list

print(np.linspace(0, 100, 51))
[  0.   2.   4.   6.   8.  10.  12.  14.  16.  18.  20.  22.  24.  26.
  28.  30.  32.  34.  36.  38.  40.  42.  44.  46.  48.  50.  52.  54.
  56.  58.  60.  62.  64.  66.  68.  70.  72.  74.  76.  78.  80.  82.
  84.  86.  88.  90.  92.  94.  96.  98. 100.]

np arrary caculating

np.arrary sum()

np.sum(array1-array2)

np arrary append

append

np.append(np1, np2,axis=0)

Reduce Dimension

x = np.array([[1, 2],[3, 4]])
print(np.ravel(x))
print(np.ravel(x,'F'))

Locating (argwhere)

arr = np.random.randint(0,10, (3,4))  
index = np.argwhere(arr < 5)
index2 = np.where(arr < 5)

>>> index
array([[0, 2],
[0, 3],
[1, 1],
[2, 0],
[2, 3]])
>>> index2
(array([0, 0, 1, 2, 2]), array([2, 3, 1, 0, 3]))

Axis

Axis flip / swap

frame2 = frame.swapaxes(0,1)

frame.shape
frame2.shape
(360, 480, 3)
(480, 360, 3)

Replace

arr[arr > 255] = x




ummmm... I am not really like to using numpy because I thing data.frame() in R is much better.
Author

Karobben

Posted on

2020-09-12

Updated on

2023-06-06

Licensed under

Comments