【NumPy基礎】100道numpy練習——初學與入門篇,numpy100道

來源:互聯網
上載者:User

【NumPy基礎】100道numpy練習——初學與入門篇,numpy100道

【NumPy基礎】100道numpy練習——初學與入門篇

@author:wepon

@blog:http://blog.csdn.net/u012162613/article/details/42784403


今天在deeplearning.net上看theano tutorial,發現一個numpy-100-exercise,介紹numpy一些基本用法的,不過不是很具體,我利用閑暇時間照著敲了一些,權且當作翻譯吧,增加函數的原型和詳細介紹。持續更新。


一、初學者10道1、在python環境中匯入numpy包,並命名為np
<span style="font-size:18px;">>>> import numpy as np</span>

2、查看numpy版本和配置資訊
<span style="font-size:18px;">>>> print np.__version__>>> np.__config__.show()</span>

3、建立零向量,zeros函數
<span style="font-size:18px;">>>> z=np.zeros((2,3))>>> print z[[ 0.  0.  0.] [ 0.  0.  0.]]</span>

4、將上面的零向量的第二行第三列元素置為1。注意python中行列下班是從0開始。
<span style="font-size:18px;">>>>z[1,2]=1>>> print z[[ 0.  0.  0.] [ 0.  0.  1.]]</span>

5、arange函數,建立一個在給定範圍的向量。
<span style="font-size:18px;">>>> z=np.arange(1,101)    %1~100範圍,注意不包括101>>> print z</span>

6、reshape函數,將array變形為矩陣
<span style="font-size:18px;">>>> Z = np.arange(9).reshape(3,3)>>> print Z[[0 1 2] [3 4 5] [6 7 8]]</span>

7、nonzero函數,尋找非0元素的下標
<span style="font-size:18px;">>>> nz=np.nonzero([1,2,3,0,0,4,0])>>> nz(array([0, 1, 2, 5]),)</span>

8、eye函數,產生單位向量
<span style="font-size:18px;">>>> z=np.eye(3)>>> print z[[ 1.  0.  0.] [ 0.  1.  0.] [ 0.  0.  1.]]</span>

9、diag函數,diagonal對角線。
<span style="font-size:18px;">>>> z=np.diag([1,2,3,4],k=0)   %k=0,以[1,2,3,4]為對角線>>> print z[[1 0 0 0] [0 2 0 0] [0 0 3 0] [0 0 0 4]]>>> z=np.diag([1,2,3,4],k=1)   %k=1,[1,2,3,4]在對角線上一行>>> print z[[0 1 0 0 0] [0 0 2 0 0] [0 0 0 3 0] [0 0 0 0 4] [0 0 0 0 0]]>>> z=np.diag([1,2,3,4],k=-1)  %k=-1,[1,2,3,4]在對角線下一行>>> print z[[0 0 0 0 0] [1 0 0 0 0] [0 2 0 0 0] [0 0 3 0 0] [0 0 0 4 0]]</span>

10、random模組的random函數,產生隨機數
<span style="font-size:18px;">>>> Z = np.random.random((3,3))>>> print Z[[ 0.95171484  0.61394126  0.38864802] [ 0.41943918  0.9398714   0.31608202] [ 0.9993507   0.91717093  0.73002723]]</span>



二、入門級10道1、建立一個8*8的“棋盤”矩陣。
<span style="font-size:18px;">>>> z=np.zeros((8,8),dtype=int)>>> z[1::2,::2]=1     %1、3、5、7行&&0、2、4、6列的元素置為1>>> print z[[0 0 0 0 0 0 0 0] [1 0 1 0 1 0 1 0] [0 0 0 0 0 0 0 0] [1 0 1 0 1 0 1 0] [0 0 0 0 0 0 0 0] [1 0 1 0 1 0 1 0] [0 0 0 0 0 0 0 0] [1 0 1 0 1 0 1 0]]>>> z[::2,1::2]=1>>> print z[[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]</span>

2、min()、max()函數
<span style="font-size:18px;">>>> z=np.random.random((10,10))>>> zmin,zmax=z.min(),z.max()>>> print zmin,zmax0.014230501632 0.99548760299</span>

3、函數tile(A,reps),reps即重複的次數,不僅可以是數字,還可以是array。比如構造棋盤矩陣:
<span style="font-size:18px;">>>> z=np.tile(np.array([[0,1],[0,1]]),(4,4))>>> print z[[0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1]]</span>

4、歸一化,將矩陣規格化到0~1,即最小的變成0,最大的變成1,最小與最大之間的等比縮放。

<span style="font-size:18px;">>>> Z = np.random.random((5,5))>>> Zmax,Zmin = Z.max(), Z.min()>>> Z = (Z - Zmin)/(Zmax - Zmin)>>> print Z[[ 0.          0.32173291  0.17607851  0.6270374   0.95000808] [ 0.49153473  0.70465605  0.61930085  0.00303294  1.        ] [ 0.4680561   0.88742782  0.29899683  0.80704789  0.12300414] [ 0.05094248  0.23065875  0.82776775  0.07873239  0.50644422] [ 0.27417053  0.78679222  0.517819    0.5649124   0.4716856 ]]</span>

5、矩陣點乘
<span style="font-size:18px;">>>> z=np.dot(np.ones((5,3)),np.ones((3,2)))>>> print z[[ 3.  3.] [ 3.  3.] [ 3.  3.] [ 3.  3.] [ 3.  3.]]</span>

6、矩陣相加,5*5矩陣+1*5的向量,相當於每一行都加上1*5矩陣
<span style="font-size:18px;">>>> Z = np.zeros((5,5))>>> Z += np.arange(5)>>> print Z[[ 0.  1.  2.  3.  4.] [ 0.  1.  2.  3.  4.] [ 0.  1.  2.  3.  4.] [ 0.  1.  2.  3.  4.] [ 0.  1.  2.  3.  4.]]</span>


7、linspace函數,在給定區間中產生均勻分布的給定個數。
函數原型 linspace(start, stop, num=50, endpoint=True, retstep=False)
<span style="font-size:18px;">>>> Z = np.linspace(0,10,11,endpoint=True, retstep=False)>>> print Z[  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.  10.]</span>

產生0~10之間均勻分布的11個數,包括0和1。
若endpoint=False,則10不包括在裡面。
若retstep=False,會同時返回均勻區間中每兩個數的間隔。
8、sort函數。調用random模組中的random函數產生10個隨機數,然後sort排序。

<span style="font-size:18px;">>>> Z = np.random.random(10)>>> Z.sort()>>> print Z[ 0.15978787  0.28050494  0.35865916  0.40047826  0.45141311  0.4828367  0.66133575  0.66775779  0.69278544  0.98095989]</span>

9、allclose函數,判斷兩個array在誤差範圍內是否相等
函數原型allclose(a, b, rtol=1e-05, atol=1e-08),若absolute(a - b) <= (atol + rtol * absolute(b))則相等。
<span style="font-size:18px;">A = np.random.randint(0,2,5)B = np.random.randint(0,2,5)equal = np.allclose(A,B)print equal</span>

10、mean函數,求平均值
<span style="font-size:18px;">>>> Z = np.random.random(30)>>> m = Z.mean()>>> print m0.362299527973>>> A = np.random.randint(0,2,5)>>> B = np.random.randint(0,2,5)>>> equal = np.allclose(A,B)>>> print equalFalse</span>

註:randint(min,max,num)產生大小為num的array,數值範圍min~max


----------------------------2015/1/15-------------------------------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.