np.size()和np.shape()函数
·
numpy.size和numpy.shape函数
np.size()函数
函数作用:计算当前数组中元素的总个数
函数调用方法:
# method 1
array.size
# method 2
import numpy as np
np.size(array)
示例代码:
import numpy as np
class NumpyStudy:
@staticmethod
def mainProgram():
array = np.array([[1, 2], [3, 4]])
# method 1
result1 = np.size(array)
# method 2
result2 = array.size
# method 3
result3 = np.product(array.shape)
print('数组array中包含的元素个数为:')
print(f"The value calculated by method 1: {result1}")
print(f"The value calculated by method 2: {result2}")
print(f"The value calculated by method 3: {result3}")
if __name__ == "__main__":
main = NumpyStudy()
main.mainProgram()
"""
数组array中包含的元素个数为:
The value calculated by method 1: 4
The value calculated by method 2: 4
The value calculated by method 3: 4
"""
我们可以看到,三种方法均可以获取到ndarray数组中的元素个数。
np.shape()函数
函数作用:计算当前数组的形状
函数调用方法:
# method 1
array.shape
# method 2
import numpy as np
np.shape(array)
示例代码:
import numpy as np
class NumpyStudy:
@staticmethod
def mainProgram():
array = np.array([[1, 2], [3, 4]])
# method 1
result1 = np.shape(array)
# method 2
result2 = array.shape
print('数组array的形状为:')
print(f"The value calculated by method 1: {result1}")
print(f"The value calculated by method 2: {result2}")
if __name__ == "__main__":
main = NumpyStudy()
main.mainProgram()
"""
数组array的形状为:
The value calculated by method 1: (2, 2)
The value calculated by method 2: (2, 2)
"""
如果大家觉得有用,就点个赞让更多的人看到吧~
更多推荐

所有评论(0)