np.squeeze

np.squeeze:从数组的形状中删除维度为 1 的维度。

np.squeeze(arr, axis)

参数:

  • arr:输入数组
  • axis:整数或整数元组,用于选择形状中一维维度的子集。

示例:

import numpy as np

x = np.arange(9).reshape(1, 3, 3)
print(x)

y = np.squeeze(x)
print(y)

print(x.shape, y.shape)
[[[0 1 2]
  [3 4 5]
  [6 7 8]]]
[[0 1 2]
 [3 4 5]
 [6 7 8]]
(1, 3, 3) (3, 3)

在用 matplotlib 直接画图,会报错,可以利用squeeze()函数将表示向量的数组转换为秩为1的数组。

plt.plot(x)
plt.show()
ValueError: x and y can be no greater than 2-D, but have shapes (1,) and (1, 3, 3)
plt.plot(y)
plt.show()



posted @   做梦当财神  阅读(718)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示