一、从python列表创建数组
官方文档:https://www.numpy.org/devdocs/reference/generated/numpy.array.html?highlight=array#numpy.array
1、和python列表不同,NumPy要求数组必须包含同一类型的数据。如果类型不匹配,NumPy会向上转换类型。如下面实例:
ls = np.array([1.36, 4, 2, 5, 3])
print(ls)
这里输出时数据都被转换成了浮点型:
[1.36 4. 2. 5. 3. ]
2、如果希望设置数组的数据类型,可以使用dtype关键字:
ls = np.array([1, 2, 3, 4, 5], dtype='float32')
[1. 2. 3. 4. 5.]
3、NumPy数组还可以被指定为多维的:
np.array([range(i, i + 3) for i in [2, 4, 6]])
这其实就是一个两层循环产生的一个列表,对每一个 i 都有 i , i + 1, i + 2,输出为一行。
array([[2, 3, 4],
[4, 5, 6],
[6, 7, 8]])
二、从头创建数组
下面列举几个使用NumPy内置方法创建数组的方法:
1、创建一个长度为10的数组,值都为0
https://www.numpy.org/devdocs/reference/generated/numpy.zeros.html#numpy.zeros
np.zeros(10, dtype=int)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
2、创建一个3 x 5的浮点数组,数值都为1
https://www.numpy.org/devdocs/reference/generated/numpy.ones.html#numpy.ones
np.ones((3, 5), dtype=float)
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
3、创建一个3 x 5的浮点数组,数值都为3.14
https://www.numpy.org/devdocs/reference/generated/numpy.full.html#numpy.full
np.full((3, 5), 3.14)
array([[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14],
[3.14, 3.14, 3.14, 3.14, 3.14]])
4、创建一个由3个整形数组成的未初始化的数组,值不确定
https://www.numpy.org/devdocs/reference/generated/numpy.empty.html#numpy.empty
np.empty(6)
np.empty(3)
np.empty(4)
array([0., 0., 0., 0., 0., 0.])
array([1., 1., 1.])
array([inf, inf, inf, inf])
5、创建一个线性序列数组,步长为2
https://www.numpy.org/devdocs/reference/generated/numpy.arange.html
>>>np.arange(0, 20, 2)
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
6、创建一个均匀分配的数组:
https://www.numpy.org/devdocs/reference/generated/numpy.linspace.html
>>>np.linspace(0, 1, 5)
array([0. , 0.25, 0.5 , 0.75, 1. ])
7、创建一个3 x 3,在0~1均匀分布的随机数组:
>>>np.random.random((3, 3))
array([[0.15284966, 0.76245701, 0.46865904],
[0.02143503, 0.18713218, 0.65109601],
[0.02995446, 0.77179684, 0.28884792]])
8、创建一个3 x 3的、均值为0、方差为1的正态分布随机数组
>>>np.random.normal(0, 1, (3, 3))
array([[ 0.07529344, 1.26555354, 0.15034909],
[-0.20187392, -0.48059882, 1.04924497],
[-0.08580298, -0.65383367, -0.59371904]])
9、创建一个3x3的随机整形数组:
>>>np.random.randint(0, 10, (3, 3))
array([[4, 1, 1],
[5, 7, 0],
[4, 6, 7]])
10、创建一个3x3的单位矩阵:
https://www.numpy.org/devdocs/reference/generated/numpy.eye.html?highlight=eye#numpy.eye
>>>np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
三、NumPy数组的属性
>>>np.random.seed(0) # 设置随机数种子
>>>x1 = np.random.randint(10, size=6) # 一维数组
>>>x2 = np.random.randint(10, size=(3, 4)) # 二维数组
>>>x3 = np.random.randint(10, size=(3, 4, 5)) # 三维数组
>>>print("x3 ndim:", x3.ndim)
>>>print("x3 shape:", x3.shape)
>>>print("x3 size:", x3.size)
>>>print("x3 dtype:", x3.dtype)
x3 ndim: 3
x3 shape: (3, 4, 5)
x3 size: 60
x3 dtype: int32
四、数组的索引和切片
与基本语法类似,这里就不再叙述。大体就是用 " :“来表示长度,” , "来表示维度。
五、数组的变形
https://www.numpy.org/devdocs/reference/generated/numpy.reshape.html?highlight=reshape#numpy.reshape 主要是用reshape函数:
>>>grid = np.arange(1, 10).reshape((3, 3))
>>>print(grid)
[[1 2 3]
[4 5 6]
[7 8 9]]
六、数组的拼接和分裂
(一)、数组的拼接
1、np.concatenate()
>>>x = np.array([1, 2, 3])
>>>y = np.array([3, 2, 1])
>>>np.concatenate([x, y])
array([1, 2, 3, 3, 2, 1])
np.concatenate()函数中axis解释:
- axis = 0,即沿着第一个轴拼接,也就是行拼接。此时要保证拼接数组的第一个维度相等。
- axis = 1,即沿着第二个轴拼接,也就是列拼接。此时要保证拼接数组的第二个维度相等。
- axis = none,即默认拼接为一个数组。
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
[3, 4, 6]])
>>> np.concatenate((a, b), axis=None)
array([1, 2, 3, 4, 5, 6])
2、np.vstack() 按行拼接
https://www.numpy.org/devdocs/reference/generated/numpy.vstack.html?highlight=vstack#numpy.vstack 即进行行拼接:
>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 3, 4])
>>> np.vstack((a,b))
array([[1, 2, 3],
[2, 3, 4]])
>>> a = np.array([[1], [2], [3]])
>>> b = np.array([[2], [3], [4]])
>>> np.vstack((a,b))
array([[1],
[2],
[3],
[2],
[3],
[4]])
3、np.hstack() 按列拼接
https://www.numpy.org/devdocs/reference/generated/numpy.hstack.html#numpy.hstack 即在列的方向上进行拼接:
>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>> np.hstack((a,b))
array([1, 2, 3, 2, 3, 4])
>>> a = np.array([[1],[2],[3]])
>>> b = np.array([[2],[3],[4]])
>>> np.hstack((a,b))
array([[1, 2],
[2, 3],
[3, 4]])
(二)、数组的分裂
1、np.split()
https://www.numpy.org/devdocs/reference/generated/numpy.split.html?highlight=split#numpy.split 后面的参数列表是分裂的位置,即分裂点。
>>> x = np.arange(8.0)
>>> np.split(x, [3, 5, 6, 10])
[array([0., 1., 2.]),
array([3., 4.]),
array([5.]),
array([6., 7.]),
array([], dtype=float64)]
2、np.hsplit() 按列分裂
>>> x = np.arange(16.0).reshape(4, 4)
>>> x
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.]])
>>> np.hsplit(x, 2)
[array([[ 0., 1.],
[ 4., 5.],
[ 8., 9.],
[12., 13.]]),
array([[ 2., 3.],
[ 6., 7.],
[10., 11.],
[14., 15.]])]
对于三维数组,分裂扔沿着第二轴:
>>> x = np.arange(8.0).reshape(2, 2, 2)
>>> x
array([[[0., 1.],
[2., 3.]],
[[4., 5.],
[6., 7.]]])
>>> np.hsplit(x, 2)
[array([[[0., 1.]],
[[4., 5.]]]),
array([[[2., 3.]],
[[6., 7.]]])]
3、np.vsplit() 按行分裂
https://www.numpy.org/devdocs/reference/generated/numpy.vsplit.html?highlight=vsplit
>>> x = np.arange(16.0).reshape(4, 4)
>>> x
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.]])
>>> np.vsplit(x, 2)
[array([[0., 1., 2., 3.],
[4., 5., 6., 7.]]), array([[ 8., 9., 10., 11.],
[12., 13., 14., 15.]])]
4、np.dsplit() 按第三维度分裂
https://www.numpy.org/devdocs/reference/generated/numpy.dsplit.html?highlight=dsplit
>>> x = np.arange(16.0).reshape(2, 2, 4)
>>> x
array([[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.]],
[[ 8., 9., 10., 11.],
[12., 13., 14., 15.]]])
>>> np.dsplit(x, 2)
[array([[[ 0., 1.],
[ 4., 5.]],
[[ 8., 9.],
[12., 13.]]]), array([[[ 2., 3.],
[ 6., 7.]],
[[10., 11.],
[14., 15.]]])]
>>> np.dsplit(x, np.array([3, 6]))
[array([[[ 0., 1., 2.],
[ 4., 5., 6.]],
[[ 8., 9., 10.],
[12., 13., 14.]]]),
array([[[ 3.],
[ 7.]],
[[11.],
[15.]]]),
array([], shape=(2, 2, 0), dtype=float64)]