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) |
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]])
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