neighbors.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:FreeDiscovery 作者: FreeDiscovery 项目源码 文件源码
def kneighbors(self, X=None, batch_size=5000):
        """Finds the K-neighbors of a point.
        Returns indices of and distances to the neighbors of each point.
        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            the input array
        batch_size : int
            the batch size
        Returns
        -------
        S_cos : array [n_samples, n_categories]
            the cosine similarity to closest point in each category
        indices : array [n_samples, n_categories]
            Indices of the nearest points in the population matrix.
        --------
        """
        X = check_array(X, accept_sparse='csr')

        n_classes = len(self._mod)

        S_res = np.zeros((X.shape[0], n_classes), dtype='float')
        nn_idx_res = np.zeros((X.shape[0], n_classes), dtype='int')

        for imod in range(n_classes):
            D_i, nn_idx_i_loc = _chunk_kneighbors(self._mod[imod].kneighbors,
                                                  X,
                                                  batch_size=batch_size)

            # only NearestNeighbor-1 (only one column in the kneighbors output)
            # convert from eucledian distance in L2 norm space to cosine
            # similarity
            # S_cos = seuclidean_dist2cosine_sim(D_i[:,0])
            S_res[:, imod] = 1 - D_i[:, 0]
            # map local index within index_mapping to global index
            nn_idx_res[:, imod] = self.index_mapping[imod][nn_idx_i_loc[:, 0]]

        return S_res, nn_idx_res
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号