我刚刚开始使用 Python,但我无法理解应该如何实现以下目标(我是一名 Java 程序员)。
这是初始代码:
def compute_distances_two_loops(self, X):
"""
Compute the distance between each test point in X and each training point
in self.X_train using a nested loop over both the training data and the
test data.
Inputs:
- X: A numpy array of shape (num_test, D) containing test data.
Returns:
- dists: A numpy array of shape (num_test, num_train) where dists[i, j]
is the Euclidean distance between the ith test point and the jth training
point.
"""
num_test = X.shape[0]
num_train = self.X_train.shape[0]
dists = np.zeros((num_test, num_train))
for i in range(num_test):
for j in range(num_train):
#####################################################################
# TODO: #
# Compute the l2 distance between the ith test point and the jth #
# training point, and store the result in dists[i, j]. You should #
# not use a loop over dimension. #
#####################################################################
dists[i, j] = np.sum(np.square(X[i] - self.X_train[j]))
#####################################################################
# END OF YOUR CODE #
#####################################################################
return dists
下面的代码应该少一个嵌套循环,同时仍输出相同的数组:
def compute_distances_one_loop(self, X):
"""
Compute the distance between each test point in X and each training point
in self.X_train using a single loop over the test data.
Input / Output: Same as compute_distances_two_loops
"""
num_test = X.shape[0]
num_train = self.X_train.shape[0]
dists = np.zeros((num_test, num_train))
for i in range(num_test):
tmp = '%s %d' % ("\nfor i:", i)
print(tmp)
print(X[i])
print("end of X[i]")
print(self.X_train[:]) # all the thing [[ ... ... ]]
print(": before, i after")
print(self.X_train[i]) # just a row
print(self.X_train[i, :])
#######################################################################
# TODO: #
# Compute the l2 distance between the ith test point and all training #
# points, and store the result in dists[i, :]. #
#######################################################################
dists[i, :] = np.sum(np.square(X[i] - self.X_train[i, :]))
print(dists[i])
#######################################################################
# END OF YOUR CODE #
#######################################################################
return dists
看起来像this应该对我有帮助,但我还是不明白。
你可以看到,我的陷阱之一是我对“:”的确切工作原理了解甚少。
我花了几个小时试图弄清楚这个问题,但似乎我确实缺乏一些核心知识。有人可以帮我吗?这个练习是针对斯坦福大学视觉识别类(class)的:这是第一个作业,但它不是我真正的家庭作业,因为我只是为了自己的乐趣而做这门类(class)。
目前,我的代码段输出的是整行的 two_loops
对角线的正确值。我不明白如何将 dists[i, :]
中的 :
与 - self.X_train[i, :]< 同步
部分。如何计算 X[i] 减去遍历整个 self.X_train 的迭代?
注意:num_test
为 500x3072,num_train
为 5000x3072。 3072来自32x32x3,即32x32图片的RGB值。 dists[i,j]
是一个 500x5000 矩阵,映射 num_test
第 i 个元素与 num_train
第 j 个元素之间的 L2 距离。
最佳答案
def compute_distances_one_loop(self, X):
"""
Compute the distance between each test point in X and each training point
in self.X_train using a single loop over the test data.
Input / Output: Same as compute_distances_two_loops
"""
num_test = X.shape[0]
num_train = self.X_train.shape[0]
dists = np.zeros((num_test, num_train))
for i in range(num_test):
tmp = '%s %d' % ("\nfor i:", i)
print(tmp)
#######################################################################
# TODO: #
# Compute the l2 distance between the ith test point and all training #
# points, and store the result in dists[i, :]. #
#######################################################################
dists[i] = np.sum(np.square(X[i] - self.X_train), axis=1)
print(dists[i])
#######################################################################
# END OF YOUR CODE #
#######################################################################
return dists
删除循环中带有 self.X_train 的打印,因为长度不同。 (索引超出范围异常) 我不确定这是否会删除第二个循环,但这是一个可行的解决方案。
另一条评论,我认为你对欧氏距离公式的理解是错误的。 您缺少最后的 sqrt。
关于python - 替换嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51902011/