选择 Numpy 数组的最后一个值

Selecting the last values of a Numpy array

我正在做一些基本的线性代数求解,我正在使用 np.linalg.solve() 来做。 np.linalg.solve() 要求 b 由等式的右侧组成,因此对于增广矩阵,需要删除增广(啊哈)。现在我使用的方法如下:

np_exercise1 = np.array([[1,5,7],[-2,-7,-5]])
a = np_exercise1.T[0:2].T
b = np_exercise1.T[2]
solution1 = np.linalg.solve(a,b)
print('x1 = {}\nx2 = {}'.format(solution1[0],solution1[1]))

有没有比矩阵的双转置更优雅的方法?

您可以在 numpy 中执行 multidimensional indexing,其中 : 选择一个维度中的所有数据:

>>> np_exercise1[:, 0:2]
array([[ 1,  5],
       [-2, -7]])
>>> np_exercise1[:, 2]
array([ 7, -5])