Python3: 数组
2023-10-11 本文已影响0人
LET149
1. 生成随机整数数组
numpy.random.randint(number, high=, size=())
number
: 生成数组所用随机整数的下限,包含
high=
: 生成数组所有随机整数的上限,不包含
size=
: 数组的维度,依次为行
,列
等;数组可以有一维
,二维
,三维
等
>>> import numpy as np
>>>
>>>
>>> kk = np.random.randint(3,10,size=(5,6))
>>> kk
array([[6, 7, 6, 4, 3, 4],
[3, 4, 9, 9, 8, 8],
[6, 6, 8, 7, 9, 3],
[5, 8, 9, 3, 7, 3],
[3, 9, 6, 7, 5, 7]])
>>>
>>> type(kk)
<class 'numpy.ndarray'>
>>>
>>> kk = np.random.randint(3,10,size=(2,3,4))
>>> kk
array([[[6, 6, 9, 4],
[3, 6, 4, 6],
[4, 4, 3, 6]],
[[6, 7, 4, 5],
[6, 8, 4, 3],
[4, 8, 3, 4]]])
2. 查看数组维度
numpy.shape(array_name)
: 调用numpy
中的shape()
函数来查看数组对象的维度属性
array_name.shape
: 调用numpy
数组对象的维度属性
>>> import numpy as np
>>>
>>>
>>> kk = np.random.randint(3,10,size=(5,6))
>>> np.shape(kk)
(5, 6)
>>> kk.shape
(5, 6)
>>>
>>> kk = np.random.randint(3,10,size=(2,3,4))
>>> np.shape(kk)
(2, 3, 4)
>>> kk.shape
(2, 3, 4)