您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Python中Numpy庫的詳細(xì)講解”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
首先得了解下什么是Numpy,從我的印象中,一般提到這個(gè)工具都會(huì)和機(jī)器學(xué)習(xí)關(guān)聯(lián)起來了。當(dāng)然和實(shí)際的理解又很多的差距。
簡單來說,Python本身不是設(shè)計(jì)為科學(xué)計(jì)算的語言,但是Python隨著使用的普及和大量的擴(kuò)展,這方面的需求會(huì)越來越大,于是有了Numpy來作為補(bǔ)充。
下載Numpy可以使用pip,一個(gè)命令即可搞定pip install numpy,大概10多M
Numpy相比TensorFlow的環(huán)境搭建要容易多了,校驗(yàn)的方式也很簡單,import即可。
>>> import numpy
如果要查看版本可以使用如下的方式。
>>> numpy.version.full_version
'1.13.3'
>>>
當(dāng)然我們可以指定別名,使用起來更簡便一些,我們后續(xù)的測試都是這么玩。
>>> import numpy as np
>>> np.version.full_version
'1.13.3'
我們先來熱熱身。
得到一個(gè)范圍10以內(nèi)的整數(shù)串,確切的說是數(shù)組。
>>> a = np.arange(10)
>>> print a
[0 1 2 3 4 5 6 7 8 9]
打印出來看是數(shù)組了
>>> type(a)
<type 'numpy.ndarray'>
然后我們變個(gè)花樣,把這個(gè)數(shù)組改造成一個(gè)2部分,每一部分是5個(gè)。
>>> a = a.reshape(2,5)
>>> print a
[[0 1 2 3 4]
[5 6 7 8 9]]
可以繼續(xù)重構(gòu),重構(gòu)成20個(gè)元素,然后分成4部分,每個(gè)部分就是5個(gè)元素
>>> a = np.arange(20)
>>> a = a.reshape(4,5)
>>> print a
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
還可以構(gòu)造高維的數(shù)組,比如下面這樣。
>>> a = a.reshape(2,2,5)
>>> print a
[[[ 0 1 2 3 4]
[ 5 6 7 8 9]]
[[10 11 12 13 14]
[15 16 17 18 19]]]
>>>
如果輸入(2,2,6)就不可以了,明顯的2*2*6=24,已經(jīng)溢出了。
>>> a = a.reshape(2,2,6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: cannot reshape array of size 20 into shape (2,2,6)
>>>
我們得到數(shù)組的維度
>>> a.ndim
3
得到數(shù)組各維度的大小
>>> a.shape
(2L, 2L, 5L)
查看數(shù)組的元素個(gè)數(shù)
>>> a.size
20
然后我們創(chuàng)建數(shù)組,可以轉(zhuǎn)換列表來得到,
>>> raw = [0,1,2,3,4]
>>> a = np.array(raw)
>>> a
array([0, 1, 2, 3, 4])
>>> print a
[0 1 2 3 4]
換個(gè)略微復(fù)雜的,也是一樣的操作。
>>> raw = [[0,1,2,3,4],[5,6,7,8,9]]
>>> b = np.array(raw)
>>> b
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>>
如果要得到顯示為0的數(shù)組,可以使用這種方式zeros
>>> d = (4,5)
>>> np.zeros(d)
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])
當(dāng)然效果不理想都顯示了小數(shù)點(diǎn),可以格式化,改成整型
>>> np.ones(d,dtype=int)
array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])
得到隨機(jī)數(shù),一口氣生成5個(gè)。
>>> np.random.rand(5)
array([ 0.61643689, 0.15915655, 0.20558268, 0.75157157, 0.50395262])
按照這個(gè)思路,隨機(jī)生成100個(gè)也是秒級。
再來看看數(shù)組運(yùn)算
這個(gè)部分就逐漸顯示出優(yōu)勢了,可以支持計(jì)算。
>>> a = np.array([[1,2],[2,5]])
>>> print a
[[1 2]
[2 5]]
>>> b = np.array([[2,3],[5,8]])
>>> print a+b
[[ 3 5]
[ 7 13]]
得到一些最大值,最小值。
>>> a = np.arange(20).reshape(4,5)
>>> print a
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
>>> print str(a.sum())
190
>>> print str(a.max())
19
>>> print str(a.min())
0
>>> print str(a.max(axis=1))
[ 4 9 14 19]
>>>
可以很容易的變換。
>>> b = np.arange(2,45,3).reshape(5,3)
>>> b = np.mat(b)
>>> print b
[[ 2 5 8]
[11 14 17]
[20 23 26]
[29 32 35]
[38 41 44]]
如果最大的數(shù)是2,從0開始,取5個(gè)數(shù)是這樣的寫法
>>> np.linspace(0,2,5)
array([ 0. , 0.5, 1. , 1.5, 2. ])
如果是9個(gè)數(shù),則是如下的方式。
>>> np.linspace(0,2,9)
array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])
>>>
>>> a = np.array([[3,2],[5,9]])
>>> print a[0][1]
2
>>> print a[1][0]
5
>>> print a[1,0]
5
>>> b = a
>>> a[0][1]=100
>>> print a
[[ 3 100]
[ 5 9]]
>>> print b
[[ 3 100]
[ 5 9]]
上面的結(jié)果看起來有些奇怪,其實(shí)問題的原因是:Python不是真正將a復(fù)制一份給b,而是將b指到了a對應(yīng)數(shù)據(jù)的內(nèi)存地址上。
可以使用copy來規(guī)避。
>>> a = np.array([[3,2],[5,9]])
>>> b = a.copy()
>>> a[0][1] = 100
>>> print a
[[ 3 100]
[ 5 9]]
>>> print b
[[3 2]
[5 9]]
指定列
>>> a = np.arange(20).reshape(4,5)
>>> print a
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
>>> print a[:,[1,3]]
[[ 1 3]
[ 6 8]
[11 13]
[16 18]]
按列拼接兩個(gè)向量成一個(gè)矩陣:
>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>> print np.column_stack((a,b))
[[1 2]
[2 3]
[3 4]]
“Python中Numpy庫的詳細(xì)講解”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。