当前位置: 首页 > news >正文

numpy np.newaxis介绍

np.newaxis 是 NumPy 中用于增加数组维度的关键字。它的作用是为数组插入一个新的维度,从而改变数组的形状(shape)。


基本用法

  • np.newaxis 等价于 None,可以作为索引使用,用于在指定位置增加一个维度。
  • 增加的维度的大小为 1

语法

array[newaxis, ...]  # 在第 0 维增加一个维度
array[..., newaxis]  # 在最后一维增加一个维度
array[:, newaxis, :]  # 在指定位置增加一个维度

例子与解释

1. 在第 0 维增加一个维度
import numpy as nparr = np.array([1, 2, 3])  # 原始数组 shape: (3,)
new_arr = arr[np.newaxis, :]  # shape: (1, 3)print(new_arr)
# Output:
# [[1 2 3]]

解释

  • 原始数组 arr 是一维的,形状为 (3,)
  • 使用 np.newaxis 后,在第 0 维增加一个新维度,形状变为 (1, 3)

2. 在最后一维增加一个维度
arr = np.array([1, 2, 3])  # shape: (3,)
new_arr = arr[:, np.newaxis]  # shape: (3, 1)print(new_arr)
# Output:
# [[1]
#  [2]
#  [3]]

解释

  • 原始数组 arr 是一维的,形状为 (3,)
  • 使用 np.newaxis 后,在最后一维增加一个新维度,形状变为 (3, 1)

3. 用于多维数组
arr = np.array([[1, 2, 3], [4, 5, 6]])  # shape: (2, 3)# 在第 0 维增加
new_arr_1 = arr[np.newaxis, :, :]  # shape: (1, 2, 3)# 在第 1 维增加
new_arr_2 = arr[:, np.newaxis, :]  # shape: (2, 1, 3)# 在最后一维增加
new_arr_3 = arr[:, :, np.newaxis]  # shape: (2, 3, 1)print("Original Shape:", arr.shape)
print("Shape after newaxis at dim 0:", new_arr_1.shape)
print("Shape after newaxis at dim 1:", new_arr_2.shape)
print("Shape after newaxis at dim 2:", new_arr_3.shape)

实际应用

1. 转换向量为列向量或行向量

在机器学习或矩阵运算中,常需要将向量变为列向量或行向量。

arr = np.array([1, 2, 3])  # shape: (3,)# 转为列向量
col_vector = arr[:, np.newaxis]  # shape: (3, 1)# 转为行向量
row_vector = arr[np.newaxis, :]  # shape: (1, 3)print("Column Vector:\n", col_vector)
print("Row Vector:\n", row_vector)

2. 扩展广播机制

使用 np.newaxis 可以调整数组形状以实现广播操作。

arr1 = np.array([1, 2, 3])  # shape: (3,)
arr2 = np.array([4, 5])     # shape: (2,)# 调整维度
arr1_expanded = arr1[np.newaxis, :]  # shape: (1, 3)
arr2_expanded = arr2[:, np.newaxis]  # shape: (2, 1)result = arr1_expanded + arr2_expanded  # shape: (2, 3)print(result)
# Output:
# [[5 6 7]
#  [6 7 8]]

等价性

np.newaxis 等价于 None,下面两种写法是相同的:

arr = np.array([1, 2, 3])# 使用 np.newaxis
new_arr_1 = arr[np.newaxis, :]# 使用 None
new_arr_2 = arr[None, :]print(np.array_equal(new_arr_1, new_arr_2))  # Output: True

总结

  1. np.newaxis 是一种增加数组维度的简单方法,实质是为数组插入大小为 1 的新维度。
  2. 常用于:
    • 调整数组形状(如向量转列/行向量)。
    • 配合广播机制使用。
    • 为高维数据的操作做准备。
  3. 使用方式简单直观,可以通过指定插入位置灵活控制新维度的位置。
http://www.lryc.cn/news/511793.html

相关文章:

  • 小程序配置文件 —— 16 项目配置文件和配置 sass
  • 【yolov5】实现FPS游戏人物检测,并定位到矩形框上中部分,实现自瞄
  • 概率统计与随机过程--作业5
  • “802.11g”,“802.11n”,“802.11ac”,“802.11ax”
  • Kubernetes 常用的网络插件
  • Retrofit和rxjava 实现窜行请求,并行请求,循环多次请求,递归请求,错误重试
  • 2025年度好用便签推荐,电脑桌面便签app分享
  • 【论文解读】Arbitrary-steps Image Super-resolution via Diffusion Inversion
  • kkFileView集成springboot:使用自定义预览接口(非minio预览接口),发现无法预览资源
  • 被裁20240927 --- 嵌入式硬件开发 STM32篇
  • 留学生交流互动系统|Java|SSM|VUE| 前后端分离
  • C/C++ 数据结构与算法【图】 图+邻接矩阵+邻接表+DFS+BFS+最小生成树+最短路径+拓扑排序详细解析【日常学习,考研必备】带图+详细代码
  • Linux实验报告7-文件管理
  • RJ45网口模块设计
  • 电子电器架构 --- 智能座舱HUD技术革新
  • 嵌入式开发中的机器人表情绘制
  • orm01
  • Maven 测试和单元测试介绍
  • Postman接口测试03|执行接口测试、全局变量和环境变量、接口关联、动态参数、断言
  • UE5 丧尸类杂兵的简单AI
  • Linux字符设备驱动开发的三种方式(分析+对比+示例)
  • C++设计模式之行为型模式概述,它们的目的与特点
  • 把Huggingface下载的arrow数据集转化为json格式
  • 复习打卡大数据篇——Hadoop YARN
  • fpga系列 HDL:ModelSim显示模拟波形以及十进制格式数值
  • Linux 基本指令
  • GO语言基础面试题
  • 要查询 `user` 表中 `we_chat_subscribe` 和 `we_chat_union_id` 列不为空的用户数量
  • 小程序基础 —— 10 如何调试小程序代码
  • Vue项目如何设置多个静态文件;如何自定义静态文件目录