ການບັນທຶກສະພາບອາກາດ NumPy

Array shape

The shape of the array is the number of elements in each dimension.

Get the shape of the array

NumPy array has an attribute named shape attribute, which returns a tuple with the number of elements in each index.

Example

Print the shape of a 2-D array:

import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)

Run Instance

The example returns (2, 4), this means that the array has 2 dimensions, each with 4 elements.

Example

Using ndmin Create a vector with values 1,2,3,4 and create an array with 5 dimensions and verify that the value of the last dimension is 4:

import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
print('shape of array :', arr.shape)

Run Instance

ຮູບແບບຂອງຕົວຢ່າງຖືກຕ້ອງຫຍັງ?

ຈຳນວນທີ່ຢູ່ບ່ອນຢູ່ທີ່ພິການຕາມຈຳນວນທີ່ຢູ່ບ່ອນຂອງພາກສັດຕ່າງດຽວ.

ບັນທຶກ 4 ໃນຕົວຢ່າງດັ່ງກ່າວ, ພວກເຮົາມີຄຸນຂອງ 4, ແລະສາມາດເວົ້າໄດ້ວ່າພາກສັດທີ 5 (4 + 1 th) ມີສິບສິບສີ່ອັນ.