gpt4 book ai didi

python - 替换嵌套循环

转载 作者:太空宇宙 更新时间:2023-11-04 02:19:46 27 4
gpt4 key购买 nike

我刚开始使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com