Numpy用法详细总结:学习numpy如何使用,看这一篇文章就足够了(中)

简介: Numpy用法详细总结:学习numpy如何使用,看这一篇文章就足够了(中)

2、切片


nd
array([[7, 9, 2, 3],
       [0, 2, 7, 3],
       [1, 9, 0, 1],
       [4, 1, 2, 8]])
nd[0:100] # 左闭右开的区间,右边可以无限大
array([[7, 9, 2, 3],
       [0, 2, 7, 3],
       [1, 9, 0, 1],
       [4, 1, 2, 8]])
lp[0:100]
[[1, 2, 3], [4, 5, 6], [7, 8]]
nd[:2]
array([[7, 9, 2, 3],
       [0, 2, 7, 3]])


nd[1:]
• 1
array([[0, 2, 7, 3],
       [1, 9, 0, 1],
       [4, 1, 2, 8]])
nd[3:0:-1] 
# 如果步长为负数,代表从后往前数,要求区间也是倒着的
• 1
• 2
array([[4, 1, 2, 8],
       [1, 9, 0, 1],
       [0, 2, 7, 3]])

nd
• 1
array([[7, 9, 2, 3],
       [0, 2, 7, 3],
       [1, 9, 0, 1],
       [4, 1, 2, 8]])

nd[:,0::2]
• 1
array([[7, 2],
       [0, 7],
       [1, 0],
       [4, 2]])

nd[1:3,0:2] # 即切行又切列
• 1
array([[0, 2],
       [1, 9]])


把girl倒过来


girl


array([[[225, 231, 231],
        [229, 235, 235],
        [222, 228, 228],
        ..., 
        [206, 213, 162],
        [211, 213, 166],
        [217, 220, 173]],
       [[224, 230, 230],
        [229, 235, 235],
        [223, 229, 229],
        ..., 
        [206, 213, 162],
        [211, 213, 166],
        [217, 220, 173]],
       [[224, 230, 230],
        [229, 235, 235],
        [223, 229, 229],
        ..., 
        [206, 213, 162],
        [211, 213, 166],
        [219, 221, 174]],
       ..., 
       [[175, 187, 213],
        [180, 192, 218],
        [175, 187, 213],
        ..., 
        [155, 162, 180],
        [153, 160, 178],
        [156, 163, 181]],
       [[175, 187, 213],
        [180, 192, 218],
        [174, 186, 212],
        ..., 
        [155, 162, 180],
        [153, 160, 178],
        [155, 162, 180]],
       [[177, 189, 215],
        [181, 193, 219],
        [174, 186, 212],
        ..., 
        [155, 162, 180],
        [153, 160, 178],
        [156, 163, 181]]], dtype=uint8)
plt.imshow(girl[::-2,::-2])
plt.show()


拼图小游戏:把女孩放在老虎背上


t = tigger.copy() # 
• 1
plt.imshow(tigger)
plt.show()

57379991c97043518dbb4669242a47df.png


girl2 = plt.imread("./source/girl2.jpg")
plt.imshow(girl2)
plt.show()

5f28f82f4f484a1ba767cfd580c8d35e.png


# 给老虎挖坑
tigger[150:450,300:600] = girl2
plt.imshow(tigger)
plt.show()


59af3ae4b08b4076bfa3993267dce1bb.png

3、变形


reshape()


resize()


tigger.shape
• 1
(786, 1200, 3)
• 1
nd = np.random.randint(0,10,size=12)
nd
array([4, 0, 1, 1, 8, 7, 7, 5, 3, 0, 7, 3])
• 1
nd.shape
• 1
(12,)
• 1
nd.reshape((3,2,2,1)) # 参数为一个元组,代表的就是要把nd变成一个什么形状

array([[[[4],
         [0]],
        [[1],
         [1]]],
       [[[8],
         [7]],
        [[7],
         [5]]],
       [[[3],
         [0]],
        [[7],
         [3]]]])
nd
• 1
array([4, 0, 1, 1, 8, 7, 7, 5, 3, 0, 7, 3])
nd.reshape((3,2))#cannot reshape array of size 12 into shape (3,8)
# 变形的时候size要保持一致

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-94-dda3397392b8> in <module>()
----> 1 nd.reshape((3,2))#cannot reshape array of size 12 into shape (3,8)
ValueError: cannot reshape array of size 12 into shape (3,2)
nd.resize((2,6))
• 1
nd
• 1
array([[4, 0, 1, 1, 8, 7],
       [7, 5, 3, 0, 7, 3]])


【注意】

  1)形变之前和形变之后的数组的size要保持一致,否则无法形变
  2)reshape()函数是把原数组拷贝副本以后对副本进行形变,并且把形变的结果返回
  3)resize()函数在原来的数组上进行形变,不需要返回结果


4、级联


级联:就是按照指定的维度把两个数组连在一起


nd1 = np.random.randint(0,10,size=(4,4))
nd2 = np.random.randint(20,40,size=(3,4))
• 1
• 2


print(nd1)
print(nd2)


[[2 5 6 1]
 [4 8 0 5]
 [9 4 7 8]
 [4 3 0 8]]
[[38 22 25 38]
 [22 38 30 21]
 [23 34 28 26]]
# 将两个数组进行级联
np.concatenate([nd1,nd2],axis=0)
# 参数1,是一个列表(或者元组),列表中是参与级联的那些数组
# 参数axis默认为0代表在行上(第0个维度)进行级联,1代表在列上(第1个维度)进行级联
array([[ 2,  5,  6,  1],
       [ 4,  8,  0,  5],
       [ 9,  4,  7,  8],
       [ 4,  3,  0,  8],
       [38, 22, 25, 38],
       [22, 38, 30, 21],
       [23, 34, 28, 26]])
np.concatenate([nd1,nd2],axis=1)
# 列级联需要行数一致
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-102-0a76346b819d> in <module>()
----> 1 np.concatenate([nd1,nd2],axis=1)
ValueError: all the input array dimensions except for the concatenation axis must match exactly
nd3 = np.random.randint(0,10,size=(4,3))
nd3
array([[1, 3, 7],
       [9, 5, 3],
       [9, 0, 2],
       [0, 7, 4]])
nd1
array([[2, 5, 6, 1],
       [4, 8, 0, 5],
       [9, 4, 7, 8],
       [4, 3, 0, 8]])
np.concatenate([nd1,nd3])
# 列数不一致,不能进行行级联
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-106-871caaeeb895> in <module>()
----> 1 np.concatenate([nd1,nd3])
ValueError: all the input array dimensions except for the concatenation axis must match exactly
np.concatenate([nd1,nd3],axis=1)
array([[2, 5, 6, 1, 1, 3, 7],
       [4, 8, 0, 5, 9, 5, 3],
       [9, 4, 7, 8, 9, 0, 2],
       [4, 3, 0, 8, 0, 7, 4]])


推广


1)形状一致才可以级联


nd4 = np.random.randint(0,10,size=(1,2,3))
nd5 = np.random.randint(0,10,size=(1,4,3))
print(nd4)
print(nd5)
[[[2 9 8]
  [9 5 6]]]
[[[9 9 6]
  [8 3 4]
  [8 7 7]
  [0 6 6]]]
np.concatenate([nd4,nd5],axis=1)
array([[[2, 9, 8],
        [9, 5, 6],
        [9, 9, 6],
        [8, 3, 4],
        [8, 7, 7],
        [0, 6, 6]]])
nd6 = np.random.randint(0,10,size=4)
nd6
array([3, 5, 3, 6])
• 1


2)维度不一致不能级联


np.concatenate([nd1,nd6])


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-124-6dd6213f71bc> in <module>()
----> 1 np.concatenate([nd1,nd6])
ValueError: all the input arrays must have same number of dimensions


级联需要注意的问题:


1)维度必须一样
2)形状必须相符(axis等于哪个维度,我们去掉这个维度以后,剩余的形状必须一致)
3)级联方向可以有axis来指定,默认是0


针对于二维数组还有hstack和vstack


nd = np.random.randint(0,10,size=(10,1))
nd


array([[1],
       [7],
       [6],
       [9],
       [0],
       [4],
       [6],
       [2],
       [0],
       [8]])
np.hstack(nd)
array([1, 7, 6, 9, 0, 4, 6, 2, 0, 8])
nd1 = np.random.randint(0,10,size=(10,2))
nd1
array([[4, 4],
       [3, 1],
       [3, 3],
       [9, 6],
       [5, 1],
       [4, 7],
       [3, 3],
       [4, 3],
       [7, 9],
       [6, 5]])
np.hstack(nd1)
array([4, 4, 3, 1, 3, 3, 9, 6, 5, 1, 4, 7, 3, 3, 4, 3, 7, 9, 6, 5])
np.vstack(nd1)
array([[4, 4],
       [3, 1],
       [3, 3],
       [9, 6],
       [5, 1],
       [4, 7],
       [3, 3],
       [4, 3],
       [7, 9],
       [6, 5]])
nd2 = np.random.randint(0,10,size=10)
nd2
array([1, 7, 4, 3, 9, 0, 3, 3, 2, 5])
np.vstack(nd2)
array([[1],
       [7],
       [4],
       [3],
       [9],
       [0],
       [3],
       [3],
       [2],
       [5]])
np.hstack(nd2)
array([1, 7, 4, 3, 9, 0, 3, 3, 2, 5])


hstack()把列数组改成行数组,把二维数组改成一维
vstack()把行数组改成列数组,把一维数组改成二维(把一维数组中的每一个元素作为一行)


5、切分


切分就是把一个数组切成多个


vsplit()

hsplit()

split()


nd = np.random.randint(0,100,size=(5,6))
nd
array([[17, 47, 83, 33, 69, 24],
       [60,  4, 34, 29, 75, 60],
       [33, 55, 67,  1, 76, 82],
       [31, 92,  1, 14, 83, 95],
       [59, 88, 81, 49, 70, 11]])
# 水平方向上切分
np.hsplit(nd,[1,4,5,8,9])
# 参数1,代表被切分的数组,参数2,是一个列表,代表了切分点的位置
[array([[17],
        [60],
        [33],
        [31],
        [59]]), array([[47, 83, 33],
        [ 4, 34, 29],
        [55, 67,  1],
        [92,  1, 14],
        [88, 81, 49]]), array([[69],
        [75],
        [76],
        [83],
        [70]]), array([[24],
        [60],
        [82],
        [95],
        [11]]), array([], shape=(5, 0), dtype=int32), array([], shape=(5, 0), dtype=int32)]
# 竖直方向上切分
np.vsplit(nd,[1,3,5])
[array([[17, 47, 83, 33, 69, 24]]), array([[60,  4, 34, 29, 75, 60],
        [33, 55, 67,  1, 76, 82]]), array([[31, 92,  1, 14, 83, 95],
        [59, 88, 81, 49, 70, 11]]), array([], shape=(0, 6), dtype=int32)]


split()函数


nd
array([[17, 47, 83, 33, 69, 24],
       [60,  4, 34, 29, 75, 60],
       [33, 55, 67,  1, 76, 82],
       [31, 92,  1, 14, 83, 95],
       [59, 88, 81, 49, 70, 11]])
np.split(nd,[1,2],axis=0)
# axis默认为0代表在第0个维度上进行切分,1代表切的是第1个维度
[array([[17, 47, 83, 33, 69, 24]]),
 array([[60,  4, 34, 29, 75, 60]]),
 array([[33, 55, 67,  1, 76, 82],
        [31, 92,  1, 14, 83, 95],
        [59, 88, 81, 49, 70, 11]])]


推广


nd1 = np.random.randint(0,10,size=(3,4,5))
nd1
array([[[5, 7, 8, 7, 9],
        [3, 6, 1, 9, 0],
        [6, 0, 2, 6, 9],
        [4, 5, 5, 3, 9]],
       [[6, 7, 6, 2, 3],
        [3, 0, 0, 5, 3],
        [9, 9, 0, 6, 2],
        [5, 4, 5, 4, 4]],
       [[8, 7, 4, 8, 9],
        [2, 2, 1, 7, 3],
        [2, 2, 9, 4, 7],
        [7, 3, 9, 4, 1]]])
np.split(nd1,[2],axis=2)
[array([[[5, 7],
         [3, 6],
         [6, 0],
         [4, 5]],
        [[6, 7],
         [3, 0],
         [9, 9],
         [5, 4]],
        [[8, 7],
         [2, 2],
         [2, 2],
         [7, 3]]]), array([[[8, 7, 9],
         [1, 9, 0],
         [2, 6, 9],
         [5, 3, 9]],
        [[6, 2, 3],
         [0, 5, 3],
         [0, 6, 2],
         [5, 4, 4]],
        [[4, 8, 9],
         [1, 7, 3],
         [9, 4, 7],
         [9, 4, 1]]])]


6、副本


nd = np.random.randint(0,100,size=6)
nd
array([34, 69, 14,  2, 48, 74])
nd1 = nd 
# 数组之间的赋值只是对地址一次拷贝,数组对象本身并没有被拷贝
• 1
• 2
nd1
• 1
array([34, 69, 14,  2, 48, 74])
nd1[0] = 100
• 1
nd1
• 1
array([100,  69,  14,   2,  48,  74])
• 1
nd
array([100,  69,  14,   2,  48,  74])
• 1
nd2 = nd.copy() 
# copy函数是把nd引用的那个数组也拷贝一份副本,并且把这个副本的地址存入了nd2
• 1
• 2
nd2[0] = 200000
• 1
nd
array([100,  69,  14,   2,  48,  74])
• 1
nd1
• 1


array([100,  69,  14,   2,  48,  74])

nd2
• 1
array([200000,     69,     14,      2,     48,     74])
• 1


讨论:由列表创建数组的过程有木有副本的创建


l = [1,2,3]
l 
• 1
• 2
[1, 2, 3]
• 1
nd = np.array(l)
nd
array([1, 2, 3])
• 1
nd[0] = 1000
• 1
l
• 1
[1, 2, 3]



说明:由列表创建数组的过程就是把列表拷贝出一个副本,然后把这个副本中的元素类型做一个统一化,然后放入数组对象中


四、ndarray的聚合操作


聚合操作指的就是对数组内部的数据进行某些特性的求解


1、求和


nd = np.random.randint(0,10,size=(3,4))
nd
• 1
• 2
array([[5, 9, 6, 8],
       [3, 7, 1, 9],
       [5, 7, 6, 3]])
nd.sum() # 完全聚合
• 1
69
• 1
nd.sum(axis=0) # 对行进行聚合(即对第0个维度进行聚合)
array([13, 23, 13, 20])
• 1
nd.sum(axis=1) # 对列进行聚合(即对第1个维度进行聚合)
• 1
array([28, 20, 21])


推广


nd = np.random.randint(0,10,size=(2,3,4))
nd
array([[[1, 0, 0, 3],
        [9, 6, 1, 8],
        [4, 9, 3, 9]],
       [[8, 0, 4, 3],
        [3, 0, 1, 8],
        [8, 0, 7, 4]]])
nd.sum()
• 1
99
• 1
nd.sum(axis=0)

array([[ 9,  0,  4,  6],
       [12,  6,  2, 16],
       [12,  9, 10, 13]])
nd.sum(axis=2)
• 1
array([[ 4, 24, 25],
       [15, 12, 19]])
• 1
• 2


聚合操作的规律:通过axis来改变聚合轴,axis=x的时候,第x的维度就会消失,把这个维度上对应的元素进行聚合


练习:给定一个4维矩阵,如何得到最后两维的和?


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


array([[[[3, 2, 9, 4, 0],
         [1, 0, 2, 3, 7],
         [4, 8, 6, 6, 5],
         [2, 3, 4, 1, 5]],
        [[3, 2, 0, 1, 3],
         [7, 3, 3, 4, 1],
         [0, 4, 0, 6, 9],
         [3, 8, 6, 0, 5]],
        [[5, 1, 3, 5, 0],
         [1, 4, 1, 8, 0],
         [9, 1, 9, 6, 5],
         [6, 1, 8, 5, 1]]],
       [[[7, 5, 3, 4, 5],
         [7, 8, 6, 7, 2],
         [9, 9, 5, 3, 4],
         [9, 2, 9, 7, 2]],
        [[3, 2, 9, 7, 7],
         [0, 8, 1, 3, 0],
         [1, 5, 5, 6, 5],
         [4, 8, 7, 2, 9]],
        [[1, 3, 5, 0, 6],
         [6, 0, 3, 5, 6],
         [2, 4, 6, 9, 0],
         [8, 7, 4, 0, 6]]]])


写法一


nd1.sum(axis=2).sum(axis=2)
• 1
array([[ 75,  68,  79],
       [113,  92,  81]])
• 1
• 2


写法二


nd1.sum(axis=-1).sum(axis=-1)
• 1
array([[ 75,  68,  79],
       [113,  92,  81]])
• 1
• 2


写法三


nd1.sum(axis=(-1,-2))
• 1
array([[ 75,  68,  79],
       [113,  92,  81]])
• 1
• 2


2、最值


nd


array([[[1, 0, 0, 3],
        [9, 6, 1, 8],
        [4, 9, 3, 9]],
       [[8, 0, 4, 3],
        [3, 0, 1, 8],
        [8, 0, 7, 4]]])
nd.sum(axis=-1)
• 1
array([[ 4, 24, 25],
       [15, 12, 19]])
• 1
• 2
nd.max()
• 1
9
• 1
nd.max(axis=-1)
array([[3, 9, 9],
       [8, 8, 8]])
• 1
• 2
nd.max(axis=1)
array([[9, 9, 3, 9],
       [8, 0, 7, 8]])
• 1
• 2
nd.min(axis=0)
array([[1, 0, 0, 3],
       [3, 0, 1, 8],
       [4, 0, 3, 4]])
相关文章
|
9月前
|
数据采集 机器学习/深度学习 数据可视化
深入学习NumPy库在数据分析中的应用场景
深入学习NumPy库在数据分析中的应用场景
|
9月前
|
存储 算法 数据挖掘
NumPy 数组学习手册:6~7
NumPy 数组学习手册:6~7
75 0
|
9月前
|
存储 索引 Python
一文掌握python数组numpy的全部用法(零基础学python(二))
一文掌握python数组numpy的全部用法(零基础学python(二))
|
4月前
|
Python
Numpy学习笔记(一):array()、range()、arange()用法
这篇文章是关于NumPy库中array()、range()和arange()函数的用法和区别的介绍。
135 6
Numpy学习笔记(一):array()、range()、arange()用法
|
9月前
|
存储 索引 Python
python学习——NumPy数值计算基础
NumPy基础知识概览:涉及nan(非数字)和inf(无穷)的概念,nan在文件读取或不适当计算时出现,inf在除0操作中出现。数组操作有深拷贝(a=b.copy())、浅拷贝(a=b[:])和引用(a=b)。创建数组方式多样,如`np.array()`、`np.arange()`等。数据类型转换如`np.float64()`、`np.int8()`。随机数生成包含均匀分布、正态分布等。数组索引和切片支持多维操作。改变数组形状用`reshape()`,展平用`ravel()`和`flatten()`。矩阵运算包括加减乘、转置、逆矩阵等。
102 2
python学习——NumPy数值计算基础
|
9月前
|
数据采集 数据挖掘 Python
numpy中的浅复制和深复制的详细用法(3)
numpy中的浅复制和深复制的详细用法(3)
numpy中的浅复制和深复制的详细用法(3)
|
9月前
|
vr&ar Python
轻松掌握Numpy日常用法,体验Numpy之快(二)
轻松掌握Numpy日常用法,体验Numpy之快(二)
|
9月前
|
存储 机器学习/深度学习 并行计算
轻松掌握Numpy日常用法,体验Numpy之快(一)
轻松掌握Numpy日常用法,体验Numpy之快(一)
|
9月前
|
存储 数据挖掘 Linux
NumPy 数组学习手册:1~5
NumPy 数组学习手册:1~5
110 0
|
9月前
|
Python
numpy与pandas的基础学习例子
numpy与pandas的基础学习例子
58 0