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 高手。