【问题标题】:Pairwise Similarity and Sorting Samples成对相似性和排序样本
【发布时间】:2019-07-24 01:20:26
【问题描述】:

以下是我正在尝试解决的作业中的一个问题:

相似矩阵的可视化。用一个四维向量(萼片长度、萼片宽度、花瓣长度、花瓣宽度)表示每个样本。对于每两个样本,计算它们的成对相似度。您可以使用欧几里得距离或其他度量来执行此操作。这导致相似度矩阵,其中元素 (i,j) 存储样本 i 和 j 之间的相似度。请对所有样本进行排序,以便同一类别的样本一起出现。使用函数 imagesc() 或任何其他函数可视化矩阵。

这是我目前写的代码:

load('iris.mat'); % create a table of the data
iris.Properties.VariableNames = {'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width' 'Class'}; % change the variable names to their actual meaning
iris_copy = iris(1:150,{'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width'}); % make a copy of the (numerical) features of the table
iris_distance = table2array(iris_copy); % convert the table to an array

% pairwise similarity
D = pdist(iris_distance); % calculate the Euclidean distance and store the result in D
W = squareform(D); % convert to squareform
figure()
imagesc(W); % visualize the matrix

现在,我认为我的编码基本正确,可以回答这个问题。我的问题是如何对所有样本进行排序,以便同一类别的样本一起出现,因为我在创建副本时去掉了名称。它是否已经通过转换为方形进行了排序?其他建议?谢谢!

【问题讨论】:

    标签: matlab euclidean-distance pairwise-distance


    【解决方案1】:

    它应该与原始数据的顺序相同。虽然您可以在之后对其进行排序,但最简单的解决方案实际上是在第 2 行之后和第 3 行之前按类对数据进行排序。

    load('iris.mat'); % create a table of the data
    iris.Properties.VariableNames = {'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width' 'Class'}; % change the variable names to their actual meaning
    % Sort the table here on the "Class" attribute. Don't forget to change the table name
    % in the next line too if you need to.
    iris_copy = iris(1:150,{'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width'}); % make a copy of the (numerical) features of the table
    

    考虑使用排序:

    tblB = sortrows(tblA,'RowNames') 根据表的行名对表进行排序。表的行名称标记沿着表的第一个维度的行。如果 tblA 没有行名,即如果 tblA.Properties.RowNames 为空,则 sortrows 返回 tblA。

    【讨论】:

    • 原始数据已经按类排序了,所以我认为它已经排序了。
    • 如果是这样,那么应该是。虽然很容易检查一些。只需手动计算几个欧几里得距离,并确保它们出现在应该出现的表格中。它应该可以正常工作。
    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多