欢迎光临
我们一直在努力

Python NumPy - 初识 NumPy 科学计算的核心库

在这里插入图片描述

👋 大家好,欢迎来到我的技术博客! 📚 在这里,我会分享学习笔记、实战经验与技术思考,力求用简单的方式讲清楚复杂的问题。 🎯 本文将围绕NumPy这个话题展开,希望能为你带来一些启发或实用的参考。 🌱 无论你是刚入门的新手,还是正在进阶的开发者,希望你都能有所收获!


文章目录

  • Python NumPy – 初识 NumPy 科学计算的核心库 🚀
    • 引言
    • 什么是 NumPy?
      • NumPy 的核心特性 ✨
    • 安装和导入 NumPy
      • 安装 NumPy
      • 导入 NumPy
    • NumPy 数组基础
      • 创建 NumPy 数组
      • 数组的基本属性
    • 数组的数据类型
      • 数据类型转换
    • 数组索引和切片
      • 一维数组的索引和切片
      • 多维数组的索引和切片
      • 高级索引技巧
    • 数组操作和函数
      • 基本数学运算
      • 通用函数 (Universal Functions)
      • 统计函数
    • 广播机制 (Broadcasting)
    • 数组变形和重塑
    • 数组合并和分割
      • 数组合并
      • 数组分割
    • 随机数生成
    • 线性代数运算
    • 文件输入输出
    • 性能优化技巧
      • 向量化操作 vs 循环
      • 内存管理技巧
    • 实际应用示例
      • 图像处理基础
      • 时间序列分析
      • 科学计算应用
    • NumPy 与其他库的集成
      • 与 Matplotlib 的集成
      • 与 Pandas 的集成
    • 高级 NumPy 技巧
      • 结构化数组
      • 掩码数组
    • 错误处理和调试技巧
      • 常见错误及其解决方法
      • 调试技巧
    • 性能基准测试
    • NumPy 在机器学习中的应用
      • 简单的线性回归实现
      • K-means 聚类算法基础实现
    • NumPy 最佳实践
      • 编程规范和建议
      • 内存管理和优化
    • 常见问题解答
      • Q1: NumPy 数组和 Python 列表有什么区别?
      • Q2: 如何处理 NumPy 中的缺失值?
      • Q3: 如何优化 NumPy 代码的性能?
    • 学习资源推荐
    • 总结

Python NumPy – 初识 NumPy 科学计算的核心库 🚀

引言

在数据科学和机器学习的世界中,NumPy 无疑是最基础也是最重要的库之一。作为 Python 生态系统中科学计算的核心库,NumPy 为我们提供了强大的多维数组对象和各种数学函数,使得处理大规模数值数据变得简单高效。🌟

无论是进行数据分析、机器学习还是科学计算,NumPy 都扮演着不可或缺的角色。它不仅为其他高级库如 Pandas、Scikit-learn、Matplotlib 等提供了底层支持,还直接为开发者提供了高效的数值计算能力。

在本文中,我们将深入探索 NumPy 的世界,从基础概念到高级应用,通过丰富的代码示例来展示这个强大库的魅力。让我们一起踏上这段 NumPy 学习之旅吧!🚀

什么是 NumPy?

NumPy(Numerical Python)是一个开源的 Python 库,专门用于处理大型多维数组和矩阵运算。它是 Python 数据科学生态系统的基础,几乎所有的科学计算库都依赖于 NumPy。

NumPy 的核心特性 ✨

  • 高效的多维数组对象:提供了一个强大的 N 维数组对象 ndarray
  • 广播功能:允许不同形状的数组进行算术运算
  • 集成 C/C++ 和 Fortran 代码:可以直接使用这些语言编写的代码
  • 线性代数、傅里叶变换和随机数生成功能
  • 高性能:由于底层是用 C 实现的,速度非常快
  • import numpy as np

    # 创建一个简单的 NumPy 数组
    arr = np.array([1, 2, 3, 4, 5])
    print(f"NumPy 数组: {arr}")
    print(f"数组类型: {type(arr)}")

    安装和导入 NumPy

    安装 NumPy

    如果你还没有安装 NumPy,可以通过 pip 进行安装:

    pip install numpy

    或者如果你使用的是 conda:

    conda install numpy

    导入 NumPy

    在 Python 脚本中,我们通常这样导入 NumPy:

    import numpy as np

    使用别名 np 是 NumPy 社区的标准做法,这样可以让代码更加简洁易读。

    NumPy 数组基础

    创建 NumPy 数组

    NumPy 提供了多种创建数组的方法,让我们来看看一些常用的方式:

    import numpy as np

    # 从 Python 列表创建数组
    list_data = [1, 2, 3, 4, 5]
    arr_from_list = np.array(list_data)
    print(f"从列表创建的数组: {arr_from_list}")

    # 创建全零数组
    zeros_arr = np.zeros(5)
    print(f"全零数组: {zeros_arr}")

    # 创建全一数组
    ones_arr = np.ones((3, 4))
    print(f"全一数组:\\n{ones_arr}")

    # 创建指定范围的数组
    range_arr = np.arange(0, 10, 2)
    print(f"范围数组: {range_arr}")

    # 创建等间距数组
    linspace_arr = np.linspace(0, 1, 5)
    print(f"等间距数组: {linspace_arr}")

    # 创建单位矩阵
    identity_matrix = np.eye(3)
    print(f"单位矩阵:\\n{identity_matrix}")

    数组的基本属性

    每个 NumPy 数组都有重要的属性,帮助我们了解数组的结构:

    import numpy as np

    # 创建一个多维数组
    arr = np.array([[1, 2, 3], [4, 5, 6]])
    print(f"数组内容:\\n{arr}")
    print(f"数组形状: {arr.shape}")
    print(f"数组维度: {arr.ndim}")
    print(f"数组大小: {arr.size}")
    print(f"元素数据类型: {arr.dtype}")
    print(f"每个元素的字节大小: {arr.itemsize}")

    #mermaid-svg-P4QZE52x11TPfJZZ{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-P4QZE52x11TPfJZZ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-P4QZE52x11TPfJZZ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-P4QZE52x11TPfJZZ .error-icon{fill:#552222;}#mermaid-svg-P4QZE52x11TPfJZZ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-P4QZE52x11TPfJZZ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-P4QZE52x11TPfJZZ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-P4QZE52x11TPfJZZ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-P4QZE52x11TPfJZZ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-P4QZE52x11TPfJZZ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-P4QZE52x11TPfJZZ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-P4QZE52x11TPfJZZ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-P4QZE52x11TPfJZZ .marker.cross{stroke:#333333;}#mermaid-svg-P4QZE52x11TPfJZZ svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-P4QZE52x11TPfJZZ p{margin:0;}#mermaid-svg-P4QZE52x11TPfJZZ .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-P4QZE52x11TPfJZZ .cluster-label text{fill:#333;}#mermaid-svg-P4QZE52x11TPfJZZ .cluster-label span{color:#333;}#mermaid-svg-P4QZE52x11TPfJZZ .cluster-label span p{background-color:transparent;}#mermaid-svg-P4QZE52x11TPfJZZ .label text,#mermaid-svg-P4QZE52x11TPfJZZ span{fill:#333;color:#333;}#mermaid-svg-P4QZE52x11TPfJZZ .node rect,#mermaid-svg-P4QZE52x11TPfJZZ .node circle,#mermaid-svg-P4QZE52x11TPfJZZ .node ellipse,#mermaid-svg-P4QZE52x11TPfJZZ .node polygon,#mermaid-svg-P4QZE52x11TPfJZZ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-P4QZE52x11TPfJZZ .rough-node .label text,#mermaid-svg-P4QZE52x11TPfJZZ .node .label text,#mermaid-svg-P4QZE52x11TPfJZZ .image-shape .label,#mermaid-svg-P4QZE52x11TPfJZZ .icon-shape .label{text-anchor:middle;}#mermaid-svg-P4QZE52x11TPfJZZ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-P4QZE52x11TPfJZZ .rough-node .label,#mermaid-svg-P4QZE52x11TPfJZZ .node .label,#mermaid-svg-P4QZE52x11TPfJZZ .image-shape .label,#mermaid-svg-P4QZE52x11TPfJZZ .icon-shape .label{text-align:center;}#mermaid-svg-P4QZE52x11TPfJZZ .node.clickable{cursor:pointer;}#mermaid-svg-P4QZE52x11TPfJZZ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-P4QZE52x11TPfJZZ .arrowheadPath{fill:#333333;}#mermaid-svg-P4QZE52x11TPfJZZ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-P4QZE52x11TPfJZZ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-P4QZE52x11TPfJZZ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-P4QZE52x11TPfJZZ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-P4QZE52x11TPfJZZ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-P4QZE52x11TPfJZZ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-P4QZE52x11TPfJZZ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-P4QZE52x11TPfJZZ .cluster text{fill:#333;}#mermaid-svg-P4QZE52x11TPfJZZ .cluster span{color:#333;}#mermaid-svg-P4QZE52x11TPfJZZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-P4QZE52x11TPfJZZ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-P4QZE52x11TPfJZZ rect.text{fill:none;stroke-width:0;}#mermaid-svg-P4QZE52x11TPfJZZ .icon-shape,#mermaid-svg-P4QZE52x11TPfJZZ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-P4QZE52x11TPfJZZ .icon-shape p,#mermaid-svg-P4QZE52x11TPfJZZ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-P4QZE52x11TPfJZZ .icon-shape .label rect,#mermaid-svg-P4QZE52x11TPfJZZ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-P4QZE52x11TPfJZZ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-P4QZE52x11TPfJZZ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-P4QZE52x11TPfJZZ :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    NumPy 数组

    形状 Shape

    维度 ndim

    大小 size

    数据类型 dtype

    元素大小 itemsize

    数组的数据类型

    NumPy 支持多种数据类型,这使得我们可以根据需要选择最合适的数据类型来节省内存或提高性能:

    import numpy as np

    # 不同数据类型的数组
    int_arr = np.array([1, 2, 3], dtype=np.int32)
    float_arr = np.array([1.0, 2.0, 3.0], dtype=np.float64)
    bool_arr = np.array([True, False, True], dtype=np.bool_)

    print(f"整型数组: {int_arr}, 类型: {int_arr.dtype}")
    print(f"浮点数组: {float_arr}, 类型: {float_arr.dtype}")
    print(f"布尔数组: {bool_arr}, 类型: {bool_arr.dtype}")

    # 显式指定数据类型
    explicit_arr = np.array([1, 2, 3], dtype=np.float32)
    print(f"显式指定类型的数组: {explicit_arr}, 类型: {explicit_arr.dtype}")

    数据类型转换

    有时候我们需要在不同的数据类型之间进行转换:

    import numpy as np

    # 创建一个整型数组
    int_arr = np.array([1, 2, 3, 4, 5])
    print(f"原始数组: {int_arr}, 类型: {int_arr.dtype}")

    # 转换为浮点型
    float_arr = int_arr.astype(np.float64)
    print(f"转换后的数组: {float_arr}, 类型: {float_arr.dtype}")

    # 使用字符串指定类型
    str_float_arr = int_arr.astype('float32')
    print(f"字符串指定类型: {str_float_arr}, 类型: {str_float_arr.dtype}")

    数组索引和切片

    NumPy 数组的索引和切片操作与 Python 列表类似,但更加灵活和强大:

    一维数组的索引和切片

    import numpy as np

    # 创建一维数组
    arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])

    # 基本索引
    print(f"第一个元素: {arr[0]}")
    print(f"最后一个元素: {arr[1]}")

    # 切片操作
    print(f"前三个元素: {arr[:3]}")
    print(f"从第四个开始的所有元素: {arr[3:]}")
    print(f"中间的元素: {arr[2:7]}")
    print(f"每隔一个取元素: {arr[::2]}")
    print(f"逆序数组: {arr[::-1]}")

    多维数组的索引和切片

    import numpy as np

    # 创建二维数组
    arr_2d = np.array([[1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]])

    print(f"二维数组:\\n{arr_2d}")

    # 访问特定元素
    print(f"第一行第二列的元素: {arr_2d[0, 1]}")
    print(f"第三行第四列的元素: {arr_2d[2, 3]}")

    # 行切片
    print(f"第一行: {arr_2d[0]}")
    print(f"前两行:\\n{arr_2d[:2]}")

    # 列切片
    print(f"第一列: {arr_2d[:, 0]}")
    print(f"后两列:\\n{arr_2d[:, 2:]}")

    # 同时对行和列进行切片
    print(f"子矩阵 (前两行,后两列):\\n{arr_2d[:2, 2:]}")

    高级索引技巧

    NumPy 还支持更高级的索引方式,如布尔索引和花式索引:

    import numpy as np

    # 布尔索引
    arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    mask = arr > 5
    print(f"大于5的元素: {arr[mask]}")

    # 复合条件
    complex_mask = (arr > 3) & (arr < 8)
    print(f"大于3且小于8的元素: {arr[complex_mask]}")

    # 花式索引
    indices = [0, 2, 4, 6, 8]
    print(f"指定索引的元素: {arr[indices]}")

    # 二维数组的花式索引
    arr_2d = np.random.randint(0, 20, (4, 4))
    row_indices = [0, 2]
    col_indices = [1, 3]
    print(f"二维数组:\\n{arr_2d}")
    print(f"指定行列位置的元素: {arr_2d[row_indices, col_indices]}")

    数组操作和函数

    NumPy 提供了大量的数学函数和操作方法,让数组计算变得简单而高效:

    基本数学运算

    import numpy as np

    # 创建测试数组
    arr1 = np.array([1, 2, 3, 4])
    arr2 = np.array([5, 6, 7, 8])

    print(f"数组1: {arr1}")
    print(f"数组2: {arr2}")

    # 基本运算
    print(f"加法: {arr1 + arr2}")
    print(f"减法: {arr1 arr2}")
    print(f"乘法: {arr1 * arr2}")
    print(f"除法: {arr1 / arr2}")
    print(f"幂运算: {arr1 ** 2}")

    # 与标量运算
    scalar = 3
    print(f"数组1乘以标量{scalar}: {arr1 * scalar}")
    print(f"数组2除以标量{scalar}: {arr2 / scalar}")

    通用函数 (Universal Functions)

    NumPy 提供了许多通用函数,这些函数可以对数组中的每个元素进行操作:

    import numpy as np

    # 创建测试数组
    arr = np.array([2, 1, 0, 1, 2])

    print(f"原数组: {arr}")

    # 数学函数
    print(f"绝对值: {np.abs(arr)}")
    print(f"平方根: {np.sqrt(np.abs(arr))}") # 注意负数不能开平方根
    print(f"指数: {np.exp(arr)}")
    print(f"自然对数: {np.log(np.abs(arr) + 1)}") # 避免log(0)
    print(f"正弦值: {np.sin(arr)}")

    # 比较函数
    print(f"最大值: {np.max(arr)}")
    print(f"最小值: {np.min(arr)}")
    print(f"平均值: {np.mean(arr)}")
    print(f"标准差: {np.std(arr)}")

    统计函数

    NumPy 提供了丰富的统计函数,方便进行数据分析:

    import numpy as np

    # 创建测试数组
    data = np.random.normal(100, 15, 1000) # 正态分布数据

    print(f"数据样本数量: {len(data)}")
    print(f"均值: {np.mean(data):.2f}")
    print(f"中位数: {np.median(data):.2f}")
    print(f"标准差: {np.std(data):.2f}")
    print(f"方差: {np.var(data):.2f}")
    print(f"最大值: {np.max(data):.2f}")
    print(f"最小值: {np.min(data):.2f}")

    # 百分位数
    print(f"25%分位数: {np.percentile(data, 25):.2f}")
    print(f"75%分位数: {np.percentile(data, 75):.2f}")

    广播机制 (Broadcasting)

    广播是 NumPy 中一个非常强大的特性,它允许不同形状的数组进行算术运算:

    import numpy as np

    # 创建不同形状的数组
    arr_2d = np.array([[1, 2, 3],
    [4, 5, 6]])
    arr_1d = np.array([10, 20, 30])

    print(f"二维数组:\\n{arr_2d}")
    print(f"一维数组: {arr_1d}")

    # 广播加法
    result = arr_2d + arr_1d
    print(f"广播加法结果:\\n{result}")

    # 标量广播
    scalar = 5
    print(f"标量: {scalar}")
    broadcast_result = arr_2d + scalar
    print(f"标量广播结果:\\n{broadcast_result}")

    # 更复杂的广播例子
    arr_3d = np.random.randint(0, 10, (2, 3, 4))
    arr_1d_broadcast = np.array([1, 2, 3, 4])

    print(f"\\n三维数组形状: {arr_3d.shape}")
    print(f"一维数组形状: {arr_1d_broadcast.shape}")

    # 广播运算
    broadcasted_sum = arr_3d + arr_1d_broadcast
    print(f"广播后结果形状: {broadcasted_sum.shape}")

    数组变形和重塑

    NumPy 提供了多种方法来改变数组的形状,这对于数据处理非常重要:

    import numpy as np

    # 创建一个一维数组
    arr_1d = np.arange(12)
    print(f"原始一维数组: {arr_1d}")

    # 重塑为二维数组
    arr_2d = arr_1d.reshape(3, 4)
    print(f"重塑为3×4二维数组:\\n{arr_2d}")

    # 重塑为三维数组
    arr_3d = arr_1d.reshape(2, 2, 3)
    print(f"重塑为2x2x3三维数组:\\n{arr_3d}")

    # 展平数组
    flattened = arr_2d.flatten()
    print(f"展平后的数组: {flattened}")

    # 转置数组
    transposed = arr_2d.T
    print(f"转置后的数组:\\n{transposed}")

    # 扁平迭代器
    print("使用扁平迭代器遍历二维数组:")
    for element in arr_2d.flat:
    print(element, end=' ')
    print() # 换行

    数组合并和分割

    在实际应用中,我们经常需要将多个数组合并或将一个数组分割成多个部分:

    数组合并

    import numpy as np

    # 创建测试数组
    arr1 = np.array([[1, 2], [3, 4]])
    arr2 = np.array([[5, 6], [7, 8]])

    print(f"数组1:\\n{arr1}")
    print(f"数组2:\\n{arr2}")

    # 按行合并 (垂直合并)
    vstacked = np.vstack((arr1, arr2))
    print(f"垂直合并结果:\\n{vstacked}")

    # 按列合并 (水平合并)
    hstacked = np.hstack((arr1, arr2))
    print(f"水平合并结果:\\n{hstacked}")

    # 使用 concatenate 函数
    concat_axis0 = np.concatenate((arr1, arr2), axis=0)
    concat_axis1 = np.concatenate((arr1, arr2), axis=1)
    print(f"沿轴0合并:\\n{concat_axis0}")
    print(f"沿轴1合并:\\n{concat_axis1}")

    # 在新轴上合并
    new_axis = np.stack((arr1, arr2), axis=0)
    print(f"在新轴上合并 (axis=0):\\n{new_axis}")

    数组分割

    import numpy as np

    # 创建测试数组
    arr = np.arange(12).reshape(3, 4)
    print(f"原始数组:\\n{arr}")

    # 水平分割
    hsplit_arrays = np.hsplit(arr, 2)
    print("水平分割结果:")
    for i, sub_arr in enumerate(hsplit_arrays):
    print(f"子数组{i}:\\n{sub_arr}")

    # 垂直分割
    vsplit_arrays = np.vsplit(arr, 3)
    print("垂直分割结果:")
    for i, sub_arr in enumerate(vsplit_arrays):
    print(f"子数组{i}:\\n{sub_arr}")

    # 使用 array_split 进行不均匀分割
    uneven_split = np.array_split(arr, 5, axis=1)
    print("不均匀分割结果:")
    for i, sub_arr in enumerate(uneven_split):
    print(f"子数组{i}:\\n{sub_arr}")

    随机数生成

    NumPy 的 random 模块提供了丰富的随机数生成功能:

    import numpy as np

    # 设置随机种子以确保结果可重现
    np.random.seed(42)

    # 生成随机浮点数数组
    random_floats = np.random.random((3, 4))
    print(f"随机浮点数数组:\\n{random_floats}")

    # 生成指定范围内的随机整数
    random_integers = np.random.randint(1, 10, size=(2, 5))
    print(f"随机整数数组:\\n{random_integers}")

    # 生成正态分布随机数
    normal_dist = np.random.normal(0, 1, 1000) # 均值0,标准差1
    print(f"正态分布样本的前10个值: {normal_dist[:10]}")
    print(f"样本均值: {np.mean(normal_dist):.3f}")
    print(f"样本标准差: {np.std(normal_dist):.3f}")

    # 生成均匀分布随机数
    uniform_dist = np.random.uniform(1, 1, 100)
    print(f"均匀分布样本的前10个值: {uniform_dist[:10]}")

    # 随机打乱数组
    original_array = np.arange(10)
    shuffled_array = original_array.copy()
    np.random.shuffle(shuffled_array)
    print(f"原始数组: {original_array}")
    print(f"打乱后的数组: {shuffled_array}")

    # 随机选择
    choices = np.random.choice([1, 2, 3, 4, 5], size=10, replace=True)
    print(f"随机选择的结果: {choices}")

    线性代数运算

    NumPy 的 linalg 模块提供了丰富的线性代数运算功能:

    import numpy as np

    # 创建测试矩阵
    A = np.array([[1, 2], [3, 4]])
    B = np.array([[5, 6], [7, 8]])

    print(f"矩阵A:\\n{A}")
    print(f"矩阵B:\\n{B}")

    # 矩阵乘法
    matrix_product = np.dot(A, B)
    print(f"矩阵乘积 A·B:\\n{matrix_product}")

    # 或者使用 @ 运算符
    matrix_product_alt = A @ B
    print(f"矩阵乘积 A@B:\\n{matrix_product_alt}")

    # 矩阵转置
    A_transpose = A.T
    print(f"A的转置:\\n{A_transpose}")

    # 行列式
    det_A = np.linalg.det(A)
    print(f"A的行列式: {det_A:.2f}")

    # 矩阵的逆
    try:
    A_inv = np.linalg.inv(A)
    print(f"A的逆矩阵:\\n{A_inv}")

    # 验证逆矩阵
    identity_check = A @ A_inv
    print(f"A·A^(-1) (应该接近单位矩阵):\\n{identity_check}")
    except np.linalg.LinAlgError:
    print("矩阵不可逆")

    # 特征值和特征向量
    eigenvalues, eigenvectors = np.linalg.eig(A)
    print(f"A的特征值: {eigenvalues}")
    print(f"A的特征向量:\\n{eigenvectors}")

    # 解线性方程组 Ax = b
    b = np.array([1, 2])
    solution = np.linalg.solve(A, b)
    print(f"线性方程组 Ax=b 的解: {solution}")

    # 验证解
    verification = A @ solution
    print(f"A·x (应该等于b): {verification}")

    文件输入输出

    NumPy 提供了简单的方法来保存和加载数组数据:

    import numpy as np
    import os

    # 创建测试数组
    test_array = np.random.rand(5, 5)

    # 保存为二进制文件
    np.save('test_array.npy', test_array)
    print("数组已保存为 test_array.npy")

    # 加载二进制文件
    loaded_array = np.load('test_array.npy')
    print(f"加载的数组形状: {loaded_array.shape}")

    # 保存为文本文件
    np.savetxt('test_array.txt', test_array, delimiter=',', fmt='%.4f')
    print("数组已保存为 test_array.txt")

    # 加载文本文件
    loaded_text_array = np.loadtxt('test_array.txt', delimiter=',')
    print(f"从文本文件加载的数组形状: {loaded_text_array.shape}")

    # 清理临时文件
    os.remove('test_array.npy')
    os.remove('test_array.txt')
    print("临时文件已清理")

    性能优化技巧

    NumPy 的性能优化对于处理大数据集至关重要:

    向量化操作 vs 循环

    import numpy as np
    import time

    # 创建大数组进行性能测试
    size = 1000000
    arr1 = np.random.rand(size)
    arr2 = np.random.rand(size)

    # 使用循环的方式(效率低)
    start_time = time.time()
    result_loop = []
    for i in range(len(arr1)):
    result_loop.append(arr1[i] * arr2[i])
    loop_time = time.time() start_time

    # 使用向量化操作(效率高)
    start_time = time.time()
    result_vectorized = arr1 * arr2
    vectorized_time = time.time() start_time

    print(f"循环方式耗时: {loop_time:.4f} 秒")
    print(f"向量化方式耗时: {vectorized_time:.4f} 秒")
    print(f"向量化比循环快 {loop_time/vectorized_time:.1f} 倍")

    # 使用内置函数而不是手动实现
    # 计算数组元素的平方和
    start_time = time.time()
    manual_sum_squares = sum(x**2 for x in arr1)
    manual_time = time.time() start_time

    start_time = time.time()
    numpy_sum_squares = np.sum(arr1**2)
    numpy_time = time.time() start_time

    print(f"手动计算平方和耗时: {manual_time:.4f} 秒")
    print(f"NumPy计算平方和耗时: {numpy_time:.4f} 秒")
    print(f"NumPy比手动计算快 {manual_time/numpy_time:.1f} 倍")

    内存管理技巧

    import numpy as np

    # 查看数组占用的内存
    arr = np.random.rand(1000, 1000)
    memory_usage = arr.nbytes / (1024 * 1024) # 转换为MB
    print(f"数组占用内存: {memory_usage:.2f} MB")

    # 使用适当的数据类型节省内存
    large_int_array = np.random.randint(0, 100, 1000000, dtype=np.int32)
    large_int_memory = large_int_array.nbytes / (1024 * 1024)
    print(f"32位整型数组内存: {large_int_memory:.2f} MB")

    large_int_array_16 = large_int_array.astype(np.int16)
    large_int_memory_16 = large_int_array_16.nbytes / (1024 * 1024)
    print(f"16位整型数组内存: {large_int_memory_16:.2f} MB")
    print(f"节省内存: {(large_int_memory large_int_memory_16):.2f} MB")

    # 就地操作避免创建新数组
    arr_inplace = np.random.rand(1000000)
    print(f"就地操作前内存使用: {arr_inplace.nbytes / (1024 * 1024):.2f} MB")

    # 就地加法
    arr_inplace += 5 # 不创建新数组
    print("执行就地加法操作")

    # 如果使用 arr_inplace = arr_inplace + 5,则会创建新数组

    实际应用示例

    让我们通过几个实际的应用示例来展示 NumPy 的强大功能:

    图像处理基础

    虽然 NumPy 本身不是图像处理库,但它为图像处理提供了基础数据结构:

    import numpy as np

    # 模拟一个简单的灰度图像 (8×8像素)
    grayscale_image = np.random.randint(0, 256, (8, 8), dtype=np.uint8)
    print("模拟灰度图像:")
    print(grayscale_image)

    # 图像反转 (负片效果)
    inverted_image = 255 grayscale_image
    print("\\n反转后的图像:")
    print(inverted_image)

    # 图像亮度调整
    brightened_image = np.clip(grayscale_image.astype(np.int16) + 50, 0, 255).astype(np.uint8)
    print("\\n亮度增加后的图像:")
    print(brightened_image)

    # 计算图像统计信息
    print(f"\\n图像统计信息:")
    print(f"最小像素值: {np.min(grayscale_image)}")
    print(f"最大像素值: {np.max(grayscale_image)}")
    print(f"平均像素值: {np.mean(grayscale_image):.2f}")
    print(f"像素标准差: {np.std(grayscale_image):.2f}")

    时间序列分析

    NumPy 在时间序列分析中也非常有用:

    import numpy as np

    # 生成模拟的时间序列数据 (比如股价)
    np.random.seed(42)
    days = 100
    initial_price = 100
    returns = np.random.normal(0.001, 0.02, days) # 日收益率
    prices = initial_price * np.cumprod(1 + returns)

    print(f"初始价格: ${initial_price}")
    print(f"最终价格: ${prices[1]:.2f}")
    print(f"总收益率: {((prices[1]/initial_price) 1)*100:.2f}%")

    # 计算移动平均线
    def moving_average(data, window_size):
    return np.convolve(data, np.ones(window_size)/window_size, mode='valid')

    ma_5 = moving_average(prices, 5)
    ma_20 = moving_average(prices, 20)

    print(f"\\n移动平均线:")
    print(f"5日均线最后5个值: {ma_5[5:]}")
    print(f"20日均线最后5个值: {ma_20[5:]}")

    # 计算波动率
    volatility = np.std(returns) * np.sqrt(252) # 年化波动率
    print(f"年化波动率: {volatility*100:.2f}%")

    科学计算应用

    NumPy 在科学计算中有广泛的应用,比如求解物理问题:

    import numpy as np

    # 模拟抛物运动轨迹
    def projectile_motion(v0, angle, g=9.81):
    """
    计算抛物运动轨迹

    参数:
    v0: 初始速度 (m/s)
    angle: 发射角度 (度)
    g: 重力加速度 (m/s²)
    """
    angle_rad = np.radians(angle)
    vx0 = v0 * np.cos(angle_rad)
    vy0 = v0 * np.sin(angle_rad)

    # 计算飞行时间
    flight_time = 2 * vy0 / g

    # 生成时间点
    t = np.linspace(0, flight_time, 100)

    # 计算位置
    x = vx0 * t
    y = vy0 * t 0.5 * g * t**2

    return t, x, y

    # 计算不同角度下的轨迹
    angles = [30, 45, 60]
    v0 = 50 # m/s

    print("抛物运动轨迹计算:")
    for angle in angles:
    t, x, y = projectile_motion(v0, angle)
    max_height = np.max(y)
    max_range = x[1]
    print(f"{angle}°发射角 – 最大高度: {max_height:.2f}m, 最远距离: {max_range:.2f}m")

    NumPy 与其他库的集成

    NumPy 作为科学计算的基础库,与其他库有着良好的集成:

    与 Matplotlib 的集成

    import numpy as np
    import matplotlib.pyplot as plt

    # 生成数据
    x = np.linspace(0, 2*np.pi, 100)
    y_sin = np.sin(x)
    y_cos = np.cos(x)

    # 创建图形
    plt.figure(figsize=(10, 6))
    plt.plot(x, y_sin, label='sin(x)', linewidth=2)
    plt.plot(x, y_cos, label='cos(x)', linewidth=2)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('三角函数图像')
    plt.legend()
    plt.grid(True)
    plt.show()

    # 生成二维数据可视化
    x_2d = np.linspace(5, 5, 100)
    y_2d = np.linspace(5, 5, 100)
    X, Y = np.meshgrid(x_2d, y_2d)
    Z = np.sin(np.sqrt(X**2 + Y**2))

    plt.figure(figsize=(8, 6))
    contour = plt.contour(X, Y, Z, levels=20)
    plt.colorbar(contour)
    plt.title('二维正弦函数等高线图')
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.show()

    与 Pandas 的集成

    import numpy as np
    import pandas as pd

    # 创建包含 NumPy 数组的 DataFrame
    dates = pd.date_range('2023-01-01', periods=100, freq='D')
    values = np.random.randn(100).cumsum()

    df = pd.DataFrame({
    'date': dates,
    'value': values
    })

    print("DataFrame 前5行:")
    print(df.head())

    # 使用 NumPy 函数处理 DataFrame
    df['normalized'] = (df['value'] np.mean(df['value'])) / np.std(df['value'])
    df['moving_avg'] = df['value'].rolling(window=5).mean()

    print("\\n处理后的 DataFrame 前10行:")
    print(df.head(10))

    # 统计分析
    print(f"\\n统计摘要:")
    print(f"均值: {np.mean(df['value']):.2f}")
    print(f"标准差: {np.std(df['value']):.2f}")
    print(f"最小值: {np.min(df['value']):.2f}")
    print(f"最大值: {np.max(df['value']):.2f}")

    高级 NumPy 技巧

    结构化数组

    NumPy 允许创建包含不同类型数据的结构化数组:

    import numpy as np

    # 定义结构化数据类型
    dtype = [('name', 'U10'), ('age', 'i4'), ('weight', 'f4')]

    # 创建结构化数组
    data = np.array([('Alice', 25, 55.5),
    ('Bob', 30, 70.2),
    ('Charlie', 35, 75.8)], dtype=dtype)

    print("结构化数组:")
    print(data)

    # 访问字段
    print(f"\\n姓名: {data['name']}")
    print(f"年龄: {data['age']}")
    print(f"体重: {data['weight']}")

    # 条件筛选
    young_people = data[data['age'] < 30]
    print(f"\\n年轻人: {young_people}")

    掩码数组

    掩码数组允许我们处理包含缺失值的数据:

    import numpy as np
    import numpy.ma as ma

    # 创建普通数组
    data = np.array([1, 2, 3, 999, 5, 6, 999, 8, 9, 10])

    # 创建掩码数组,将-999标记为无效值
    masked_data = ma.masked_equal(data, 999)

    print(f"原始数据: {data}")
    print(f"掩码数组: {masked_data}")
    print(f"掩码: {masked_data.mask}")

    # 对掩码数组进行计算
    print(f"掩码数组的均值: {masked_data.mean():.2f}")
    print(f"掩码数组的最大值: {masked_data.max()}")

    # 另一种创建掩码数组的方法
    data2 = np.array([1, 2, np.nan, 4, 5, np.nan, 7, 8, 9, 10])
    masked_data2 = ma.masked_invalid(data2)
    print(f"\\n包含 NaN 的数据: {data2}")
    print(f"处理后的掩码数组: {masked_data2}")

    错误处理和调试技巧

    在使用 NumPy 时,了解如何处理常见错误和调试问题是很有帮助的:

    常见错误及其解决方法

    import numpy as np

    # 形状不匹配错误
    try:
    arr1 = np.array([1, 2, 3])
    arr2 = np.array([[1, 2], [3, 4]])
    result = arr1 + arr2 # 这会导致广播错误
    except ValueError as e:
    print(f"形状不匹配错误: {e}")

    # 正确的做法 – 调整形状
    arr1_reshaped = arr1.reshape(1, 1)
    print(f"调整形状后的 arr1: {arr1_reshaped}")
    print(f"arr2: {arr2}")
    try:
    result_correct = arr1_reshaped + arr2
    print(f"正确计算的结果:\\n{result_correct}")
    except ValueError as e:
    print(f"仍然有错误: {e}")

    # 数据类型相关错误
    try:
    int_arr = np.array([1, 2, 3], dtype=np.int32)
    float_result = int_arr / 2.0
    print(f"整数除以浮点数的结果: {float_result}")
    print(f"结果的数据类型: {float_result.dtype}")
    except Exception as e:
    print(f"数据类型错误: {e}")

    # 索引越界错误
    arr = np.array([1, 2, 3, 4, 5])
    try:
    value = arr[10] # 索引超出范围
    except IndexError as e:
    print(f"索引越界错误: {e}")
    # 正确的做法
    if 10 < len(arr):
    value = arr[10]
    else:
    print("索引超出数组长度")

    调试技巧

    import numpy as np

    # 使用 np.info() 获取数组信息
    arr = np.random.rand(3, 4, 5)
    print("数组详细信息:")
    np.info(arr)

    # 检查数组是否包含 NaN 或无穷大值
    test_arr = np.array([1, 2, np.nan, 4, np.inf, np.inf])

    print(f"\\n检查数组内容:")
    print(f"包含 NaN: {np.isnan(test_arr).any()}")
    print(f"包含无穷大: {np.isinf(test_arr).any()}")
    print(f"包含有限值: {np.isfinite(test_arr).all()}")

    # 找到 NaN 的位置
    nan_positions = np.where(np.isnan(test_arr))[0]
    print(f"NaN 的位置: {nan_positions}")

    # 替换 NaN 值
    cleaned_arr = np.nan_to_num(test_arr, nan=0.0, posinf=1000, neginf=1000)
    print(f"清理后的数组: {cleaned_arr}")

    性能基准测试

    让我们进行一些性能测试来比较不同方法的效率:

    import numpy as np
    import time

    def performance_comparison():
    """比较不同方法的性能"""
    size = 1000000

    # 测试数据
    python_list = list(range(size))
    numpy_array = np.arange(size)

    # Python 列表求和
    start_time = time.time()
    python_sum = sum(x**2 for x in python_list)
    python_time = time.time() start_time

    # NumPy 数组求和
    start_time = time.time()
    numpy_sum = np.sum(numpy_array**2)
    numpy_time = time.time() start_time

    # NumPy 向量化操作
    start_time = time.time()
    squared_array = numpy_array * numpy_array
    vectorized_sum = np.sum(squared_array)
    vectorized_time = time.time() start_time

    print("性能对比结果:")
    print(f"Python 列表方法: {python_time:.4f} 秒")
    print(f"NumPy 直接求和: {numpy_time:.4f} 秒")
    print(f"NumPy 向量化操作: {vectorized_time:.4f} 秒")
    print(f"NumPy 比 Python 快 {python_time/numpy_time:.1f} 倍")

    performance_comparison()

    # 内存使用对比
    def memory_usage_comparison():
    """比较内存使用情况"""
    import sys

    size = 1000000

    # Python 列表
    python_list = [i for i in range(size)]
    python_memory = sys.getsizeof(python_list)

    # NumPy 数组
    numpy_array = np.arange(size, dtype=np.int64)
    numpy_memory = numpy_array.nbytes

    print(f"\\n内存使用对比:")
    print(f"Python 列表内存: {python_memory / (1024*1024):.2f} MB")
    print(f"NumPy 数组内存: {numpy_memory / (1024*1024):.2f} MB")
    print(f"内存节省比例: {(1 numpy_memory/python_memory)*100:.1f}%")

    memory_usage_comparison()

    NumPy 在机器学习中的应用

    NumPy 是许多机器学习算法的基础,让我们看看它在 ML 中的一些基本应用:

    简单的线性回归实现

    import numpy as np
    import matplotlib.pyplot as plt

    def simple_linear_regression(X, y):
    """
    简单线性回归实现
    y = ax + b
    """

    # 添加偏置项
    X_with_bias = np.column_stack([np.ones(X.shape[0]), X])

    # 使用正规方程求解参数
    # θ = (X^T X)^(-1) X^T y
    theta = np.linalg.inv(X_with_bias.T @ X_with_bias) @ X_with_bias.T @ y

    return theta

    # 生成模拟数据
    np.random.seed(42)
    X = np.random.randn(100, 1)
    y = 2 * X.squeeze() + 1 + np.random.randn(100) * 0.5 # y = 2x + 1 + noise

    # 训练模型
    theta = simple_linear_regression(X, y)
    print(f"线性回归参数: 斜率={theta[1]:.3f}, 截距={theta[0]:.3f}")

    # 预测
    X_test = np.array([[0], [1], [2]])
    X_test_with_bias = np.column_stack([np.ones(X_test.shape[0]), X_test])
    predictions = X_test_with_bias @ theta
    print(f"预测结果: {predictions}")

    # 可视化结果
    plt.figure(figsize=(10, 6))
    plt.scatter(X, y, alpha=0.6, label='训练数据')
    plt.plot(X_test, predictions, 'r-', linewidth=2, label='线性回归')
    plt.xlabel('X')
    plt.ylabel('y')
    plt.title('简单线性回归')
    plt.legend()
    plt.grid(True)
    plt.show()

    K-means 聚类算法基础实现

    import numpy as np
    import matplotlib.pyplot as plt

    def kmeans(X, k, max_iters=100):
    """
    简单的K-means聚类实现
    """

    # 随机初始化聚类中心
    centroids = X[np.random.choice(X.shape[0], k, replace=False)]

    for _ in range(max_iters):
    # 计算每个点到聚类中心的距离
    distances = np.sqrt(((X centroids[:, np.newaxis])**2).sum(axis=2))

    # 分配每个点到最近的聚类中心
    labels = np.argmin(distances, axis=0)

    # 更新聚类中心
    new_centroids = np.array([X[labels == i].mean(axis=0) for i in range(k)])

    # 检查收敛
    if np.allclose(centroids, new_centroids):
    break

    centroids = new_centroids

    return centroids, labels

    # 生成模拟数据
    np.random.seed(42)
    cluster1 = np.random.randn(50, 2) + [2, 2]
    cluster2 = np.random.randn(50, 2) + [2, 2]
    cluster3 = np.random.randn(50, 2) + [2, 2]
    X = np.vstack([cluster1, cluster2, cluster3])

    # 执行K-means聚类
    k = 3
    centroids, labels = kmeans(X, k)

    print(f"找到 {k} 个聚类中心:")
    for i, centroid in enumerate(centroids):
    print(f"聚类中心 {i+1}: ({centroid[0]:.2f}, {centroid[1]:.2f})")

    # 可视化结果
    plt.figure(figsize=(10, 8))
    colors = ['red', 'blue', 'green']
    for i in range(k):
    cluster_points = X[labels == i]
    plt.scatter(cluster_points[:, 0], cluster_points[:, 1],
    c=colors[i], alpha=0.6, label=f'聚类 {i+1}')

    plt.scatter(centroids[:, 0], centroids[:, 1],
    c='black', marker='x', s=200, linewidths=3, label='聚类中心')
    plt.xlabel('特征 1')
    plt.ylabel('特征 2')
    plt.title('K-means 聚类结果')
    plt.legend()
    plt.grid(True)
    plt.show()

    NumPy 最佳实践

    编程规范和建议

    import numpy as np

    # 1. 始终导入 NumPy 为 np
    # import numpy as np # 已经在顶部导入

    # 2. 明确指定数据类型
    # 好的做法
    explicit_array = np.array([1, 2, 3], dtype=np.float64)
    # 避免隐式的类型推断

    # 3. 使用向量化操作而非循环
    # 好的做法
    def vectorized_operation(arr):
    return arr ** 2 + 2 * arr + 1

    # 避免的做法
    def loop_operation(arr):
    result = []
    for x in arr:
    result.append(x**2 + 2*x + 1)
    return np.array(result)

    # 4. 预分配数组大小
    def efficient_array_creation(size):
    # 好的做法 – 预分配
    result = np.empty(size)
    for i in range(size):
    result[i] = i ** 2
    return result

    # 5. 使用适当的函数
    # 对于大的数组,使用 np.concatenate 而不是 np.append
    large_arrays = [np.random.rand(1000) for _ in range(10)]
    # 好的做法
    efficient_concat = np.concatenate(large_arrays)
    # 避免的做法
    # inefficient_append = np.array([])
    # for arr in large_arrays:
    # inefficient_append = np.append(inefficient_append, arr)

    print("最佳实践示例完成")

    内存管理和优化

    import numpy as np

    # 1. 使用适当的数据类型节省内存
    def memory_efficient_arrays():
    # 大整数数组使用较小的数据类型
    large_numbers = np.arange(1000000, dtype=np.int32) # 而不是默认的 int64
    print(f"32位整数数组内存: {large_numbers.nbytes / (1024*1024):.2f} MB")

    # 布尔数组
    boolean_array = np.random.choice([True, False], 1000000)
    print(f"布尔数组内存: {boolean_array.nbytes / (1024*1024):.2f} MB")

    # 使用 uint8 存储小的非负整数
    small_positive = np.random.randint(0, 256, 1000000, dtype=np.uint8)
    print(f"uint8数组内存: {small_positive.nbytes / (1024*1024):.2f} MB")

    memory_efficient_arrays()

    # 2. 就地操作避免创建新数组
    def inplace_operations():
    arr = np.random.rand(1000000)

    # 就地操作 – 节省内存
    arr += 5 # 而不是 arr = arr + 5
    arr *= 2 # 而不是 arr = arr * 2

    print("就地操作完成")

    inplace_operations()

    # 3. 删除不需要的大数组
    def memory_cleanup():
    large_temp_array = np.random.rand(10000000)
    # 进行一些计算…
    result = np.sum(large_temp_array)

    # 删除大数组释放内存
    del large_temp_array

    return result

    result = memory_cleanup()
    print(f"计算结果: {result:.2f}")

    常见问题解答

    Q1: NumPy 数组和 Python 列表有什么区别?

    NumPy 数组和 Python 列表的主要区别包括:

  • 性能: NumPy 数组在数值计算方面比 Python 列表快得多
  • 内存效率: NumPy 数组更节省内存
  • 数据类型: NumPy 数组要求所有元素具有相同的数据类型
  • 功能: NumPy 提供了丰富的数学函数和操作
  • import numpy as np
    import time

    # 性能比较示例
    size = 1000000

    # Python 列表
    python_list = list(range(size))
    start_time = time.time()
    python_result = [x * 2 for x in python_list]
    python_time = time.time() start_time

    # NumPy 数组
    numpy_array = np.arange(size)
    start_time = time.time()
    numpy_result = numpy_array * 2
    numpy_time = time.time() start_time

    print(f"Python 列表操作时间: {python_time:.4f} 秒")
    print(f"NumPy 数组操作时间: {numpy_time:.4f} 秒")
    print(f"NumPy 快 {python_time/numpy_time:.1f} 倍")

    Q2: 如何处理 NumPy 中的缺失值?

    NumPy 提供了几种处理缺失值的方法:

    import numpy as np

    # 使用 NaN 表示缺失值
    data_with_nan = np.array([1, 2, np.nan, 4, 5, np.nan, 7])

    # 检查缺失值
    print(f"包含 NaN: {np.isnan(data_with_nan)}")
    print(f"非缺失值: {~np.isnan(data_with_nan)}")

    # 移除缺失值
    cleaned_data = data_with_nan[~np.isnan(data_with_nan)]
    print(f"移除缺失值后: {cleaned_data}")

    # 替换缺失值
    filled_data = np.where(np.isnan(data_with_nan), 0, data_with_nan)
    print(f"替换缺失值后: {filled_data}")

    # 使用掩码数组
    import numpy.ma as ma
    masked_data = ma.masked_invalid(data_with_nan)
    print(f"掩码数组: {masked_data}")
    print(f"掩码数组的均值: {masked_data.mean()}")

    Q3: 如何优化 NumPy 代码的性能?

    以下是一些优化 NumPy 代码性能的技巧:

    import numpy as np
    import time

    # 1. 使用向量化操作
    def optimization_example():
    size = 1000000
    arr = np.random.rand(size)

    # 低效方法 – 使用循环
    def slow_method(array):
    result = np.empty_like(array)
    for i in range(len(array)):
    result[i] = array[i] ** 2 + 2 * array[i] + 1
    return result

    # 高效方法 – 向量化操作
    def fast_method(array):
    return array ** 2 + 2 * array + 1

    # 性能比较
    start_time = time.time()
    slow_result = slow_method(arr)
    slow_time = time.time() start_time

    start_time = time.time()
    fast_result = fast_method(arr)
    fast_time = time.time() start_time

    print(f"循环方法耗时: {slow_time:.4f} 秒")
    print(f"向量化方法耗时: {fast_time:.4f} 秒")
    print(f"性能提升: {slow_time/fast_time:.1f} 倍")

    optimization_example()

    # 2. 使用适当的数据类型
    def data_type_optimization():
    # 大整数数组
    large_ints = np.arange(1000000, dtype=np.int64)
    print(f"int64 数组内存: {large_ints.nbytes / (1024*1024):.2f} MB")

    # 如果数值范围允许,使用更小的数据类型
    small_ints = np.arange(1000000, dtype=np.int32)
    print(f"int32 数组内存: {small_ints.nbytes / (1024*1024):.2f} MB")
    print(f"内存节省: {(large_ints.nbytes small_ints.nbytes) / (1024*1024):.2f} MB")

    data_type_optimization()

    学习资源推荐

    想要深入学习 NumPy,以下是一些优质的学习资源:

  • 官方文档: NumPy Documentation – 最权威的学习资料
  • 教程网站: Real Python NumPy Tutorial – 详细的入门教程
  • 在线课程: Coursera Python for Data Science – 包含 NumPy 的完整数据科学课程
  • # 创建一个学习计划示例
    learning_plan = {
    "Week 1": ["NumPy 基础", "数组创建和操作"],
    "Week 2": ["索引和切片", "数组变形"],
    "Week 3": ["数学函数", "广播机制"],
    "Week 4": ["线性代数", "随机数生成"],
    "Week 5": ["性能优化", "实际项目练习"]
    }

    print("NumPy 学习计划:")
    for week, topics in learning_plan.items():
    print(f"{week}: {', '.join(topics)}")

    总结

    NumPy 作为 Python 科学计算的核心库,在数据科学、机器学习和科学计算领域发挥着至关重要的作用。通过本文的学习,我们掌握了:

    • 🔢 NumPy 数组的基本概念和创建方法
    • 📊 数组的索引、切片和操作技巧
    • ⚡ 广播机制和性能优化策略
    • 🧮 线性代数运算和统计函数
    • 🔄 数组合并与分割技术
    • 🎲 随机数生成和文件 I/O 操作
    • 🛠️ 实际应用场景和最佳实践

    NumPy 的强大之处在于它的简洁性和高效性。通过向量化操作,我们可以用很少的代码完成复杂的数值计算任务。同时,它为更高层次的科学计算库提供了坚实的基础。

    随着你在数据科学领域的深入发展,NumPy 将成为你不可或缺的工具。建议通过大量的练习来巩固所学知识,并在实际项目中应用这些技能。

    记住,掌握 NumPy 不是一蹴而就的过程,需要持续的练习和应用。希望本文能够为你开启 NumPy 学习之旅提供有力的帮助!🌟

    Happy coding with NumPy! 💻✨


    🙌 感谢你读到这里! 🔍 技术之路没有捷径,但每一次阅读、思考和实践,都在悄悄拉近你与目标的距离。 💡 如果本文对你有帮助,不妨 👍 点赞、📌 收藏、📤 分享 给更多需要的朋友! 💬 欢迎在评论区留下你的想法、疑问或建议,我会一一回复,我们一起交流、共同成长 🌿 🔔 关注我,不错过下一篇干货!我们下期再见!✨

    赞(0)
    未经允许不得转载:171主机测评 » Python NumPy - 初识 NumPy 科学计算的核心库
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址