两个2D numpy数组之间的运算

时间:2019-10-24 18:47:42

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

我正在尝试对两个2D numpy数组进行操作,以使第一个numpy数组的每一行都对第二个numpy数组的所有行进行操作。

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

这是我保存两个数组的方式:

import numpy as np
trainingData = np.genfromtxt('trainingData.csv', delimiter=',')
inTraining = trainingData[:, :-1]
print(inTraining)
testData = np.genfromtxt('testData.csv', delimiter=',')
inTest = testData[:, :-1]
print(inTest)

这是我尝试过的方法,似乎还没有结束:

def euclideanDistance( c1, c2):
        d1 = inTraining[]
        d2 = inTest[]
        a = math.sqrt( (d1[0]-d2[0])**2 + (d1[1]-d2[1])**2 )
        print(a)
        return a

预期输出应类似于包含以下各项之间的运算结果的第一个列表:

[0.54131721 0.52305685 0.42921551, 0.37434461 0.52591475 0.36184407] and [[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]]

1 个答案:

答案 0 :(得分:1)

IIUC,您想计算test的每一行到train的每一行之间的距离。那是distance_matrix

from scipy.spatial import distance_matrix
distance_matrix(test,train)

输出:

array([[0.27979822, 0.38277359, 0.35792442],
       [0.44997152, 0.43972939, 0.51706358],
       [0.3833412 , 0.48532177, 0.49455157]])

如果只想使用numpy代码,则可以查看distance_matrix的代码以查看发生了什么。基本上,这是广播动作:

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

dist_mat(train,test)

输出:

array([[0.27979822, 0.38277359, 0.35792442],
       [0.44997152, 0.43972939, 0.51706358],
       [0.3833412 , 0.48532177, 0.49455157]])