
NumPy数组的属性
(1)随机种子值: np.random.seed( ):使用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)) #二维数组
(2)数组的维度:.ndim;每个维度的大小:.shape;数组的总大小:.size。
print('x3 ndim: ', x3.ndim)print('x3 shape: ', x3.shape)print('x3 size: ', x3.size)
(3)数组的数据类型: .dtype。
print('x3 ndim: ', x3.ndim)print('x3 shape: ', x3.shape)print('x3 size: ', x3.size)
(4)每个元素字节大小: .itemsize;总字节大小: .nbytes
print('x3 itemsize: ', x3.itemsize)print('x3 nbytes: ', x3.nbytes)
x3 itemsize: 4x3 nbytes: 240
数组索引:获取单个元素
(1)正向索引单个元素。
print(x1[-1])
(2)从末尾索引单个元素。
print(x1[-1])
3)多维数组索引用逗号隔开。
print(x2[1, 0])print(x3[2, 1, 0])
也可直接修改元素的值
x2[1, 0] = 1print(x2)
NumPy数组是固定类型的,所以将一个浮点值插入一个整型数组时,浮点值会被截短成整型,如:
x2[1, 0] = 3.14print(x2)
数组的变形
数组的变形通过reshape()函数来实现。
如将数字1~9放入3×3的矩阵中,可采用如下方法:
1.对于reshape()函数,原始数组的大小必须和**变形后的数组大小(即size)**一致。
另外,还有常见的用法,将一维数组变为二维的行或列。这可以通过reshape()函数来实现,也可通过newaxis关键字来实现。
数组的拼接
(1)np.concatenate
拼接两个数组为一个:
x = np.array([1, 2, 3])y = np.array([4, 5, 6])x_y = np.concatenate([x, y])print(x_y)
将 arr
转换为2行的2维数组。
arr = np.arange(10)
import numpy as nparr = np.arange(10)# 方法1x = np.reshape(arr, newshape=[2, 5])print(x)# [[0 1 2 3 4]# [5 6 7 8 9]]# 方法2x = np.reshape(arr, newshape=[2, -1])print(x)# [[0 1 2 3 4]# [5 6 7 8 9]]
垂直堆叠数组a和数组b。
a = np.arange(10).reshape([2, -1])b = np.repeat(1, 10).reshape([2, -1])
import numpy as npa = np.arange(10).reshape([2, -1])b = np.repeat(1, 10).reshape([2, -1])print(a)# [[0 1 2 3 4]# [5 6 7 8 9]]print(b)# [[1 1 1 1 1]# [1 1 1 1 1]]# 方法1print(np.concatenate([a, b], axis=0))# [[0 1 2 3 4]# [5 6 7 8 9]# [1 1 1 1 1]# [1 1 1 1 1]]# 方法2print(np.vstack([a, b]))
将数组a与数组b水平堆叠。
a = np.arange(10).reshape([2, -1])b = np.repeat(1, 10).reshape([2, -1])
import numpy as npa = np.arange(10).reshape([2, -1])b = np.repeat(1, 10).reshape([2, -1])print(a)# [[0 1 2 3 4]# [5 6 7 8 9]]print(b)# [[1 1 1 1 1]# [1 1 1 1 1]]# 方法1print(np.concatenate([a, b], axis=1))# [[0 1 2 3 4 1 1 1 1 1]# [5 6 7 8 9 1 1 1 1 1]]# 方法2print(np.hstack([a, b]))# [[0 1 2 3 4 1 1 1 1 1]# [5 6 7 8 9 1 1 1 1 1]]
将 arr
的2维数组按列输出。
arr = np.array([[16, 17, 18, 19, 20],[11, 12, 13, 14, 15],[21, 22, 23, 24, 25],[31, 32, 33, 34, 35],[26, 27, 28, 29, 30]])
import numpy as nparr = np.array([[16, 17, 18, 19, 20],[11, 12, 13, 14, 15],[21, 22, 23, 24, 25],[31, 32, 33, 34, 35],[26, 27, 28, 29, 30]])y = arr.flatten(order='F')print(y)
给定两个随机数组A和B,验证它们是否相等。
A = np.random.randint(0,2,5) B = np.random.randint(0,2,5)
import numpy as npA = np.array([1,2,3])B = np.array([1,2,3])# Assuming identical shape of the arrays and a tolerance for the comparison of valuesequal = np.allclose(A,B)print(equal)# Checking both the shape and the element values, no tolerance (values have to be exactly equal)equal = np.array_equal(A,B)print(equal)
在给定的numpy数组中找到重复的条目(第二次出现以后),并将它们标记为True。第一次出现应为False。
import numpy as npnp.random.seed(100)a = np.random.randint(0, 5, 10)print(a)# [0 0 3 0 2 4 2 2 2 2]b = np.full(10, True)vals, counts = np.unique(a, return_index=True)b[counts] = Falseprint(b)# [False True False True False False True True True True]
建立一个随机数在1-10之间的3行2列的数组,并将其转换成2行3列的数组。
import numpy as npx = np.random.randint(10, size=[3, 2])print(x)y = np.reshape(x, [2,3])print(y)




