【NumPy基础】100道numpy练习——Apprentice篇

时间:2023-02-13 17:41:24

【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] = 1
Traceback (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.4368492
0.73953192
0.50792598 0.48929711 0.684396 0.2561839 ]
>>> print T
[ 0.87330531 0.42096163 0.71600756 1.13994582 1.5430462
1.04801403
1.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.60977188
0.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
...
-128
127
-2147483648
2147483647
-9223372036854775808
9223372036854775807
>>> for dtype in [np.float32, np.float64]:
... print np.finfo(dtype).min
... print np.finfo(dtype).max
... print np.finfo(dtype).eps
...
-3.40282e+38
3.40282e+38
1.19209e-07
-1.79769313486e+308
1.79769313486e+308
2.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 scipy
import scipy
Z = 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.60279818
0.57375342 0.51979489 0.44822088 0.36787944]
[ 0.44822088 0.54610814 0.63331324 0.69905581 0.73444367
0.73444367
0.69905581 0.63331324 0.54610814 0.44822088]
[ 0.51979489 0.63331324 0.73444367 0.81068432 0.85172308
0.85172308
0.81068432 0.73444367 0.63331324 0.51979489]
[ 0.57375342 0.69905581 0.81068432 0.89483932 0.9401382
0.9401382
0.89483932 0.81068432 0.69905581 0.57375342]
[ 0.60279818 0.73444367 0.85172308 0.9401382 0.98773022
0.98773022
0.9401382 0.85172308 0.73444367 0.60279818]
[ 0.60279818 0.73444367 0.85172308 0.9401382 0.98773022
0.98773022
0.9401382 0.85172308 0.73444367 0.60279818]
[ 0.57375342 0.69905581 0.81068432 0.89483932 0.9401382
0.9401382
0.89483932 0.81068432 0.69905581 0.57375342]
[ 0.51979489 0.63331324 0.73444367 0.81068432 0.85172308
0.85172308
0.81068432 0.73444367 0.63331324 0.51979489]
[ 0.44822088 0.54610814 0.63331324 0.69905581 0.73444367
0.73444367
0.69905581 0.63331324 0.54610814 0.44822088]
[ 0.36787944 0.44822088 0.51979489 0.57375342 0.60279818
0.60279818
0.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])