两个2D阵列,将一个2D阵列切成一排并与第二个2D阵列一起操作

时间:2019-10-24 21:56:15

标签: python python-3.x numpy numpy-ndarray numpy-broadcasting

我被困在有两个numpy 2D数组的位置:

Array1

test[] = [[0.54131721 0.52305685 0.42921551, 0.37434461 0.52591475 0.36184407]
 [0.53091097 0.3000469  0.39346106, 0.29261769 0.3806552  0.33904193]
 [0.29331853 0.44518117 0.41390863, 0.2510257  0.50481932 0.43607184]]

Array2

train[] =[[0.5301304,  0.62645837, 0.44524917, 0.40806674 0.46013734 0.61033772]
 [0.43333892 0.46062429 0.56937923, 0.6451305  0.33103777 0.35859095]
 [0.60879428 0.72451976 0.2661216, 0.38850336 0.41685737 0.57226228]]

我想创建一个函数,在该函数中,我将传递一个完整的train []二维数组和一个test []一维数组(逐行);

temp_test[] = [0.54131721, 0.52305685, 0.42921551, 0.37434461, 0.52591475, 0.36184407] 
train = [[0.5301304,  0.62645837, 0.44524917, 0.40806674 0.46013734 0.61033772],[0.43333892, 0.46062429, 0.56937923, 0.6451305,  0.33103777, 0.35859095], [0.60879428, 0.72451976, 0.2661216, 0.38850336, 0.41685737 ,0.57226228]]

我已经准备好对两个2D数组执行相同的一组操作的功能,但是我需要在test []行上一个接一个地进行操作。这是我的两个2D数组的代码:

def dist_mat(x,y):
    return np.sqrt(np.sum((x- y[:,None])**2, axis=-1))

dist_mat(train,test)

0 个答案:

没有答案