打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
计算两个矩阵之间的欧式距离

在我们使用k-NN模型时,需要计算测试集中每一点到训练集中每一点的欧氏距离,即需要求得两矩阵之间的欧氏距离。在实现k-NN算法时通常有三种方案,分别是使用两层循环,使用一层循环和不使用循环。

使用两层循环

分别对训练集和测试集中的数据进行循环遍历,计算每两个点之间的欧式距离,然后赋值给dist矩阵。此算法没有经过任何优化。

num_test = X.shape[0]    num_train = self.X_train.shape[0]    dists = np.zeros((num_test, num_train))     for i in xrange(num_test):      for j in xrange(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.                                    #        #####################################################################        # pass        dists[i][j] = np.sqrt(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 xrange(num_test):      #######################################################################      # TODO:                                                               #      # Compute the l2 distance between the ith test point and all training #      # points, and store the result in dists[i, :].                        #      #######################################################################      # pass      dists[i] = np.sqrt(np.sum(np.square(self.X_train - X[i]), axis = 1))      #######################################################################      #                         END OF YOUR CODE                            #      #######################################################################    return dists

不使用循环

运算效率最高的算法是将训练集和测试集都使用矩阵表示,然后使用矩阵运算的方法替代之前的循环操作。但此操作需要我们对矩阵的运算规则非常熟悉。接下来着重记录如何计算两个矩阵之间的欧式距离。

记录测试集矩阵P的大小为M*D,训练集矩阵C的大小为N*D(测试集中共有M个点,每个点为D维特征向量。训练集中共有N个点,每个点为D维特征向量)
Pi是P的第i行,记Cj是C的第j行
Pi=[Pi1Pi2PiD]Cj=[Cj1Cj2CjD]


首先计算PiCj之间的距离dist(i,j)
d(Pi,Cj)=(Pi1Cj1)2+(Pi2Cj2)2++(PiDCjD)2=(Pi12+Pi22++PiD2)+(Cj12+Cj22++CjD2)2×(Pi1Cj1+Pi2Cj2++PiDCiD)=Pi2+Cj22×PiCjT


我们可以推广到距离矩阵的第i行的计算公式
dist[i]=(Pi2Pi2Pi2)+(C12C22CN2)2×Pi(C1TC2TCNT)=(Pi2Pi2Pi2)+(C12C22CN2)2×PiCT


继续将公式推广为整个距离矩阵
dist=(P12P12P12P22P22P22PM2PM2PM2)+(C12C22CN2C12C22CN2C12C22CN2)2×PCT

表示为python代码:

def compute_distances_no_loops(self, X):    """    Compute the distance between each test point in X and each training point    in self.X_train using no explicit loops.    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))     #########################################################################    # TODO:                                                                 #    # Compute the l2 distance between all test points and all training      #    # points without using any explicit loops, and store the result in      #    # dists.                                                                #    #                                                                       #    # You should implement this function using only basic array operations; #    # in particular you should not use functions from scipy.                #    #                                                                       #    # HINT: Try to formulate the l2 distance using matrix multiplication    #    #       and two broadcast sums.                                         #    #########################################################################    # pass    dists = np.sqrt(-2*np.dot(X, self.X_train.T) + np.sum(np.square(self.X_train), axis = 1) + np.transpose([np.sum(np.square(X), axis = 1)]))    #########################################################################    #                         END OF YOUR CODE                              #    #########################################################################    return dists
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
经典机器学习算法-第四章K近邻
数学推导 纯Python实现机器学习算法13:Lasso回归
用Python实现机器学习算法—— K 最近邻算法
混合专家系统(Mixture of experts)
Python实现机器学习算法——Ridge岭回归
基于TensorFlow的FM实现
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服