NumPy是一个常用于科学计算的Python库,尤其在深度学习和机器学习中应用广泛。
NumPy:初学者的绝对基础知识 — NumPy v1.25 手册https://numpy.org/doc/stable/user/absolute_beginners.html
导入 numpy 库(应该遵循标准的 NumPy 约定)。
import numpy as np
打印 NumPy 的版本号。
print(np.__version__)
arange
function to generate 10 elements from 0 to 9 and store them in a variable named ndarray
.使用 arange 函数生成 10 个从 0 到 9 的元素,并将它们存储在名为 ndarray 的变量中。
ndarray = np.arange(10)
print(ndarray)
4. array
函数array
function to convert data in Python list format into an equivalent ndarray
named ndarray1
. 利用array
函数将 Python 列表格式的数据转换为名为 ndarray1 的等效 ndarray。
ndarray1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(ndarray1)
ndarray
and the ndarray1
into a 2-row by 5-column array.将 ndarray 和 ndarray1 改造成 2 行 x 5 列数组。
ndarray = ndarray.reshape(2, 5)
ndarray1 = ndarray1.reshape(2, 5)
print(ndarray)
print(ndarray1)
ndarray
and ndarray1
using the *
operator, and print the result使用 * 运算符计算 ndarray 和 ndarray1 的元素乘积,并打印结果
result = ndarray * ndarray1
print(result)
ndarray
and ndarray1
using the @
operator, and print the result. You need to use the T
attribute to perform a transpose operation on ndarray1
.使用 @ 运算符计算 ndarray 和 ndarray1 的矩阵乘积,并打印结果。需要使用 T 属性在 ndarray1 上执行转置操作。
result1 = ndarray @ ndarray1.T
print(result1)