K-NN从Matlab到Python的邻居

时间:2017-04-14 20:51:53

标签: python matlab

这是我去年写的关于概率分布的代码 k-Matarest中最近的邻居:

   function [ p_y_x ] = p_y_x_KNN(y, K )
    % Function calculates distribution p(y|x) for each class and each object 
    % from test dataset using KNN classifier
    % y - matrix of sorted class labels for training dataset N1xN2
    % K - number of nearest neighbors
    % p_y_x - probability matrix for object in X
    % each row of matrix represents distribution p(y|x)) N1xM

    % N1 - number of elements in testing dataset
    % N2 - number of elements in training dataset
    % M - number of classes

    N1 = size(y,1);
    M = length(unique(y));
    p_y_x = zeros(N1,M);
    N2 = size(y,2);
    for i=1:N1
        for j=1:M
            p_y_x(i,j) = (1/K)*sum(y(i, 1:K) == j);
        end
    end
    end

它有效。现在我需要将它翻译成Python。到目前为止我有这个,我无法理解它有什么问题。它不会起作用。

def p_y_x_knn(y, k):
    """
    Function calculates conditional probability p(y|x) for
    all classes and all objects from test set using KNN classifier
    :param y: matrix of sorted labels for training set N1xN2
    :param k: number of nearest neighbours
    :return: matrix of probabilities for objects X
    """
    N1, N2 = y.shape
    M = len(np.unique(y))
    p_y_x = np.zeros(shape=(N1, M))
    for i in range(1,N1):
        for j in range(1,M):
            p_y_x[i, j] = (1/k)*(np.sum(y[i,0:k] == j+1))
    return p_y_x

我无法粘贴回溯,因为这个函数只是一个更大的项目的一部分,我得到的唯一输出是'FAIL',甚至不像往常那样'错误',我可以看到什么不是工作。排序标签的y矩阵就像其他已提供的标签一样正确。也许有些人在我的推理中可以看到任何明显的错误?

修改 更改了代码:

N1, N2 = y.shape
M = len(np.unique(y))
p_y_x = np.zeros((N1, M))
for i in range(N1):
    for j in range(M):
        p_y_x[i, j] = (1.0/k)*(np.sum(y[i,0:k-1] == j))
return p_y_x

我改变了范围和k作为@StackPlayer建议,我丢失了'j + 1'因为我相信,不应该增加。我仍然没有收到任何错误,只是一个'失败'。

2 个答案:

答案 0 :(得分:2)

您可能需要将0:k调整为0:k-1 对于for循环一样,使用范围就是这样(不要试图将MATLAB 1索引强制执行到Python的0索引上!)

答案 1 :(得分:1)

Stack Player说得对。我对此答案的补充是将python3与此函数一起使用或将(1/k)更改为(1.0/k)并使用python2。*因为在python2 (1/k)中,其中k是整数,返回整数0和所有元素是零。

好的,在Python和Matlab代码上使用此代码进行的测试为我提供了相同的结果。

def p_y_x_knn(y, k):
    """
    Function calculates conditional probability p(y|x) for
    all classes and all objects from test set using KNN classifier
    :param y: matrix of sorted labels for training set N1xN2
    :param k: number of nearest neighbours
    :return: matrix of probabilities for objects X
    """
    N1, N2 = y.shape
    M = len(np.unique(y))
    p_y_x = np.zeros((N1, M))
    for i in range(N1):
        for j in range(M):
            p_y_x[i, j] = (1.0/k)*(np.sum(y[i,0:k] == j+1))
    return p_y_x