numpy必知必會-第九天

41 vstack 與 hstack。

把兩個array分別進(jìn)行水平合并與垂直合并。
例如:
輸入

a = np.arange(10)
b = np.arange(10)

輸出
垂直合并

array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

水平合并

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

解決辦法:

a = np.arange(10)
b = np.arange(10)
new_ab = np.vstack((a, b))
new_ab

輸出:

array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
new_ab2 = np.hstack((a,b))
new_ab2

輸出:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

42 在array中進(jìn)行概率采樣

例如:
在data array內(nèi)的元素分別進(jìn)行采樣,其元素的分布概率為0.5, 0.25, 0.25。
data array如下

data  = np.array(['apple', 'banana', 'peach'])

解決辦法:

data  = np.array(['apple', 'banana', 'peach'])
species_out = np.random.choice(data, 150, p=[0.5, 0.25, 0.25])
len(species_out[species_out=='apple']), len(species_out[species_out=='banana']), len(species_out[species_out=='peach'])

輸出:

(79, 37, 34)

可見三種元素的種類分布基本與[0.5, 0.25, 0.25]一致。

43 把a(bǔ)rray 中的元素進(jìn)行排序,并輸出第二大的元素。

例如構(gòu)建array a如下:

a=np.arange(100)

解決辦法:

np.sort(a)[-2]

np.sort可以方便的對array進(jìn)行排序,默認(rèn)是從小到大。

44 按照指定列,對2D array進(jìn)行排序。

例如:
構(gòu)建2D 數(shù)據(jù)

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter=',', dtype='object')

按照第一列對iris array進(jìn)行排序。
解決辦法:

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter=',', dtype='object')

print(iris[iris[:,0].argsort()][:20])

輸出

[[b'4.3' b'3.0' b'1.1' b'0.1' b'Iris-setosa']
 [b'4.4' b'3.2' b'1.3' b'0.2' b'Iris-setosa']
 [b'4.4' b'3.0' b'1.3' b'0.2' b'Iris-setosa']
 [b'4.4' b'2.9' b'1.4' b'0.2' b'Iris-setosa']
 [b'4.5' b'2.3' b'1.3' b'0.3' b'Iris-setosa']
 [b'4.6' b'3.6' b'1.0' b'0.2' b'Iris-setosa']
 [b'4.6' b'3.1' b'1.5' b'0.2' b'Iris-setosa']
 [b'4.6' b'3.4' b'1.4' b'0.3' b'Iris-setosa']
 [b'4.6' b'3.2' b'1.4' b'0.2' b'Iris-setosa']
 [b'4.7' b'3.2' b'1.3' b'0.2' b'Iris-setosa']
 [b'4.7' b'3.2' b'1.6' b'0.2' b'Iris-setosa']
 [b'4.8' b'3.0' b'1.4' b'0.1' b'Iris-setosa']
 [b'4.8' b'3.0' b'1.4' b'0.3' b'Iris-setosa']
 [b'4.8' b'3.4' b'1.9' b'0.2' b'Iris-setosa']
 [b'4.8' b'3.4' b'1.6' b'0.2' b'Iris-setosa']
 [b'4.8' b'3.1' b'1.6' b'0.2' b'Iris-setosa']
 [b'4.9' b'2.4' b'3.3' b'1.0' b'Iris-versicolor']
 [b'4.9' b'2.5' b'4.5' b'1.7' b'Iris-virginica']
 [b'4.9' b'3.1' b'1.5' b'0.1' b'Iris-setosa']
 [b'4.9' b'3.1' b'1.5' b'0.1' b'Iris-setosa']]

我們只輸出前20行。

45 查找一個array中最高頻的元素

例如:
構(gòu)建數(shù)據(jù):

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter=',', dtype='object')

查找第三列中最高頻的元素。

解決辦法:

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
iris = np.genfromtxt(url, delimiter=',', dtype='object')

vals, counts = np.unique(iris[:, 2], return_counts=True)
print(vals[np.argmax(counts)])

輸出

b'1.5'

這意味著在iris中第三列最高頻的元素為b'1.5'。

要點(diǎn)解析:np.unique(iris[:, 2], return_counts=True)對iris的第三列進(jìn)行unique操作,返回vals 與 counts。vals存放的是unique之后的元素(去掉重復(fù)項),counts里面存放對應(yīng)元素出現(xiàn)的頻次。

array([b'1.0', b'1.1', b'1.2', b'1.3', b'1.4', b'1.5', b'1.6', b'1.7',
        b'1.9', b'3.0', b'3.3', b'3.5', b'3.6', b'3.7', b'3.8', b'3.9',
        b'4.0', b'4.1', b'4.2', b'4.3', b'4.4', b'4.5', b'4.6', b'4.7',
        b'4.8', b'4.9', b'5.0', b'5.1', b'5.2', b'5.3', b'5.4', b'5.5',
        b'5.6', b'5.7', b'5.8', b'5.9', b'6.0', b'6.1', b'6.3', b'6.4',
        b'6.6', b'6.7', b'6.9'], dtype=object),
 array([ 1,  1,  2,  7, 12, 14,  7,  4,  2,  1,  2,  2,  1,  1,  1,  3,  5,
         3,  4,  2,  4,  8,  3,  5,  4,  5,  4,  8,  2,  2,  2,  3,  6,  3,
         3,  2,  2,  3,  1,  1,  1,  2,  1])

np.argmax(counts)返回counts中的最大值索引,本例中將返回5。則最后vals[5]返回b'1.5'。

多加練習(xí),你就是下一個numpy 高手。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基礎(chǔ)篇NumPy的主要對象是同種元素的多維數(shù)組。這是一個所有的元素都是一種類型、通過一個正整數(shù)元組索引的元素表格(...
    oyan99閱讀 5,290評論 0 18
  • 26 把numpy array元素的指定列合成新的array 例如:輸入 把每行的第五列,取出并組成新的array...
    人工智能人話翻譯官閱讀 1,412評論 1 5
  • 31 如何查找百分位數(shù) 第q個百分位數(shù)是這樣一個值,它使得至少有q%的數(shù)據(jù)項小于或等于這個值,且至少有(100-q...
    人工智能人話翻譯官閱讀 647評論 1 6
  • 亞洲寵物展3
    罷不爾奔閱讀 108評論 0 0
  • 暮春總有些曖昧 癢得小動物們 到處亂跑 不知是脫毛還是 盡日惹飛絮 我知道 它們清醒得 什么似的 身體里的鐘擺 總...
    諾鶇竹隱閱讀 1,174評論 27 25

友情鏈接更多精彩內(nèi)容