您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)python如何實(shí)現(xiàn)在多維數(shù)組中挑選符合條件的全部元素,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
問題產(chǎn)生:今天在編寫神經(jīng)網(wǎng)絡(luò)的Cluster作業(yè)時(shí),需要根據(jù)根據(jù)數(shù)據(jù)標(biāo)簽用不同的顏色畫出數(shù)據(jù)的分布情況,由此學(xué)習(xí)到了這種高效的方法。
傳統(tǒng)思路:用for循環(huán)來挑選符合條件的元素,這樣十分浪費(fèi)時(shí)間。
代碼示例:
from sklearn.datasets.samples_generator import make_blobs import numpy as np import matplotlib.pyplot as plt #product 20 samples and divide them in 4 different types X, label_true = make_blobs(n_samples=20,centers=4) print("Data:{:}".format(X)) print("label_true:{:}".format(label_true)) #eliminate the repeated elements labels=np.unique(label_true) print("labels:{:}".format(labels)) #plot fig = plt.figure() ax = fig.add_subplot(1, 1, 1) colors = 'rgbycm' for index,elem in enumerate(labels): position=label_true==elem print("position{:}:{:}".format(index,position)) plt.scatter(X[position,0],X[position,1],label="cluster %d"%elem,color=colors[index%len(colors)]) plt.show()
實(shí)驗(yàn)結(jié)果:
Data:[[ 6.28987299 1.19041843] [ 2.12673463 -1.90647309] [-8.56276424 1.8136798 ] [ 2.42611937 -3.81970786] [ 1.83488662 -3.10733306] [ 6.28320138 -0.24840258] [-6.74802304 1.13642657] [ 2.21681643 6.28894411] [-7.16100601 0.04482262] [ 1.66858847 3.42225284] [ 3.19972789 4.58804196] [-7.37006942 0.57068008] [ 0.52465584 -2.68794047] [ 2.71075921 3.57281778] [ 5.99343237 0.0120798 ] [ 4.28307033 4.28727222] [ 0.73714246 -2.38643522] [ 5.58384782 -0.62066592] [-8.44295576 -0.05933983] [ 5.33991984 1.24833992]] label_true:[0 2 1 2 2 0 1 3 1 3 3 1 2 3 0 3 2 0 1 0] labels:[0 1 2 3] position0:[ True False False False False True False False False False False False False False True False False True False True] position1:[False False True False False False True False True False False True False False False False False False True False] position2:[False True False True True False False False False False False False True False False False True False False False] position3:[False False False False False False False True False True True False False True False True False False False False]
結(jié)果分析:
我們可以看出黃色部分的作用,第一行 position=label_true==elem 的作用是讓position在label_true==elem的位置置為True,反之為False,從而得到的position是一個(gè)True和False的集合,
而第三行 X[position,0],X[position,1] 就是選擇為True的位置上的橫坐標(biāo)和縱坐標(biāo),打印出來。還有點(diǎn)懵?我們用最簡單的數(shù)組來表示
代碼示例
import numpy as np a=np.empty(shape=[0,4], dtype=int) a=np.append(a,[[1,2,3,4],[2,3,4,5],[7,8,9,10]],axis=0) position=[True,False,True] print(a) print(a[position,3])
結(jié)果:
[[ 1 2 3 4] [ 2 3 4 5] [ 7 8 9 10]] [ 4 10]
結(jié)果分析:
顯然這是一個(gè)3行4列的矩陣,我們用position得到的是[a[0],a[2]],然后取a[0]和a[2]的第4個(gè)元素,則為4和10.
關(guān)于“python如何實(shí)現(xiàn)在多維數(shù)組中挑選符合條件的全部元素”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。