2015-09-10 51 views
-1

所以我意識到這既是一個理論問題,也是一個編碼問題,但是如果我有一個包含10個標籤(x1,x2,...,x10)及其相應的「位置」向量(v1,v2, ...,v10)。如何摺疊標籤列表| Python

我想根據它們的L2-範數距離來摺疊它們。例如,如果v1接近於v10,則將所有x10的值重新標記爲x1,依此類推。

因此,最終結果可能會假設看起來像新標籤:(x1,x3,x7,x8)。有沒有辦法巧妙地將它變成(x1',x2',x3',x4')?,這樣人們就不會感到困惑,並且假設新標籤是相同的。

已知: 標籤=矢量NX1的具有所有標籤(1,2,3 ...,10)

例如代碼:

epsilon = 0.2 # defines distance 
change = [] # initialize vector of labels to change 

# matrix is NxN matrix of the pairwise distances between all our vectors (v1,..,v10) 
for i in range(0, distancematrix): 
    for j in range(0, distancematrix): 
     # add all pairs of labels that are "close", so that we may relabel 
     if i!=j and distancematrix[i, j] < epsilon: 
      change.append((i,j)) 

這將產生對列表我想重新標記。是否有一種巧妙的方式來重寫「標籤」,以便合併所有要合併的對,並保留不屬於任何合併的標籤。然後重新組織它從(1,2,3,4),如果我合併6對數字(10-6 = 4)。

謝謝。我意識到這是一個奇怪的問題,所以如果你有問題,請讓我知道!

回答

0

如果標籤不參與任何合併,該怎麼辦?你想保留原始標籤嗎?如果是這樣,如果該標籤超出了新的範圍?

總體來說,我認爲這是簡單地生成只給出標籤的數量新標籤:

new_label_list = ["x"+str(n+1)+"'" for n in range(len(change))] 

對於長度爲4的變化,這給你 [「X1' 」,「X2' 」, 「x3'」,「x4」「]

您是否看到新標籤是如何構建的?

leading "x" 
string version of the index, 1 .. length 
trailing prime character 
+0

是的我確實想保留原來的標籤,如果它不涉及合併,並且如果標籤超出了新的範圍,我想濃縮它。說我結束標籤(1,3,4,5),我想使它labels_new(1,2,3,4),但當然編程。 – ajl123

+0

我的問題是關於標籤的形式。例如,如果您的合併標籤是[「x1」,「x3」,「x4」,「x5」],您是否希望新設置只移動x5標籤,如[「x1」,「x2-prime 「,」x3「,」x4「]?這涉及更多一點。 – Prune

0

這實際上是爲我做的工作。

# creates a list of numbers from 0 to the length of your newlabels vector 
changeto = [i for i in range(0, len(np.unique(newlabels)))] 

# get the unique values of your newlabels (e.g. 0, 3, 4, 5, 10) 
currentlabels = np.unique(newlabels) 

# change all your labels to your new mapping (e.g. 0 -> 0, 3 -> 1, 4 -> 2, etc.) 
for i in range(0, len(changeto)): 
    if currentlabels[i] != changeto[i]: 
     # change the 'states' in newlabels to new label 
     newlabels = [changeto[i] if x==currentlabels[i] else x for x in newlabels] 

也許不漂亮,但是你擁有你的新標籤映射到線0,1,2,...,其中x是你的新濃縮標籤向量的長度。