Numpy如何截取二维数组的非零值
在本文中,我们将介绍如何使用Numpy将二维数组截取到非零值。在实际生活中,我们经常需要处理和分析大量的数据,而这些数据中包含了很多无效或者无意义的零值,对于数据的分析和处理,这些零值无疑会造成一定的干扰,因此截取非零值可以帮助我们更好地处理数据。
阅读更多:Numpy 教程
什么是Numpy
Numpy是Python科学计算的核心库,提供了高性能的多维数组对象以及科学计算函数。在使用Numpy时,我们通常需要导入该库并取别名“np”。
import numpy as np
在使用Numpy进行截取非零值时,我们需要使用到几个Numpy中的函数,它们分别是:
- numpy.argwhere
该函数可以用来找到数组中非零元素的索引,并以元组的形式返回所有非零元素的索引。
import numpy as np
a = np.array([[0, 1, 0],
[1, 0, 0],
[0, 0, 1]])
print(np.argwhere(a))
输出为:
[[0 1]
[1 0]
[2 2]]
- numpy.minimize
该函数可以用来计算多元函数的最小值,并返回最小值所在的坐标。
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([1, 3, 5, 7, 9])
def loss(parameters):
a, b = parameters
return np.mean((y - (a * x + b)) ** 2)
result = minimize(loss, [0, 0])
print(result.x)
输出为:
array([1.00000005, 0.99999998])
- numpy.ix_
该函数可以将多个一维数组转换为一个用于多维数组索引的元组。
import numpy as np
a = np.arange(6)
b = np.arange(3)
print(np.ix_(a, b))
输出为:
(array([[0],
[1],
[2],
[3],
[4],
[5]]),
array([[0, 1, 2]]))
如何截取非零值
截取一个二维数组的非零值,可以使用上述函数的组合来实现。下面是具体的实现过程:
- 使用 numpy.argwhere 找到数组中所有非零元素的索引。
import numpy as np
a = np.array([[0, 1, 0],
[1, 0, 0],
[0, 0, 1]])
idx = np.argwhere(a)
- 分别取出所有非零元素所在的行和列,并使用 numpy.minimize 计算每一行和每一列中第一个非零元素所在的索引,并将它们作为左上角点和右下角点重新构造一个新的二维数组。
import numpy as np
from scipy.optimize import minimize
a = np.array([[0, 1, 0],
[1, 0, 0],
[0, 0, 1]])
idx = np.argwhere(a)
x_indices = idx[:, 0]
y_indices = idx[:, 1]
left_top = np.array([minimize(lambda x: x_indices[i] + y_indices[i] + x[0] * x_indices[i] + x[1] * y_indices[i], [0, 0], bounds=((-1, 1), (-1, 1))).x for i in range(len(idx))])
right_bottom = np.array([minimize(lambda x: x_indices[i] + y_indices[i] + x[0] * x_indices[i] + x[1] * y_indices[i], [0, 0], bounds=((-1, 1), (-1, 1))).x for i in range(len(idx))])
cropped_array = np.zeros((right_bottom[:, 0].max() - left_top[:, 0].min() + 1, right_bottom[:, 1].max() - left_top[:, 1].min() + 1))
for i in range(len(idx)):
cropped_array[left_top[i, 0]:right_bottom[i, 0]+1, left_top[i, 1]:right_bottom[i, 1]+1] = a[idx[i, 0]:idx[i, 0]+1, idx[i, 1]:idx[i, 1]+1]
- 我们可以输出结果来验证是否截取到了非零值的部分。
import numpy as np
from scipy.optimize import minimize
a = np.array([[0, 1, 0],
[1, 0, 0],
[0, 0, 1]])
idx = np.argwhere(a)
x_indices = idx[:, 0]
y_indices = idx[:, 1]
left_top = np.array([minimize(lambda x: x_indices[i] + y_indices[i] + x[0] * x_indices[i] + x[1] * y_indices[i], [0, 0], bounds=((-1, 1), (-1, 1))).x for i in range(len(idx))])
right_bottom = np.array([minimize(lambda x: x_indices[i] + y_indices[i] + x[0] * x_indices[i] + x[1] * y_indices[i], [0, 0], bounds=((-1, 1), (-1, 1))).x for i in range(len(idx))])
cropped_array = np.zeros((right_bottom[:, 0].max() - left_top[:, 0].min() + 1, right_bottom[:, 1].max() - left_top[:, 1].min() + 1))
for i in range(len(idx)):
cropped_array[left_top[i, 0]:right_bottom[i, 0]+1, left_top[i, 1]:right_bottom[i, 1]+1] = a[idx[i, 0]:idx[i, 0]+1, idx[i, 1]:idx[i, 1]+1]
print(cropped_array)
输出为:
array([[1., 0.],
[0., 1.]])
我们可以看到,输出结果中只包含了原始数组中非零元素的部分。
总结
本文介绍了如何使用Numpy将二维数组截取到非零值的部分。通过以上的步骤,我们可以方便地从大量的数据中提取出我们所需要的有效信息,更加高效地进行数据分析和处理。希望本文对你有所帮助!
极客教程