標籤:numpy python
【NumPy基礎】100道numpy練習——Apprentice篇
@author:wepon
@blog:http://blog.csdn.net/u012162613/article/details/42811297
今天又用半小時掃了一下Apprentice篇裡的10道exercise,不知道怎麼翻譯Apprentice(學徒~~)這個詞,就直接以Apprentice篇作為題目了。numpy文法直白如水啊,花這些時間exercise有點浪費了.......Anyway,為了後面更熟練地用一些ML的包,利用閑暇時間掃一掃基本文法。原地址:https://github.com/rougier/numpy-100
1、將array屬性設定為唯讀(read-only)
>>> Z = np.zeros(10)>>> Z.flags.writeable = False>>> Z[0] = 1Traceback (most recent call last):File "<stdin>", line 1, in <module>ValueError: assignment destination is read-only
2、將笛卡爾座標系下的點(x,y)轉化為極座標系的(R,T)
>>> Z = np.random.random((10,2))>>> print Z[[ 0.49094922 0.58585236][ 0.32265092 0.14445935][ 0.8078448 0.70284832][ 0.35341989 0.76888775][ 0.01212107 0.43668101][ 0.36924292 0.64075512][ 0.22349212 0.4561141 ][ 0.44836271 0.19591462][ 0.6639701 0.16595659][ 0.25559111 0.01741761]]>>> X,Y = Z[:,0], Z[:,1]>>> R = np.sqrt(X**2+Y**2)>>> T = np.arctan2(Y,X) %求反正切值>>> print R[ 0.76436518 0.35351396 1.07079829 0.84622337 0.43684920.739531920.50792598 0.48929711 0.684396 0.2561839 ]>>> print T[ 0.87330531 0.42096163 0.71600756 1.13994582 1.54304621.048014031.11518737 0.41195346 0.24492773 0.06804118]
3、擷取數組中最大值/最小值對應的下標,argmax()、argmin()函數
>>> Z = np.random.random(10)>>> print Z[ 0.90315764 0.06574957 0.86297424 0.46519603 0.01174512 0.609771880.52107975 0.6328012 0.12344019 0.05034712]>>> Z.argmax()0>>> Z.argmin()4
4、meshgrid()產生格點矩陣,用於三維曲面的分格線座標
>>> Z = np.zeros((10,10), [('x',float),('y',float)])>>> Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,10), np.linspace(0,1,10))>>> print Z
5、各種資料類型的最大值/最小值,主要有8位、32位、64位整型,32位、64位浮點型
>>> for dtype in [np.int8, np.int32, np.int64]:... print np.iinfo(dtype).min... print np.iinfo(dtype).max... -128127-21474836482147483647-92233720368547758089223372036854775807>>> for dtype in [np.float32, np.float64]:... print np.finfo(dtype).min... print np.finfo(dtype).max... print np.finfo(dtype).eps... -3.40282e+383.40282e+381.19209e-07-1.79769313486e+3081.79769313486e+3082.22044604925e-16
6、產生一個矩陣,矩陣中每個元素由座標(x,y)以及顏色(r,g,b)組成
>>> Z = np.zeros(10, [ ('position', [ ('x', float, 1),... ('y', float, 1)]),... ('color', [ ('r', float, 1),... ('g', float, 1),... ('b', float, 1)])])>>> print Z[((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0))((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0))((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0))((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0))((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0))]
7、產生10個座標(x,y),計算兩兩之間的距離
>>> Z = np.random.random((10,2))>>> X,Y = np.atleast_2d(Z[:,0]), np.atleast_2d(Z[:,1])>>> D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)>>> print D %D是10*10,對角線的值都是0
也可以直接用scipy裡的cdist函數
# Much faster with scipyimport scipyZ = np.random.random((10,2))D = scipy.spatial.distance.cdist(Z,Z)print D
8、產生二維的高斯矩陣
>>> X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(- 1,1,10))>>> D = np.sqrt(X*X+Y*Y)>>> sigma, mu = 1.0, 0.0 %方差=1,均值=0>>> G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )>>> print G[[ 0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.602798180.57375342 0.51979489 0.44822088 0.36787944][ 0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.734443670.69905581 0.63331324 0.54610814 0.44822088][ 0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.851723080.81068432 0.73444367 0.63331324 0.51979489][ 0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.94013820.89483932 0.81068432 0.69905581 0.57375342][ 0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.987730220.9401382 0.85172308 0.73444367 0.60279818][ 0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.987730220.9401382 0.85172308 0.73444367 0.60279818][ 0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.94013820.89483932 0.81068432 0.69905581 0.57375342][ 0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.851723080.81068432 0.73444367 0.63331324 0.51979489][ 0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.734443670.69905581 0.63331324 0.54610814 0.44822088][ 0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.602798180.57375342 0.51979489 0.44822088 0.36787944]]
9、any函數,判斷矩陣中的元素是否滿足給定的條件,是的話返回True,否則返回False。例子:判斷二維矩陣中有沒有一整列數為0?
>>> Z = np.random.randint(0,3,(2,10))>>> print Z[[1 0 1 2 2 0 1 0 1 0][0 0 1 2 2 0 1 0 0 1]]>>> print Z.any(axis=0) %按列判斷,全0則為False[ True False True True True False True False True True]>>> print (~Z.any(axis=0)).any()True
10、找出數組中與給定值最接近的數
>>> Z=array([[0,1,2,3],[4,5,6,7]])>>> print Z[[0 1 2 3][4 5 6 7]]>>> z=5.1>>> np.abs(Z - z).argmin()5>>> print Z.flat[np.abs(Z - z).argmin()]5
補充介紹:
flat是一個一維迭代器,將Z當成一維向量去遍曆,Z[5]將超出原來矩陣的邊界,Z.flat[5]才是正確的用法。
另外,不得不提flatten,它將矩陣攤平為一維向量:
>>> Z.flatten()array([0, 1, 2, 3, 4, 5, 6, 7])
【NumPy基礎】100道numpy練習——Apprentice篇