0
点赞
收藏
分享

微信扫一扫

【AI大语言模型应用】使用Ollama搭建本地大语言模型

爱上流星雨 2024-09-21 阅读 19

1、np.ndenumerate
作用:用于在多维数组上进行迭代。这个函数返回一个迭代器,它生成一个包含数组索引和对应元素值的元组。
用法:

import numpy as np

arr = np.array([[1, 2], [3, 4]])
for index, value in np.ndenumerate(arr):
    print(index, value)

结果:

(0, 0) 1
(0, 1) 2
(1, 0) 3
(1, 1) 4

2、np.newaxis
作用:用于增加数组的维度。在数组中使用 np.newaxis 可以在指定位置增加一个新的轴,这通常用于改变数组的形状而不需要复制数据。
用法:
将一维数组转换为二维数组的行向量:

import numpy as np

arr = np.array([1, 2, 3])
arr_2d = arr[np.newaxis, :]
print(arr_2d)
# 输出:
# [[1 2 3]]

3、np.repeat
可以沿着指定的轴重复数组中的元素多次,生成一个新的数组。
语法:numpy.repeat(a, repeats, axis=None)

import numpy as np

# 创建一个简单的数组
a = np.array([1, 2, 3])

# 重复数组中的每个元素两次
repeated_array = np.repeat(a, 2)
print(repeated_array)  # 输出: [1 1 2 2 3 3]

# 创建一个二维数组
a_2d = np.array([[1, 2], [3, 4]])

# 沿着水平轴(axis=1)重复每个元素两次
repeated_2d_array = np.repeat(a_2d, 2, axis=1)
print(repeated_2d_array)
# 输出:
# [[1 1 2 2]
#  [3 3 4 4]]

# 沿着垂直轴(axis=0)重复每行两次
repeated_2d_array_axis0 = np.repeat(a_2d, 2, axis=0)
print(repeated_2d_array_axis0)
# 输出:
# [[1 2]
#  [1 2]
#  [3 4]
#  [3 4]]
举报

相关推荐

0 条评论