Find Nearest Transition in N-dimensional Array

▼魔方 西西 提交于 2020-05-09 16:26:30

问题


I want to find index of the nearest transition in a numpy ndarray of integers given a current index in an efficient manner. Transition means a change of value.

For example, in the 2D array below, the right output for location (2,4) would be (3,6) (transition from Class 1 to Class 8) at the approximate distance of 2.236. In case of more than one optimum, returning any would suffice.

import seaborn as sns
import numpy as np

step_size = [1,1]  # size of step in each dimension
arr = np.array([[6,6,1,1,1,1,1,1,1,8],[6,1,1,1,1,1,1,1,8,8],[6,1,1,1,1,1,1,1,8,8],[6,1,1,1,1,1,8,8,8,8],[6,6,1,1,1,1,1,8,8,8]])
sns.heatmap(arr, annot=True, cbar=False)

The application is to estimate distances to boundaries. Such as a sample's distance to decision boundary in classification algorithms where an accurate algorithm or formula for that is not available (like xgboost, and unlike SVM and decision trees).

来源:https://stackoverflow.com/questions/61022427/find-nearest-transition-in-n-dimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!