代码之家  ›  专栏  ›  技术社区  ›  Blair

向量的最大努力分类算法

  •  4
  • Blair  · 技术社区  · 15 年前

    给定四个表示“类”的二进制向量:

    [1,0,0,0,0,0,0,0,0,0]
    [0,0,0,0,0,0,0,0,0,1]
    [0,1,1,1,1,1,1,1,1,0]
    [0,1,0,0,0,0,0,0,0,0]
    

    有哪些方法可以将浮点值的向量分类为这些“类”中的一个?

    在大多数情况下,基本舍入工作:

    round([0.8,0,0,0,0.3,0,0.1,0,0,0]) = [1 0 0 0 0 0 0 0 0 0] 
    

    但我该如何处理一些干扰呢?

    round([0.8,0,0,0,0.6,0,0.1,0,0,0]) != [1 0 0 0 0 1 0 0 0 0]
    

    第二个案例对1000000000来说应该是更好的匹配,但是我完全失去了解决方案,因为没有明确的匹配。

    我想用matlab来完成这个任务。

    3 回复  |  直到 14 年前
        1
  •  5
  •   Jacob    15 年前

    找到 SSD (sum of squared differences) 对每个“类”使用测试向量,并使用具有最少SSD的测试向量。

    下面是一些代码:我添加了一个 0 到测试向量的末尾,因为它只有9位数字,而类只有10位。

    CLASSES = [1,0,0,0,0,0,0,0,0,0
               0,0,0,0,0,0,0,0,0,1
               0,1,1,1,1,1,1,1,1,0
               0,1,0,0,0,0,0,0,0,0];
    
    TEST = [0.8,0,0,0,0.6,0,0.1,0,0,0];
    
    % Find the difference between the TEST vector and each row in CLASSES
    difference = bsxfun(@minus,CLASSES,TEST);
    % Class differences
    class_diff = sum(difference.^2,2);
    % Store the row index of the vector with the minimum difference from TEST
    [val CLASS_ID] = min(class_diff);
    % Display
    disp(CLASSES(CLASS_ID,:))
    

    为了说明目的, difference 如下所示:

     0.2    0   0   0   -0.6    0   -0.1    0   0   0
    -0.8    0   0   0   -0.6    0   -0.1    0   0   1
    -0.8    1   1   1    0.4    1    0.9    1   1   0
    -0.8    1   0   0   -0.6    0   -0.1    0   0   0
    

    每节课到测试的距离是这样的, class_diff 以下内容:

     0.41
     2.01
     7.61
     2.01
    

    显然,第一场比赛是最好的,因为它的差别最小。

        2
  •  2
  •   Community CDub    8 年前

    这和 Jacob 只做了 不同的距离测量:


    %%
    CLASSES = [1,0,0,0,0,0,0,0,0,0
               0,0,0,0,0,0,0,0,0,1
               0,1,1,1,1,1,1,1,1,0
               0,1,0,0,0,0,0,0,0,0];
    
    TEST = [0.8,0,0,0,0.6,0,0.1,0,0,0];
    
    %%
    % sqrt( sum((x-y).^2) )
    euclidean = sqrt( sum(bsxfun(@minus,CLASSES,TEST).^2, 2) );
    
    % sum( |x-y| )
    cityblock = sum(abs(bsxfun(@minus,CLASSES,TEST)), 2);
    
    % 1 - dot(x,y)/(sqrt(dot(x,x))*sqrt(dot(y,y)))
    cosine = 1 - ( CLASSES*TEST' ./ (norm(TEST)*sqrt(sum(CLASSES.^2,2))) );
    
    % max( |x-y| )
    chebychev = max( abs(bsxfun(@minus,CLASSES,TEST)), [], 2 );
    
    dist = [euclidean cityblock cosine chebychev];
    
    %%
    [minDist classIdx] = min(dist);
    

    选择你喜欢的那个:)

        3
  •  1
  •   klynch    15 年前

    一个简单的欧几里得距离算法就足够了。距离该点最小的班级将是你的候选人。

    http://en.wikipedia.org/wiki/Euclidean_distance

    推荐文章