【numpy常用知识速查】

目录

【numpy常用知识速查】

; 2. numpy创建空数组


x = np.empty((3,5), dtype=np.int8)
x

array([[  16,   71,   73, -119,   51],
       [   2,    0,    0,  112,  -78],
       [  54, -122,   51,    2,    0]], dtype=int8)

创建全0和全1的数组为

x = np.zeros((3,5), dtype=np.int8)
x = np.ones((3,5), dtype=np.int8)
  1. numpy.arrange()和numpy.linspace()
numpy.arange(start, stop, step, dtype)

【numpy常用知识速查】
x = np.arange(0,10,2,dtype=np.float)
print(x)

[0. 2. 4. 6. 8.]
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

【numpy常用知识速查】
x = np.linspace(10,20,20,dtype=np.float)
print(x)

[10.         10.52631579 11.05263158 11.57894737 12.10526316 12.63157895
 13.15789474 13.68421053 14.21052632 14.73684211 15.26315789 15.78947368
 16.31578947 16.84210526 17.36842105 17.89473684 18.42105263 18.94736842
 19.47368421 20.        ]

np.arange()是设置间距,而np.linspace()是设置数量。

  1. numpy的布尔索引
x = np.arange(20)
y = np.arange(40)
x = x.reshape(2,10)
y = y.reshape(2,10,2)
print('x为:\n', x)
print('y为:\n', y)
print('过滤后的x:\n', x[x>5])
print('过滤后的y:\n', y[y>5])

x为:
 [[ 0  1  2  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]]
y为:
 [[[ 0  1]
  [ 2  3]
  [ 4  5]
  [ 6  7]
  [ 8  9]
  [10 11]
  [12 13]
  [14 15]
  [16 17]
  [18 19]]

 [[20 21]
  [22 23]
  [24 25]
  [26 27]
  [28 29]
  [30 31]
  [32 33]
  [34 35]
  [36 37]
  [38 39]]]
过滤后的x:
 [ 6  7  8  9 10 11 12 13 14 15 16 17 18 19]
过滤后的y:
 [ 6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 30 31 32 33 34 35 36 37 38 39]
  1. 遍历多维数组的每一个元素(ndarray.flat)
x = np.arange(100).reshape(10,10)
for i in x.flat:
    print(i, end=' ')

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  1. 扩展数组的维度numpy.expand_dims(array, axis)

【numpy常用知识速查】
x = np.arange(20).reshape(5,4)
print('x的形状为:', x.shape,'\n')
y = np.expand_dims(x, axis=0)
print('y的形状为:', y.shape,'\n')

x的形状为: (5, 4)

y的形状为: (1, 5, 4)

在将图片数据输入神经网络时,有时需要将(Channel,Height,Width)类型的图片数据转换成(Number,Channel,Height,Width),这时就可以用np.expand_dims()函数了。

  1. 删除数组中的一维的轴np.squeeze()
x = np.arange(20).reshape(1,5,1,4,1)
print('x的形状为:', x.shape,'\n')
x = np.squeeze(x)
print('x的形状为:', x.shape,'\n')

x的形状为: (1, 5, 1, 4, 1)

x的形状为: (5, 4)

  1. 链接相同形状的数组np.concatenate()

【numpy常用知识速查】
x = np.arange(20).reshape(1, 5, 4)
y = np.arange(20,40).reshape(1, 5, 4)
print('x的形状为:', x.shape,'\n')
print('y的形状为:', y.shape,'\n')
a = np.concatenate((x,y), axis=0)
print('a的形状为:', a.shape,'\n')
print(a)

x的形状为: (1, 5, 4)

y的形状为: (1, 5, 4)

a的形状为: (2, 5, 4)

[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]
  [12 13 14 15]
  [16 17 18 19]]

 [[20 21 22 23]
  [24 25 26 27]
  [28 29 30 31]
  [32 33 34 35]
  [36 37 38 39]]]
  1. 堆叠相同形状的数组np.stack()

用法和np.concatenate()类似,但:
np.concatenate()不会增加数组的轴数量,只是在原来的数组后面塞入新的数组;
而np.stack()会在axis=0处新加一个轴:

x = np.arange(20).reshape(5, 4)
y = np.arange(20,40).reshape(5, 4)
print('x的形状为:', x.shape,'\n')
print('y的形状为:', y.shape,'\n')
a = np.stack((x,y), axis=0)
print('a的形状为:', a.shape,'\n')
print(a)

x的形状为: (5, 4)

y的形状为: (5, 4)

a的形状为: (2, 5, 4)

[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]
  [12 13 14 15]
  [16 17 18 19]]

 [[20 21 22 23]
  [24 25 26 27]
  [28 29 30 31]
  [32 33 34 35]
  [36 37 38 39]]]
  1. 在数组末尾添加值np.append()

说明:只有在x.shape = (n, a, b),y.shape=(m, a, b)时,才可以用np.append(),且必须指定axis=0,否则会变成一维数组

【numpy常用知识速查】
x = np.arange(20).reshape(5, 4)
y = np.arange(20,40).reshape(5, 4)
x = np.expand_dims(x, axis=0)
y = np.expand_dims(y, axis=0)
print('x的形状为:', x.shape,'\n')
print('y的形状为:', y.shape,'\n')
a = np.append(x, y, axis = 0)
print('a的形状为:', a.shape,'\n')
b = np.append(a, x, axis=0)
print('b的形状为:', b.shape,'\n')
print(b)

x的形状为: (1, 5, 4)

y的形状为: (1, 5, 4)

a的形状为: (2, 5, 4)

b的形状为: (3, 5, 4)

[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]
  [12 13 14 15]
  [16 17 18 19]]

 [[20 21 22 23]
  [24 25 26 27]
  [28 29 30 31]
  [32 33 34 35]
  [36 37 38 39]]

 [[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]
  [12 13 14 15]
  [16 17 18 19]]]
  1. 删除某一行或某一列np.delete()

【numpy常用知识速查】
x = np.arange(20).reshape(5, 4)
print('x是:\n', x)
y = np.delete(x, 1, axis=0)
z = np.delete(x, 1, axis=1)
print('删除第一行:\n', y)
print('删除第一列:\n', z)
a = np.delete(x, [0,1], axis=0)
print('删除第0行和第1行:\n', a)

x是:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]]
删除第一行:
 [[ 0  1  2  3]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]]
删除第一列:
 [[ 0  2  3]
 [ 4  6  7]
 [ 8 10 11]
 [12 14 15]
 [16 18 19]]
删除第0行和第1行:
 [[ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]]
  1. 去除重复值np.unique(arr)

如果arr不是一维数组,则会被展开

x = np.array([[1,2,3],[4,5,6],[1,2,9]])
print('x是:\n', x)
y = np.unique(x)
print('去重后:\n', y)

x是:
 [[1 2 3]
 [4 5 6]
 [1 2 9]]
去重后:
 [1 2 3 4 5 6 9]
  1. numpy取整函数

np.around():四舍五入
np.floor():向下取整
np.ceil():向上取整

  1. 求最大/最小值

14.1 返回元素:np.amax()/np.amin()


x = np.array([[1,2,3],[4,5,6],[7,8,9]])
print('x\n', x)
print('amin()\n',np.amin(x, axis=1))
print('amin()\n',np.amin(x, axis=0))
print('amin()\n',np.amin(x))

x
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
amin()
 [1 4 7]
amin()
 [1 2 3]
amin()
 1

14.2 返回索引:np.argmin()/np.argmax()


x = np.random.randint(1,100, size=(3,3))
print('x\n', x)
print('argmin()【axis=1】\n',np.argmin(x, axis=1))
print('argmin()【axis=0】\n',np.argmin(x, axis=0))
print('argmin()\n',np.argmin(x))

x
 [[78 23 43]
 [52 88 96]
 [46 19 82]]
argmin()【axis=1】
 [1 0 1]
argmin()【axis=0】
 [2 2 0]
argmin()
 7
  1. 把多维数组展开到一维x.flatten()
x = np.arange(20).reshape(1,2,2,5)
print('x的形状为:\n', x.shape)
print('将x展开到一维:\n', x.flatten())

x的形状为:
 (1, 2, 2, 5)
将x展开到一维:
 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
  1. 排序函数

16.1 返回数组np.sort()

x = np.array([[10,12,3],[24,15,76],[7,98,-9]])
print('x\n', x)
print('排序后:\n', np.sort(x))
print('排序后(axis=1):\n', np.sort(x, axis=1))
print('排序后(axis=0):\n', np.sort(x, axis=0))

x
 [[10 12  3]
 [24 15 76]
 [ 7 98 -9]]
排序后:
 [[ 3 10 12]
 [15 24 76]
 [-9  7 98]]
排序后(axis=1):
 [[ 3 10 12]
 [15 24 76]
 [-9  7 98]]
排序后(axis=0):
 [[ 7 12 -9]
 [10 15  3]
 [24 98 76]]

16.2 排序后返回索引np.argsort()

x = np.random.randint(1, 100, size=10)
print('x为:\n', x)
y = np.argsort(x)
print('排序后返回索引:\n', y)
print('x[y]:\n', x[y])

x为:
 [74 29 91 19  8 55 82 22 21 53]
排序后返回索引:
 [4 3 8 7 1 9 5 0 6 2]
x[y]:
 [ 8 19 21 22 29 53 55 74 82 91]
  1. 提取符合条件的元素np.where()
x = np.random.randint(1,10,size=(3,3))
print('x为:\n', x)
y = np.where(np.power(x, 2) > 50)
print('y为:\n', y)
print('y的类型为:\n', type(y))
print('x[y]为:\n', x[y])

x为:
 [[6 9 5]
 [7 7 6]
 [6 8 3]]
y为:
 (array([0, 2], dtype=int64), array([1, 1], dtype=int64))
y的类型为:
 <class 'tuple'>
x[y]为:
 [9 8]
  1. 随机数函数
np.random.randint(0, 10)

np.random.randint(0, 10, 5)

np.random.randint(0, 10, size=5)

np.random.randint(0, 10, size=(2, 5))

np.random.seed(100)
np.random.randint(0, 10, 5)

np.random.random()
np.random.random(5)
np.random.random((3, 5))

np.random.normal(size=5)
np.random.normal(size=(3, 5))

【numpy常用知识速查】
  1. 更多关于numpy的详细讲解(相对目前不常用)

numpy更多详细讲解

Original: https://blog.csdn.net/qq_44166630/article/details/121477224
Author: SinHao22
Title: 【numpy常用知识速查】

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/761048/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球