← 返回题库
初级

标量、向量、矩阵、张量

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    import numpy as np
    scalar = np.array(6)
    print(f"标量: {scalar}, 形状: {scalar.shape}, 维度: {scalar.ndim}")
    array_1d = np.array([1, 2, 3, 4, 5, 6])
    print(f"一维数组: {array_1d}, 形状: {array_1d.shape}, 维度: {array_1d.ndim}")
    vector = np.arange(6).reshape(1, 6)
    print(f"向量: {vector}, 形状: {vector.shape}, 维度: {vector.ndim}")
    matrix = np.arange(12).reshape(2, 6)
    print(f"矩阵: {matrix}, 形状: {matrix.shape}, 维度: {matrix.ndim}")
    tensor = np.arange(12).reshape(2, 3, 2)
    print(f"张量: {tensor}, 形状: {tensor.shape}, 维度: {tensor.ndim}")

示例

输入
solve()
期望输出
标量: 6, 形状: (), 维度: 0
一维数组: [1 2 3 4 5 6], 形状: (6,), 维度: 1
向量: [[0 1 2 3 4 5]], 形状: (1, 6), 维度: 2
矩阵: [[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]], 形状: (2, 6), 维度: 2
张量: [[[ 0  1]
  [ 2  3]
  [ 4  5]]

 [[ 6  7]
  [ 8  9]
  [10 11]]], 形状: (2, 3, 2), 维度: 3
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码