使用 NumPy 将数字附加到向量
Append numbers to vectors with NumPy
我想用 NumPy 在特定位置存储多个力。
我需要一个位置为 positions
的矢量和一个在特定位置具有力 forces
的矢量。我提示用户使用 input("Please input position: ")
和 input("Please input force: ")
.
输入这些位置和力
如何使用 NumPy 将这些输入值附加到两个单独的向量?我试过了
import numpy as np
# empty as default
positions = None
forces = None
# append
np.append(positions, int(input("Please input position: ")))
np.append(forces, int(input("Please input position: ")))
# print positions and forces
print(positions)
print(forces)
但变量 positions
和 forces
保持为空。
就像这样:
positions = np.array(())
positions=np.append(positions, int(input("Please input position: ")))
你这里有三个问题。
首先,None
不是数组,因此附加到它没有任何意义。 (NumPy 实际上会让你这样做,但它通过将其视为 array([None], dtype=object)
来实现,这几乎肯定不是你想要的。)
其次,append
不会就地修改其数组参数,它 returns 一个新数组。* 正如文档所说:
A copy of arr with values appended to axis. Note that append
does not occur in-place: a new array is allocated and filled.
此外,您确实应该在创建数组时设置数组的数据类型;否则,您将获得默认值 float64
,而 NumPy 将浪费地将所有漂亮的整数转换为浮点数。
所以,做你想做的事:
positions = np.array([], dtype=int)
forces = np.array([], dtype=int)
# append
positions = np.append(positions, int(input("Please input position: ")))
forces = np.append(forces, int(input("Please input position: ")))
# print positions and forces
print(positions)
print(forces)
第三,NumPy 数组并不意味着一次增长一个元素。这个问题不会导致你的错误——对于这么小的数组(大小 (1,)
),它甚至不会产生明显的影响——但这是一个非常糟糕的习惯。许多其他类型,如 Python 的内置列表,都适用于此,但不是 NumPy 数组。
通常要么您事先知道您将拥有多少个值,在这种情况下您可以预先分配数组。如果没有,您通常会从动态构建的某种可迭代对象构建它,例如 list
.** 以下是一些示例:
# Pre-allocating the array
positions = np.zeros((1,), dtype=int)
positions[0] = int(input("Please input position: "))
# Building the array from a dynamically-built list
positions = []
positions.append(int(input("Please input position: ")))
positions = np.array(positions, dtype=int)
# Building the array all at once from a static list
positions = np.array([int(input("Please input position: "))], dtype=int)
* 如果它 确实 尝试就地修改,第一个问题会更糟,因为那时你会尝试修改不可变单例值 None
…
** 对于非常大的数组,不要使用 list
,因为构建临时 list
的成本太高。相反,您通常希望创建生成器或其他类型的惰性迭代器,并使用 np.fromiter
.
我想用 NumPy 在特定位置存储多个力。
我需要一个位置为 positions
的矢量和一个在特定位置具有力 forces
的矢量。我提示用户使用 input("Please input position: ")
和 input("Please input force: ")
.
如何使用 NumPy 将这些输入值附加到两个单独的向量?我试过了
import numpy as np
# empty as default
positions = None
forces = None
# append
np.append(positions, int(input("Please input position: ")))
np.append(forces, int(input("Please input position: ")))
# print positions and forces
print(positions)
print(forces)
但变量 positions
和 forces
保持为空。
就像这样:
positions = np.array(())
positions=np.append(positions, int(input("Please input position: ")))
你这里有三个问题。
首先,None
不是数组,因此附加到它没有任何意义。 (NumPy 实际上会让你这样做,但它通过将其视为 array([None], dtype=object)
来实现,这几乎肯定不是你想要的。)
其次,append
不会就地修改其数组参数,它 returns 一个新数组。* 正如文档所说:
A copy of arr with values appended to axis. Note that
append
does not occur in-place: a new array is allocated and filled.
此外,您确实应该在创建数组时设置数组的数据类型;否则,您将获得默认值 float64
,而 NumPy 将浪费地将所有漂亮的整数转换为浮点数。
所以,做你想做的事:
positions = np.array([], dtype=int)
forces = np.array([], dtype=int)
# append
positions = np.append(positions, int(input("Please input position: ")))
forces = np.append(forces, int(input("Please input position: ")))
# print positions and forces
print(positions)
print(forces)
第三,NumPy 数组并不意味着一次增长一个元素。这个问题不会导致你的错误——对于这么小的数组(大小 (1,)
),它甚至不会产生明显的影响——但这是一个非常糟糕的习惯。许多其他类型,如 Python 的内置列表,都适用于此,但不是 NumPy 数组。
通常要么您事先知道您将拥有多少个值,在这种情况下您可以预先分配数组。如果没有,您通常会从动态构建的某种可迭代对象构建它,例如 list
.** 以下是一些示例:
# Pre-allocating the array
positions = np.zeros((1,), dtype=int)
positions[0] = int(input("Please input position: "))
# Building the array from a dynamically-built list
positions = []
positions.append(int(input("Please input position: ")))
positions = np.array(positions, dtype=int)
# Building the array all at once from a static list
positions = np.array([int(input("Please input position: "))], dtype=int)
* 如果它 确实 尝试就地修改,第一个问题会更糟,因为那时你会尝试修改不可变单例值 None
…
** 对于非常大的数组,不要使用 list
,因为构建临时 list
的成本太高。相反,您通常希望创建生成器或其他类型的惰性迭代器,并使用 np.fromiter
.