[PYTHON] numpy tips

Functions around the array

function command Remarks
List → Array numpy.array(List variable) The conversion source variable can be a tuple
List → Array(Type specification) numpy.array(List variable, numpy.Data type)
Array → List ndarray.tolist()
Array size ndarray.shape
Number of array dimensions ndarray.ndim
Number of elements in the array ndarray.size
Number of bytes in the array ndarray.nbytes
Array contents numpy.type(ndarray)
Variable type of array ndarray.dtype
Change variable type ndarray.astype(numpy.Data type)

Joining arrays

There are three types: dstack, hstack, and vstack. Each acronym stands for depth wise (along third axis), horizontal (column wise), and vertically (row wise).

Try with the following variables

>>> a = np.array((1,2,3))
>>> b = np.array([4,5,6])
>>> a
array([1, 2, 3])
>>> b
array([4, 5, 6])

dstack depth wise (along third axis) Is it a combination after transposing?

>>> np.dstack((a,b))
array([[[1, 4],
        [2, 5],
        [3, 6]]])

hstack horizontally (column wise)

>>> np.hstack((a,b))
array([1, 2, 3, 4, 5, 6])

vstack vertically (row wise)

>>> np.vstack((a,b))
array([[1, 2, 3],
       [4, 5, 6]])

Array sort

You can use ndarray.sort (). In this case, the contents of the ndarray variable are sorted. If you set numpy.sort (ndarray), a copy of the sorted result will be returned. By the way, there is also ndarray.argsort () which returns the index when sorted. Since ndarray.sort () specifies the axis to sort, be careful at runtime.

Try sorting the following variables

>>> hoge = np.array([[1,3,2],[6,5,4],[9,7,8]])
>>> hoge
array([[1, 3, 2],
       [6, 5, 4],
       [9, 7, 8]])

Normal sorting. Sort runs row by row.

>>> np.sort(hoge)
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Use axis to sort by column.

>>> np.sort(hoge, axis=0)
array([[1, 3, 2],
       [6, 5, 4],
       [9, 7, 8]])

If you make it Structured Arrays and name the array, it seems that you can sort by specific column specification. But I don't know how to make Structured Arrays well ...

Recommended Posts

numpy tips
Python and numpy tips
Python / Numpy np.newaxis thinking tips
numpy practice 1
python tips
numpy part 1
Jupyter Tips 4
NumPy basics
Numpy Memorandum_Matrix
python tips
Jupyter Tips 5
Scapy Tips
Jupyter Tips 3
Jupyter Tips 2
About numpy
NumPy axis
Use Numpy
numpy part 2
Python Tips
Python tips
Python Conda Tips
My Numpy (Python)
where in numpy
Django's personal tips
Python debugging tips
Python click tips
[pandas] GroupBy Tips
numpy unit test
NumPy array manipulation (3)
Unexpectedly (?) Python tips
list and numpy
NumPy universal functions
Tensorflow personal tips
numpy memorandum 1 / np.pad
#Python basics (#Numpy 1/2)
Django Template Tips
#Python basics (#Numpy 2/2)
fabric Writing Tips
numpy index search
NumPy array manipulation (1)
[Numpy] Shuffle ndarray
LTTng Tracing Tips
Python #Numpy basics
numpy non-basic techniques
About Numpy broadcast
[PyTorch] Sample ① ~ NUMPY ~
Install Numpy + atlas
[Python] Numpy memo
Choregraphe Tips Collection
[Tips] First-order difference calculation and inverse conversion [python / numpy]