簡體   English   中英

成對相似性和排序樣本

[英]Pairwise Similarity and Sorting Samples

以下是我要解決的作業中的問題:

可視化相似度矩陣。 用四維向量表示每個樣本(間隔長度,萼片寬度,花瓣長度,花瓣寬度)。 對於每兩個樣本,計算它們的成對相似度。 您可以使用歐幾里德距離或其他指標來執行此操作。 這導致了一個相似度矩陣,其中元素(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

現在,我認為我的編碼基本上可以回答這個問題。 我的問題是如何對所有樣本進行排序,以使同一類別中的樣本同時出現,因為在創建副本時我擺脫了名稱。 它已經通過轉換為正方形進行排序了嗎? 還有其他建議嗎? 謝謝!

它應與原始數據的順序相同。 雖然您可以在之后對其進行排序,但最簡單的解決方案是在第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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM