AI如何帮你快速实现Vue-TreeSelect组件开发
2026/1/22 10:20:12
# 创建一个包含6个元素的一维数组 import numpy as np data = np.array([1, 2, 3, 4, 5, 6]) # 将其重塑为 2 行 3 列的二维数组 reshaped_data = data.reshape(2, 3) print(reshaped_data) # 输出: # [[1 2 3] # [4 5 6]]| 原始形状 | 变换操作 | 新形状 |
|---|---|---|
| (6,) | reshape(2, 3) | (2, 3) |
| (2, 3) | transpose() | (3, 2) |
| (1, 4, 1) | squeeze() | (4,) |
reshape是 NumPy 中用于改变数组形状的核心方法,其基本语法为:
arr.reshape(shape)其中shape可以是元组或一系列整数,表示目标维度。reshape 操作不会复制数据,而是返回原数据的视图(view)。
-1可自动推导该轴的长度,如arr.reshape(2, -1)表示第二维由系统计算-1,否则无法确定形状import numpy as np arr = np.arange(12) reshaped = arr.reshape(3, 4) # 转为 3 行 4 列 print(reshaped.shape) # 输出: (3, 4)上述代码将一维数组转为二维矩阵,体现了维度变换的直观性与高效性。reshape 在深度学习中常用于张量预处理。
import numpy as np arr = np.arange(6) # [0, 1, 2, 3, 4, 5] reshaped = arr.reshape(2, 3)该操作将原数组按行优先(C风格)填充为两行三列矩阵。参数`2, 3`指定新形状,必须满足乘积等于原元素数(2×3=6)。arr.reshape(3, -1) # 结果为 (3, 2)此技巧常用于动态数据流中,只需固定一个维度,另一个由系统推导。import numpy as np data = np.arange(12).reshape(3, 4) reshaped = data.reshape(2, 6)上述代码将 3×4 的二维数组转换为 2×6 形式。由于原始数组包含 12 个元素,目标形状 2×6 同样满足总元素数一致,因此合法。reshape 按照内存连续顺序逐行填充新结构,保持数据一致性。timeout := time.Duration(-1) if timeout < 0 { // 永久等待模式 waitForever() } else { time.Sleep(timeout) }上述代码中,`-1` 被显式转换为负的 `time.Duration`,通过条件判断进入无超时逻辑分支,常用于内部服务间强依赖调用。import numpy as np flat_image = np.random.rand(1, 784) image_2d = flat_image.reshape(28, 28)该操作将一维向量重塑为二维灰度图,便于后续卷积层处理。参数-1可自动推断维度,如reshape(-1, 28, 28)可批量处理多张图像。data = np.arange(100) windowed = data[:96].reshape(-1, 4) # 每4个点为一个序列样本此方法高效生成时间步结构,适配循环神经网络输入要求,提升训练效率。transpose推广至高维张量。默认反转轴顺序,也可显式指定新顺序:import numpy as np arr = np.arange(6).reshape(2, 3) transposed = arr.transpose() # 等价于 .T print(transposed.shape) # 输出: (3, 2)该代码将形状为 (2, 3) 的数组转置为 (3, 2),元素位置由 (i,j) 映射至 (j,i)。对于三维张量,transpose(2,0,1)表示将原第2轴变为第0轴,第0轴变第1轴,第1轴变第2轴,实现灵活的数据重排。import numpy as np A = np.array([[1, 2], [3, 4], [5, 6]]) A_transposed = A.T print(A_transposed) # 输出: # [[1 3 5] # [2 4 6]]该代码将 3×2 矩阵转为 2×3 矩阵,.T 属性实现行列互换,适用于所有二维数组。np.transpose显式指定轴顺序。例如,将形状为 (2, 3, 4) 的张量调整为 (4, 2, 3):B = np.random.rand(2, 3, 4) B_permuted = np.transpose(B, (2, 0, 1)) print(B_permuted.shape) # (4, 2, 3)参数(2, 0, 1)表示新结构的第一轴来自原数组的第2轴(索引从0开始),依此类推,实现灵活的数据重排。import numpy as np # 假设原始图像是 HWC 格式 (224, 224, 3) img_hwc = np.random.rand(224, 224, 3) # 转置为 CHW 格式 img_chw = np.transpose(img_hwc, (2, 0, 1)) print(img_chw.shape) # 输出: (3, 224, 224)该操作将通道维前置,符合卷积神经网络输入规范。参数 `(2, 0, 1)` 指定原维度索引的新位置:第2维变为第0维,第0维变第1维,第1维变第2维。expand_dims在张量操作中用于在指定位置插入长度为 1 的新维度,常用于广播对齐、模型输入适配或维度语义增强。| 方法 | 等价性 |
|---|---|
np.expand_dims(x, axis=0) | x[np.newaxis, ...] |
tf.expand_dims(x, axis=-1) | x[..., tf.newaxis] |
def manual_expand_dims(tensor, axis): # axis 支持负索引,自动归一化 ndim = len(tensor.shape) axis = axis if axis >= 0 else ndim + axis + 1 return tensor.reshape(tensor.shape[:axis] + (1,) + tensor.shape[axis:])该函数通过reshape拆分原形状并在axis处插入单维元组(1,),完全复现expand_dims的语义与边界行为。import numpy as np x = np.array([1, 2, 3]) y = x[:, np.newaxis] # 形状由 (3,) 变为 (3, 1)该方法利用`np.newaxis`在指定位置插入长度为1的新轴,本质是广播机制的一部分。z = x.reshape(3, 1) # 同样变为 (3, 1)`reshape`需明确指定新形状,灵活性更高,可同时调整多个维度。| 方法 | 可读性 | 灵活性 | 适用场景 |
|---|---|---|---|
| np.newaxis | 高 | 中 | 快速增维 |
| reshape | 中 | 高 | 复杂形状变换 |
import torch x = torch.randn(1, 3, 1, 5) y = x.squeeze() print(y.shape) # torch.Size([3, 5])此处所有 size=1 的维度被自动移除。也可指定维度:`x.squeeze(0)` 仅压缩第0维。z = y.unsqueeze(0).unsqueeze(2) print(z.shape) # torch.Size([1, 3, 1, 5])该操作完美还原原始形状,实现与 `squeeze` 的配对使用。import torch from PIL import Image import torchvision.transforms as T # 定义预处理变换 transform = T.Compose([ T.Resize((224, 224)), T.ToTensor(), # 归一化并转为 CHW 格式 T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) image = Image.open("example.jpg") input_tensor = transform(image).unsqueeze(0) # 增加 batch 维度 print(input_tensor.shape) # 输出: torch.Size([1, 3, 224, 224])上述代码将单张图像转换为符合 ResNet 等主流模型输入要求的标准张量。其中unsqueeze(0)用于添加批次维度,确保输入形状与模型期望一致。// Flink 流处理示例:实时统计每分钟请求数 env.addSource(new FlinkKafkaConsumer<>("requests", schema, props)) .keyBy(event -> event.getService()) .window(TumblingProcessingTimeWindows.of(Time.minutes(1))) .aggregate(new RequestCounter()) .addSink(new InfluxDBSink());| 调度器 | 适用场景 | 资源隔离 | 启动延迟 |
|---|---|---|---|
| YARN | 企业私有集群 | 中等 | 较高 |
| Kubernetes | 云原生环境 | 强 | 低 |
| Standalone | 测试环境 | 弱 | 低 |