博客
关于我
Numpy 入门
阅读量:799 次
发布时间:2023-02-17

本文共 3857 字,大约阅读时间需要 12 分钟。

numpy是Python中的一个强大数值计算库,专注于处理和存储大型矩阵数据,具备比Python内置的list和tuple更高的处理效率。它在科学计算、数据分析等领域应用广泛。本文将详细介绍numpy矩阵的创建、操作方法以及相关功能。

一、矩阵的创建

1. 常见矩阵创建方法

  • 一维矩阵

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

    创建一个一维数组。

  • 二维矩阵

    array2 = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]])

    创建一个二维矩阵。

  • 三维矩阵

    array3 = np.array([[[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]]])

    创建一个三维矩阵。

  • 全零矩阵

    x = np.zeros((2, 3))print(x)

    输出结果:

    [[0 0 0] [0 0 0]]
  • 全一矩阵

    x = np.ones((2, 3))print(x)

    输出结果:

    [[1 1 1] [1 1 1]]
  • 空矩阵

    empty = np.empty((2, 3))print(empty)

    输出结果:

    [[0.00000000e+000  2.31584192e+077  1.48219694e-323] [nan               2.13839732e-314   4.17203541e-309]]
  • 全填充矩阵

    x = np.full((2, 2), 7)print(x)

    输出结果:

    [[7 7] [7 7]]
  • 单位矩阵

    x = np.eye(2)print(x)

    输出结果:

    [[1.  0.] [0.  1.]]
  • 按范围生成矩阵

    x = np.arange(10).reshape(2, 5)print(x)

    输出结果:

    [[0  1  2  3  4] [5  6  7  8  9]]

2. 随机矩阵创建

  • 随机值矩阵

    x = np.random.random((3, 2))print(x)

    输出结果:

    [[0.02877225  0.0536815 ] [0.95767806  0.63741724] [0.10975063  0.78859285]]
  • 随机整数矩阵

    x = np.random.randint(0, 10, (3, 2))print(x)

    输出结果:

    [[7 9] [1 8] [6 3]]
  • 随机正态矩阵

    x = np.random.normal(size=(3, 2))print(x)

    输出结果:

    [[1.24485604 -0.73626677] [1.3671711  -0.24442532] [-1.08691618 -0.6656498 ]]

二、矩阵的访问

1. 切片访问

  • 行和列的切片

    x = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])y = x[:3, 1:3]print(y)

    输出结果:

    [[2 3] [6 7] [10 11]]
  • 单元素访问

    print(x[2, 3])

    输出结果:12

  • 修改元素并反向访问

    y[0, 0] = 77print(x[0, 1])

    输出结果:77

2. 整型数组访问

  • 传统方式

    print(np.array([x[0, 0], x[1, 1], x[2, 0]]))

    输出结果:

    [1 4 5]
  • 切片方式

    print(x[[0, 1], [1, 0]])

    输出结果:

    [2 3]

3. 布尔型数组访问

  • 创建布尔索引

    x = np.array([[1, 2], [3, 4], [5, 6]])bool_idx = (x > 2)print(bool_idx)

    输出结果:

    [[False False] [True  True] [True  True]]
  • 筛选数据

    print(x[bool_idx])print(x[x > 2])

    输出结果:

    [3 4 5 6]

三、矩阵的计算

1. 基础运算

  • 加法

    x = np.array([[1, 2], [3, 4]])y = np.array([[5, 6], [7, 8]])print(np.add(x, y))

    输出结果:

    [[6 8] [10 12]]
  • 减法

    print(np.subtract(x, y))

    输出结果:

    [[-4 -4] [-4 -4]]
  • 乘法

    print(np.multiply(x, y))

    输出结果:

    [[5 12] [21 32]]
  • 除法

    print(np.divide(x, y))

    输出结果:

    [[0.2         0.33333333] [0.42857143  0.5       ]]
  • 平方根

    print(np.sqrt(x))

    输出结果:

    [[1.          1.41421356] [1.73205081  2.        ]]
  • 求和

    print(np.sum(x))print(np.sum(x, axis=0))print(np.sum(x, axis=1))

    输出结果:

    10[4 6][3 7]

2. 矩阵乘法

  • 矩阵乘法
    a = np.array([[1, 2], [3, 4]])b = np.array([11, 12])print(a.dot(b))

    输出结果:

    [35 81]

3. 矩阵转置

  • 矩阵转置
    a = np.array([[1, 2], [3, 4]])print(a.T)

    输出结果:

    [[1 3] [2 4]]

四、矩阵的参数获取

1. 维度

  • 矩阵维度
    x = np.array([1, 2, 3, 4, 5])y = np.array([[1, 2, 3], [4, 5, 6]])print(x.ndim, y.ndim)

    输出结果:

    1 2

2. 矩阵形状

  • 矩阵形状
    print(x.shape, y.shape)

    输出结果:

    (5,) (2, 3)

3. 矩阵大小

  • 矩阵大小
    print(x.size, y.size)

    输出结果:

    5 6

4. 元素类型

  • 元素类型
    print(x.dtype, y.dtype)

    输出结果:

    int64 int64

五、矩阵的合并

  • 横向合并

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

    输出结果:

    [[1 2 5 6] [3 4 7 8]]
  • 纵向合并

    d = np.vstack((a, b))print(d)

    输出结果:

    [[1 2] [3 4] [5 6] [7 8]]
  • 矩阵拼接

    e = np.concatenate((a, b), axis=0)print(e)

    输出结果:

    [[1 2] [3 4] [5 6] [7 8]]
  • 纵向拼接

    f = np.concatenate((a, b), axis=1)print(f)

    输出结果:

    [[1 2 5 6] [3 4 7 8]]

六、矩阵的切分

  • 行切分

    a = np.arange(0, 12).reshape((3, 4))print(a)

    输出结果:

    [[0 1 2 3] [4 5 6 7] [8 9 10 11]]
    b = np.hsplit(a, 2)print(b)

    输出结果:

    [[array([[0, 1], [4, 5], [8, 9]]), array([[2, 3], [6, 7], [10, 11]])]
  • 列切分

    c = np.vsplit(a, 1)print(c)

    输出结果:

    [[array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])]]
  • 混合切分

    d = np.split(a, 2, axis=1)print(d)

    输出结果:

    [[array([[0, 1], [4, 5], [8, 9]]), array([[2, 3], [6, 7], [10, 11]])]
  • 平均切分

    d = np.array_split(a, 3, axis=1)print(d)

    输出结果:

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

七、矩阵的深拷贝和浅拷贝

  • 浅拷贝示例

    a = np.arange(0, 8).reshape(4, 2)print(a)

    输出结果:

    [[0 1] [2 3] [4 5] [6 7]]
    b = a[2, :]b[0] = 11print(a)

    输出结果:

    [[0 1] [2 3] [11 5] [6 7]]
  • 深拷贝示例

    b = np.copy(a)[2, :]b[0] = 11print(a)

    输出结果:

    [[0 1] [2 3] [4 5] [6 7]]

通过本文中的内容,读者可以全面了解numpy矩阵的操作方法,包括矩阵的创建、访问、计算、合并、切分以及深浅拷贝等功能。这些操作对数据科学家和工程师进行科学计算和数据分析非常有用。

转载地址:http://igjfk.baihongyu.com/

你可能感兴趣的文章
Netty核心模块组件
查看>>
Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
查看>>
Netty源码—2.Reactor线程模型一
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty相关
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>
Network Sniffer and Connection Analyzer
查看>>
NetworkX系列教程(11)-graph和其他数据格式转换
查看>>
Networkx读取军械调查-ITN综合传输网络?/读取GML文件
查看>>
Net与Flex入门
查看>>
net包之IPConn
查看>>
NFinal学习笔记 02—NFinalBuild
查看>>