选择一个Numpy数组的最后一个值

时间:2019-12-08 22:34:03

标签: python numpy matrix linear-algebra

我正在做一些基本的线性代数求解,并且正在使用np.linalg.solve()进行求解。 np.linalg.solve()要求b由等式的右边组成,因此,对于扩充矩阵,需要删除扩充(ahem)。现在,我正在使用的方法如下:

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]))

与矩阵的两次转置相比,有没有比这更优雅的方法了?

1 个答案:

答案 0 :(得分:0)

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

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