关于np.newaxis的一点理解

本文详细介绍了如何使用np.newaxis来改变NumPy数组的维度。通过具体示例对比了使用shape属性与np.newaxis的不同方法,帮助读者更好地理解np.newaxis在实际应用中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于np.newaxis的用法,出现了很多介绍,这一篇相对来说是比较容易理解的。

文章转载自:https://blog.youkuaiyun.com/molu_chase/article/details/78619731

经常在sklearn上看到np.newaxis,这里记录一下我的理解

np.arange(0, 10)

这句话 生成的是一个一维的数组,如下:

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

输出其shape= (10,)

那么我如何才能将其转化为shape=(1,10)呢

可以用两种方法:

1.使用shape

  1. y=np.arange(1, 11)
  2. y.shape=(10,1)
  3. print(y)

结果如下:

  1. [[ 1]
  2. [ 2]
  3. [ 3]
  4. [ 4]
  5. [ 5]
  6. [ 6]
  7. [ 7]
  8. [ 8]
  9. [ 9]
  10. [10]]

2. 使用np.newaxis
print(np.arange(0, 10)[:, np.newaxis])
结果如下:
  1. [[0]
  2. [1]
  3. [2]
  4. [3]
  5. [4]
  6. [5]
  7. [6]
  8. [7]
  9. [8]
  10. [9]]

如上所示,应该清楚了np.newaxis了吧

上面的代码实质就是将原本的(10,)移到行上,然后新增一列

import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from mpl_toolkits.mplot3d import Axes3D from matplotlib.colors import Normalize from scipy.interpolate import griddata # --------------------- 1. 原始数据与公共参数 --------------------- data = { "30min": { "temperatures": [ [0.00, 74.80, 80.08, 80.96, 78.32, 76.56, 0.00], [0.00, 64.33, 68.87, 69.63, 67.36, 65.84, 0.00], [0.00, 48.62, 52.05, 52.62, 50.91, 49.76, 0.00], [0.00, 28.42, 30.43, 30.76, 29.76, 29.09, 0.00], [0.00, 8.98, 9.61, 9.72, 9.40, 9.19, 0.00], [0.00, 1.50, 1.60, 1.62, 1.57, 1.53, 0.00] ] } } # 公共参数 depths = [0, 17, 34, 51, 68, 85] # 原始深度 (mm) horizontal_positions = [0, 50, 150, 250, 350, 450, 500] # 水平位置 (mm) heating_times = [10, 20, 30] # 加热时间 (min) profile_idx = horizontal_positions.index(250) # 250mm对应的列索引 mirror_depths = np.concatenate([-np.array(depths[1:])[::-1], depths]) # 镜像深度(对称于0mm) # --------------------- 2. 绘制2D镜像温度分布图 --------------------- def plot_2d_mirror(): fig_2d = plt.figure(figsize=(18, 12)) norm = Normalize(vmin=0, vmax=100) # 统一色标0-100℃ cmap = 'jet' for i, (time_key, time_data) in enumerate(data.items()): # 创建镜像温度矩阵 original_temps = np.array(time_data["temperatures"]) mirror_temps = np.vstack([original_temps[1:][::-1], original_temps]) # 创建网格 X, Y = np.meshgrid(horizontal_positions, mirror_depths) # 绘制二维温度分布图 ax = fig_2d.add_subplot(2, 3, i+1) contour = ax.contourf(X, Y, mirror_temps, levels=20, cmap=cmap, norm=norm) # 设置标题和坐标轴 ax.set_title(f'Heating Time: {time_key}', fontsize=14) ax.set_xlabel('Horizontal Position (mm)', fontsize=12) ax.set_ylabel('Depth (mm)', fontsize=12) ax.set_xlim(0, 500) ax.set_ylim(-85, 85) ax.grid(True, linestyle='--', alpha=0.5) # 添加色标 cbar = plt.colorbar(contour, ax=ax) cbar.set_label('Temperature (°C)', fontsize=12) # 添加250mm处的垂直线 ax.axvline(x=250, color='red', linestyle='--', linewidth=1.5, alpha=0.7) ax.text(255, 70, '250mm', color='red', fontsize=10, bbox=dict(facecolor='white', alpha=0.7)) # 添加对称轴标记 ax.axhline(y=0, color='black', linestyle='-', linewidth=1.0) ax.text(10, 5, 'Symmetry Plane (Depth=0)', color='black', fontsize=10, bbox=dict(facecolor='white', alpha=0.7)) fig_2d.suptitle('2D Temperature Distribution with Mirror Effect', fontsize=16, y=0.95) plt.tight_layout(rect=[0, 0, 1, 0.95]) return fig_2d fig_2d = plot_2d_mirror() plt.show() 将这个代码所绘制出来的二维平面数据沿Y=0这个轴旋转圈,就得到了维数据,再将>0的区域显示出来。就得到了被加热区域的范围示意图。图形的照片类似一个球形,在我给的代码上扩充完成
最新发布
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值