1. import numpy as np
  2. import cv2
  3. import matplotlib.pyplot as plt

一、数组的创建

1. 创建二维数组

  1. np.array([
  2. [1,2,3],
  3. [4,6,8],
  4. ])
  1. array([[1, 2, 3],
  2. [4, 6, 8]])

*数组宽度需要一致

  1. np.array([
  2. [1,2,3],
  3. [4,6,8,7],
  4. ])
  1. array([list([1, 2, 3]), list([4, 6, 8, 7])], dtype=object)

2. 和python类似的range()函数

  1. np.arange(2, 6, 0.5)
  1. array([2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5])

3. 创建元素全部为 1/0/随意/指定 的数组(ones、zeros、empty、full)

  1. np.ones(shape=(5,3))
  1. array([[1., 1., 1.],
  2. [1., 1., 1.],
  3. [1., 1., 1.],
  4. [1., 1., 1.],
  5. [1., 1., 1.]])
  1. np.ones_like(np.array([[1,2],[7,8]]))
  1. array([[1, 1],
  2. [1, 1]])

创建不初始化的数组

  1. np.empty(shape=(3,2,2))
  1. array([[[0., 0.],
  2. [0., 0.]],
  3. [[0., 0.],
  4. [0., 0.]],
  5. [[0., 0.],
  6. [0., 0.]]])

创建数组,指定形状和填充值

  1. np.full((3,4), 5)
  1. array([[5, 5, 5, 5],
  2. [5, 5, 5, 5],
  3. [5, 5, 5, 5]])

单位矩阵

  1. np.eye(5)
  1. array([[1., 0., 0., 0., 0.],
  2. [0., 1., 0., 0., 0.],
  3. [0., 0., 1., 0., 0.],
  4. [0., 0., 0., 1., 0.],
  5. [0., 0., 0., 0., 1.]])

等差数列

  1. np.linspace(0, 20, num=11, endpoint=True)
  1. array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20.])

等比数列

  1. np.logspace(1, 10, num=10, base=2)
  1. array([ 2., 4., 8., 16., 32., 64., 128., 256., 512.,
  2. 1024.])
1. range和linspace都可以产生等差数列;

2. arange指定的是步长,linspace和logspace指定的是区间内数值的个数。

二、数组和列表

array采用紧凑形式存储,即直接存储数据,而不是像列表一样存储地址

1. 创建数组和列表,进行时间和空间衡量

三、 数组运算

支持矢量运算(广播运算,内存对齐)

  1. a = np.array([1,2,3])
  1. b = np.array([
  2. [7,2,6],
  3. [6,5,4]
  4. ])
  1. c = 3

广播运算,对于形状不同的数组,进行扩展

  1. a+b+c
  1. array([[11, 7, 12],
  2. [10, 10, 10]])
  1. d = np.array([[1],[2],[3]])
  1. d+a
  1. array([[2, 3, 4],
  2. [3, 4, 5],
  3. [4, 5, 6]])

并不是所有情况都可以广播运算,有的时候,广播也不能够形式相同

  1. np.array([1,2])+a
  1. ---------------------------------------------------------------------------
  2. NameError Traceback (most recent call last)
  3. <ipython-input-1-d106fb5a0ffe> in <module>()
  4. ----> 1 np.array([1,2])+a
  5. NameError: name 'np' is not defined

四、数据类型

1. 指定数据类型

  1. np.array([1, 2.3, 2], dtype=np.float32)
  1. array([1. , 2.3, 2. ], dtype=float32)

2. 更改数据类型

  1. a = np.array([1, 2.3, 2], dtype=np.float32)
  2. a.astype(np.int32)
  1. array([1, 2, 2], dtype=int32)
  1. a.dtype=np.int32
  2. a
  1. array([1065353216, 1075000115, 1073741824], dtype=int32)
  1. a = np.arange(24)
  2. a.reshape((3,8))
  1. array([[ 0, 1, 2, 3, 4, 5, 6, 7],
  2. [ 8, 9, 10, 11, 12, 13, 14, 15],
  3. [16, 17, 18, 19, 20, 21, 22, 23]])
  1. np.reshape(a, (3, -1))
  1. array([[ 0, 1, 2, 3, 4, 5, 6, 7],
  2. [ 8, 9, 10, 11, 12, 13, 14, 15],
  3. [16, 17, 18, 19, 20, 21, 22, 23]])

五、 索引和切片

5.1 reshape可以实现维数的改变

  1. a = np.arange(12)
  2. b=a.reshape((3,4))
  3. b
  1. array([[ 0, 1, 2, 3],
  2. [ 4, 5, 6, 7],
  3. [ 8, 9, 10, 11]])

5.2 切片,返回原数组对象的视图,共享底层数据

  1. b[1:, 1:3]
  1. array([[ 5, 6],
  2. [ 9, 10]])
切片,返回视图,如果改变底层数据,另外一个也会发生影响
  1. c = b[:]
  2. c[0, 0] = 777
  3. b
  1. array([[777, 1, 2, 3],
  2. [ 4, 5, 6, 7],
  3. [ 8, 9, 10, 11]])
如果需要深拷贝,可以使用copy()
  1. c = b.copy()
  2. c[0, 0] = 888
  3. b
  1. array([[777, 1, 2, 3],
  2. [ 4, 5, 6, 7],
  3. [ 8, 9, 10, 11]])

5.3 数组索引

传入整形索引列表,返回的是新拷贝对象

  1. index = [5, 8, 2, 7]
  2. a[index]
  1. array([5, 8, 2, 7])
创建一个bool数组,可以根据布尔数组选择元素
  1. age = np.array([15, 135, 56, 15, 65, 123, 156, 96, 61, 166, 41, 20])
  2. a[age>50]
  1. array([1, 2, 4, 5, 6, 7, 8, 9])

*布尔数组元素个数必须和目标数组数量一致

  1. age1 = np.array([14, 135, 56, 35, 63, 123, 152, 96, 61, 162, 42, 20])
  2. age1 == age
  1. array([False, True, True, False, False, True, False, True, True,
  2. False, False, True])
  1. a[age1 == age]
  1. array([ 1, 2, 5, 7, 8, 11])
  1. a[(age1 == age) & (age>50)]
  1. array([1, 2, 5, 7, 8])

六、 数据扁平化

ravel是浅拷贝, flatten是深拷贝

  1. a = np.arange(10).reshape(2,5)
  2. b = a.flatten()
  3. b[0] = 777
  4. a
  1. array([[0, 1, 2, 3, 4],
  2. [5, 6, 7, 8, 9]])
  1. c = a.ravel()
  2. c[0] = 777
  3. a
  1. array([[777, 1, 2, 3, 4],
  2. [ 5, 6, 7, 8, 9]])

reshape会把原数组扁平化后,再进行结构化

  1. a = np.arange(24)
  2. a.reshape((6,4), order='c')
  1. array([[ 0, 1, 2, 3],
  2. [ 4, 5, 6, 7],
  3. [ 8, 9, 10, 11],
  4. [12, 13, 14, 15],
  5. [16, 17, 18, 19],
  6. [20, 21, 22, 23]])
  1. a.reshape((6,4), order='f')
  1. array([[ 0, 6, 12, 18],
  2. [ 1, 7, 13, 19],
  3. [ 2, 8, 14, 20],
  4. [ 3, 9, 15, 21],
  5. [ 4, 10, 16, 22],
  6. [ 5, 11, 17, 23]])

七、 统计函数

  • mean/sum
  • max/min
  • argmax/argmin
  • std/ver
  • consum/conprod
  1. a = np.array([
  2. [1,2,3],
  3. [8,6,1],
  4. [0,4,15],
  5. [8,52,32]
  6. ])
  1. a.shape
  1. (4, 3)

很重要的一个概念:轴

  1. a.sum(axis=0)
  1. array([17, 64, 51])
  1. a.sum(axis=1)
  1. array([ 6, 15, 19, 92])

当维度扩展到多维时,统计规则:

按照指定轴下标索引的方向统计

  1. x = np.arange(24).reshape(2,3,4)
  2. x.shape
  1. (2, 3, 4)
  1. x.sum(axis=0)
  1. array([[12, 14, 16, 18],
  2. [20, 22, 24, 26],
  3. [28, 30, 32, 34]])
  1. x.mean(axis=1)
  1. array([[ 4., 5., 6., 7.],
  2. [16., 17., 18., 19.]])

八、随机函数

1. 随机数

  1. np.random.rand() // 产生的区间为[0,1)
  1. 0.5876140647572566
  1. np.random.rand(2,3,4)
  1. array([[[0.39923779, 0.54564034, 0.29082511, 0.55575218],
  2. [0.9706234 , 0.46321137, 0.53333865, 0.45499656],
  3. [0.59093915, 0.71118072, 0.20639271, 0.63456103]],
  4. [[0.43380432, 0.67861236, 0.90540132, 0.41274326],
  5. [0.33723336, 0.64989966, 0.54551242, 0.50927546],
  6. [0.42052748, 0.88828045, 0.63187932, 0.88410905]]])

2. 标准正态分布

  1. np.random.rand(3,3)
  1. array([[0.4789705 , 0.06642018, 0.23518938],
  2. [0.38329877, 0.43547922, 0.28622591],
  3. [0.91541056, 0.04645713, 0.52973722]])

3. 指定标准差和均值的正态分布

  1. np.random.normal(size=(2,3), loc=5, scale=5)
  1. array([[ 6.65254795, 4.86225972, 5.30554417],
  2. [ 7.78982908, 9.07412392, 16.97508793]])

4. 随机整数

  1. np.random.seed(1)
  2. np.random.randint(5, 15, size=(8,8))
  1. array([[10, 13, 14, 10, 5, 5, 6, 12],
  2. [11, 14, 7, 9, 10, 7, 9, 7],
  3. [ 9, 12, 12, 14, 6, 12, 5, 11],
  4. [14, 14, 12, 11, 14, 6, 5, 6],
  5. [13, 13, 8, 14, 13, 12, 8, 11],
  6. [10, 6, 14, 8, 9, 13, 6, 9],
  7. [ 5, 8, 14, 7, 5, 9, 14, 7],
  8. [12, 12, 14, 13, 11, 14, 8, 12]])
  1. np.random.seed(1) // 设定相同的种子
  2. np.random.randint(5, 15, size=(8,8))
  1. array([[10, 13, 14, 10, 5, 5, 6, 12],
  2. [11, 14, 7, 9, 10, 7, 9, 7],
  3. [ 9, 12, 12, 14, 6, 12, 5, 11],
  4. [14, 14, 12, 11, 14, 6, 5, 6],
  5. [13, 13, 8, 14, 13, 12, 8, 11],
  6. [10, 6, 14, 8, 9, 13, 6, 9],
  7. [ 5, 8, 14, 7, 5, 9, 14, 7],
  8. [12, 12, 14, 13, 11, 14, 8, 12]])
  1. import random
  2. a = random.randrange(5, 15)
  3. b = random.randint(5, 15)
  4. a,b
  1. (13, 9)

5. 洗牌

  1. a = np.arange(10)
  2. np.random.shuffle(a)
  3. a
  1. array([0, 7, 1, 3, 8, 2, 9, 5, 4, 6])

6. 产生随机小数[a, b)

  1. np.random.uniform(2.1, 5.2)
  1. 2.816147808859026

九、 连接和拆分

  1. a = np.arange(12).reshape(3,2,2)
  2. b = np.arange(12, 24).reshape(3,2,2)
  3. c = np.concatenate((a,b), axis=2)
  4. c
  1. array([[[ 0, 1, 12, 13],
  2. [ 2, 3, 14, 15]],
  3. [[ 4, 5, 16, 17],
  4. [ 6, 7, 18, 19]],
  5. [[ 8, 9, 20, 21],
  6. [10, 11, 22, 23]]])
  1. np.split(c, 2, axis=2) // 切割数量必须能整除,
  1. [array([[[ 0, 1],
  2. [ 2, 3]],
  3. [[ 4, 5],
  4. [ 6, 7]],
  5. [[ 8, 9],
  6. [10, 11]]]), array([[[12, 13],
  7. [14, 15]],
  8. [[16, 17],
  9. [18, 19]],
  10. [[20, 21],
  11. [22, 23]]])]
  1. np.split(c, [3,], axis=2) // 还可以通过列表指定拆分位置,效果如[0-2][3-最后]
  1. [array([[[ 0, 1, 12],
  2. [ 2, 3, 14]],
  3. [[ 4, 5, 16],
  4. [ 6, 7, 18]],
  5. [[ 8, 9, 20],
  6. [10, 11, 22]]]), array([[[13],
  7. [15]],
  8. [[17],
  9. [19]],
  10. [[21],
  11. [23]]])]

十、其它函数

  1. a = np.arange(24).reshape(6,4)
  2. a.any()
  1. True
  1. a.all()
  1. False

转置(颠倒下标)

  1. a
  1. array([[ 0, 1, 2, 3],
  2. [ 4, 5, 6, 7],
  3. [ 8, 9, 10, 11],
  4. [12, 13, 14, 15],
  5. [16, 17, 18, 19],
  6. [20, 21, 22, 23]])
  1. a.T
  1. array([[ 0, 4, 8, 12, 16, 20],
  2. [ 1, 5, 9, 13, 17, 21],
  3. [ 2, 6, 10, 14, 18, 22],
  4. [ 3, 7, 11, 15, 19, 23]])

轴变换

  1. a = np.arange(24).reshape(2,3,4)
  2. a.transpose(2,0,1)
  1. array([[[ 0, 4, 8],
  2. [12, 16, 20]],
  3. [[ 1, 5, 9],
  4. [13, 17, 21]],
  5. [[ 2, 6, 10],
  6. [14, 18, 22]],
  7. [[ 3, 7, 11],
  8. [15, 19, 23]]])

十一、 运算

1. 乘法

  1. a = np.arange(24).reshape(6,4)
  2. b = np.arange(24).reshape(4,6)
  3. c = 5
  4. d = np.arange(4).T
  1. np.dot(a,c)
  1. array([[ 0, 5, 10, 15],
  2. [ 20, 25, 30, 35],
  3. [ 40, 45, 50, 55],
  4. [ 60, 65, 70, 75],
  5. [ 80, 85, 90, 95],
  6. [100, 105, 110, 115]])

点积(dot 和 @)

  1. np.dot(a, b)
  1. array([[ 84, 90, 96, 102, 108, 114],
  2. [ 228, 250, 272, 294, 316, 338],
  3. [ 372, 410, 448, 486, 524, 562],
  4. [ 516, 570, 624, 678, 732, 786],
  5. [ 660, 730, 800, 870, 940, 1010],
  6. [ 804, 890, 976, 1062, 1148, 1234]])
  1. a@d
  1. array([ 14, 38, 62, 86, 110, 134])
  1. a = np.arange(24).reshape(2,3,4)
  2. b = np.arange(24).reshape(2,4,3)
  3. a@b
  1. array([[[ 42, 48, 54],
  2. [ 114, 136, 158],
  3. [ 186, 224, 262]],
  4. [[ 906, 960, 1014],
  5. [1170, 1240, 1310],
  6. [1434, 1520, 1606]]])

注意:a的最后一维和b的倒数第二维长度相同才能点积

  1. (a@b).shape
  1. (2, 3, 3)

十二、 排序

  • np.sort() 返回新对象
  • 对象.sort() 就地修改
  1. x = np.array([1,43,5,7,3,43,43])

np.sort(对象)返回新对象

  1. y = np.sort(x)
  2. y
  1. array([ 1, 3, 5, 7, 43, 43, 43])
  1. x
  1. array([ 1, 43, 5, 7, 3, 43, 43])

对象运用排序方法,返回原对象

  1. x.sort()
  2. x
  1. array([ 1, 3, 5, 7, 43, 43, 43])

unique() 去重 & 排序

  1. np.unique(x)
  1. array([ 1, 3, 5, 7, 43])

三目运算

  1. a = np.array([1, 3, 5, 7, 43])
  2. b = np.array([4, 2, 56, 2, 1])
  3. np.where(a>b, a, b)
  1. array([ 4, 3, 56, 7, 43])

i/o操作

  1. a = np.arange(24).reshape(2,3,4)
  1. np.save("./" ,a)
  1. result = np.load("./.npy", )
  2. result
  1. array([[[ 0, 1, 2, 3],
  2. [ 4, 5, 6, 7],
  3. [ 8, 9, 10, 11]],
  4. [[12, 13, 14, 15],
  5. [16, 17, 18, 19],
  6. [20, 21, 22, 23]]])

练习,读取图像

  1. a = plt.imread('/home/geoffrey/图片/baidu.png')
  1. a.shape
  1. (258, 540, 4)
  1. plt.imshow(a)
  2. plt.show()

  1. result = np.split(a, 3)
  1. img_black = np.full((500, 500),fill_value=255, dtype=np.uint8)
  1. img_black
  1. array([[255, 255, 255, ..., 255, 255, 255],
  2. [255, 255, 255, ..., 255, 255, 255],
  3. [255, 255, 255, ..., 255, 255, 255],
  4. ...,
  5. [255, 255, 255, ..., 255, 255, 255],
  6. [255, 255, 255, ..., 255, 255, 255],
  7. [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)
  1. cv2.imshow('', img_black)
  2. cv2.WaitKey(0)
  1. ---------------------------------------------------------------------------
  2. AttributeError Traceback (most recent call last)
  3. <ipython-input-26-700b93714854> in <module>()
  4. 1 cv2.imshow('', img_black)
  5. ----> 2 cv2.WaitKey(0)
  6. AttributeError: module 'cv2.cv2' has no attribute 'WaitKey'
  1. plt.imshow(img_black)
  1. <matplotlib.image.AxesImage at 0x7f544658d128>

numpy学习笔记.的更多相关文章

  1. NumPy学习笔记 三 股票价格

    NumPy学习笔记 三 股票价格 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.&l ...

  2. NumPy学习笔记 二

    NumPy学习笔记 二 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...

  3. NumPy学习笔记 一

    NumPy学习笔记 一 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...

  4. numpy 学习笔记

    numpy 学习笔记 导入 numpy 包 import numpy as np 声明 ndarray 的几种方法 方法一,从list中创建 l = [[1,2,3], [4,5,6], [7,8,9 ...

  5. Numpy学习笔记(下篇)

    目录 Numpy学习笔记(下篇) 一.Numpy数组的合并与分割操作 1.合并操作 2.分割操作 二.Numpy中的矩阵运算 1.Universal Function 2.矩阵运算 3.向量和矩阵运算 ...

  6. Numpy学习笔记(上篇)

    目录 Numpy学习笔记(上篇) 一.Jupyter Notebook的基本使用 二.Jpuyter Notebook的魔法命令 1.%run 2.%timeit & %%timeit 3.% ...

  7. Python数据分析:Numpy学习笔记

    Numpy学习笔记 ndarray多维数组 创建 import numpy as np np.array([1,2,3,4]) np.array([1,2,3,4,],[5,6,7,8]) np.ze ...

  8. 数据分析之Pandas和Numpy学习笔记(持续更新)<1>

    pandas and numpy notebook        最近工作交接,整理电脑资料时看到了之前的基于Jupyter学习数据分析相关模块学习笔记.想着拿出来分享一下,可是Jupyter导出来h ...

  9. numpy学习笔记Ⅰ

    一直被numpy和matplotlib困扰,打算好好学习一下,也是从自己的观点,学对自己帮助最大的部分 主要参考<https: www.runoob.com="" numpy ...

  10. Python numpy学习笔记(一)

    下边代码是关于numpy的一些基本用法,包括数组和矩阵操作等... import numpy as np print "<== print version ==>" p ...

随机推荐

  1. Modbus库开发笔记之六:Modbus RTU Master开发

    这一节我们来封装最后一种应用(Modbus RTU Master应用),RTU主站的开发与TCP客户端的开发是一致的.同样的我们也不是做具体的应用,而是实现RTU主站的基本功能.我们将RTU主站的功能 ...

  2. Confluence 6 跟踪你安装中的自定义修改

    在 Confluence 中的系统信息(System Information)部分,有一个 修改(Modification)的选项.在这个选项中列出了自你 Confluence 安装以来,你 Conf ...

  3. Confluence 6 CSS 编辑技巧

    开始编辑空间样式表 一个空间的样式表是你开始对 CSS 进行自定义编辑的好的开始.在空间样式表中,包含了你所有可以进行修改的元素.当你对空间样式表进行编辑的时候,空间样式表的修改只会对你修改的空间有效 ...

  4. js中去除字符串中所有的html标签

    对于获取了一大堆字符串但是又不想要里面的html标签怎么办? 特别是像博客园这个富文本框中,可以带样式的,取出来的文章内容也是带样式的. 但是在某些地方只要显示文本不想显示其他标签,只好这样做. &l ...

  5. 五.Bash Shell编程基础入门实战

    知识回顾 运行脚本我们一般用sh 不用单独去加执行权限 OLDBOY=10只适用当前环境 局部变量 export OLDBOY把它设置为临时的环境变量应为已经=10了所以不用export OLDBOY ...

  6. 伪Ap接入点

    1.创建一个伪造的Ap接入点,必须购买一个无线网卡的设备,接受功率在300Mbps ,低于这个传输速率的值,效果很差,都达到用户可以连接验证的效果.其芯片必须支持kali linux 内核系统. 2. ...

  7. cf29d 深搜,dfs序

    #include<bits/stdc++.h> using namespace std; #define maxn 500 ]; int n,head[maxn],tot,a[maxn], ...

  8. CF508E

    贪心题是很有趣的... 首先,本题为括号匹配问题,那么可以考虑进行栈模拟 然后,我们思考一下如何匹配:虽然题目中仅对右括号的位置提出了区域性要求,但可以发现,对能匹配上的栈顶括号立刻进行匹配一定是一种 ...

  9. Python中的xxx+=xxx和xxx=xxx+xxx一些区别及执行过程

    预知小知识: Python中的变量与其他语言稍有差异,如a = 10并不是直接在内存中创建一个变量a其值为10,而是在内存中创建一个a这个a指向这个10,在Python中所有牵扯到等号的均不是值赋值, ...

  10. python发送邮件(在邮件中显示HTMLTestRunner生成的报告)

    import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart f ...