如何将阵列切成最小值

我正在尝试定义一个函数,该函数查找数组的最小值并将其切成该值(正负5个位置)。我的数组看起来像这样:


[[ 0.          9.57705087]

 [ 0.0433      9.58249315]

 [ 0.0866      9.59745942]

 [ 0.1299      9.62194967]

 [ 0.1732      9.65324278]

 [ 0.2165      9.68725702]

 [ 0.2598      9.72263184]

 [ 0.3031      9.75256437]

 [ 0.3464      9.77025178]

 [ 0.3897      9.76889121]

 [ 0.433       9.74167982]

 [ 0.4763      9.68589645]

 [ 0.5196      9.59881999]

 [ 0.5629      9.48861383]

 [ 0.6062      9.3593597 ]]

但是,我正在处理更大的集合,并且需要一个可以自动完成该函数的函数,而无需手动查找最小值,然后围绕该集合对数组进行切片。我想找到数组[:,1]值的最小值,然后将切片应用于整个数组。


慕尼黑5688855
浏览 132回答 2
2回答

largeQ

用于获取最小值的索引。这将仅使用第二列来执行此操作(您尚未指定它是否是跨列的最小值)。np.argmin()your_array[:np.argmin(your_array[:, 1]), :]要将其切成比最小值更远的 5 个值,请使用:your_array[:np.argmin(your_array[:, 1]) + 5, :]

陪伴而非守候

给定您的目标数组:import numpy as npanarray = np.array([[ 0., 9.57705087],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.0433, 9.58249315],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.0866, 9.59745942],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.1299, 9.62194967],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.1732, 9.65324278],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.2165, 9.68725702],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.2598, 9.72263184],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.3031, 9.75256437],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.3464, 9.77025178],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.3897, 9.76889121],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.433, 9.74167982],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.4763, 9.68589645],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.5196, 9.59881999],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.5629, 0.48861383],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ 0.6062, 9.3593597]])此函数将完成以下工作:def slice_by_five(array):&nbsp; &nbsp; argmin = np.argmin(array[:,1])&nbsp; &nbsp; if argmin < 5:&nbsp; &nbsp; &nbsp; &nbsp; return array[:argmin+6,:]&nbsp; &nbsp; return array[argmin-5:argmin+6,:]check = slice_by_five(anarray)print(check)输出:[[0.3897&nbsp; &nbsp; &nbsp;9.76889121]&nbsp;[0.433&nbsp; &nbsp; &nbsp; 9.74167982]&nbsp;[0.4763&nbsp; &nbsp; &nbsp;9.68589645]&nbsp;[0.5196&nbsp; &nbsp; &nbsp;9.59881999]&nbsp;[0.5629&nbsp; &nbsp; &nbsp;9.48861383]&nbsp;[0.6062&nbsp; &nbsp; &nbsp;9.3593597 ]]该函数当然可以推广以考虑任何大小的邻域:ndef slice_by_n(array, n):&nbsp; &nbsp; argmin = np.argmin(array[:,1])&nbsp; &nbsp; if argmin < n:&nbsp; &nbsp; &nbsp; &nbsp; return array[:argmin+n+1,:]&nbsp; &nbsp; return array[argmin-n:argmin+n+1,:]check = slice_by_n(anarray, 2)print(check)输出:[[0.5196&nbsp; &nbsp; &nbsp;9.59881999]&nbsp;[0.5629&nbsp; &nbsp; &nbsp;9.48861383]&nbsp;[0.6062&nbsp; &nbsp; &nbsp;9.3593597 ]]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java