1、numpy练习题Numpy拓展练习一 1、在python环境中导入numpy包,并命名为np import numpy as np2、查看numpy版本和配置信息 print np._version_ np._config_.show()3、创建零向量,zeros函数 z=np.zeros(2,3) print z 0. 0. 0. 0. 0. 0.4、将上面的零向量的第二行第三列元素置为1。注意python中行列下班是从0开始。z1,2=1 print z 0. 0. 0. 0. 0. 1.5、arange函数,创建一个在给定范围的向量。 z=np.arange(1,101) %1100范围
2、,注意不包括101 print z6、reshape函数,将array变形为矩阵 Z = np.arange(9).reshape(3,3) print Z0 1 2 3 4 5 6 7 87、nonzero函数,寻找非0元素的下标 nz=np.nonzero(1,2,3,0,0,4,0) nz(array(0, 1, 2, 5),)8、eye函数,生成单位向量 z=np.eye(3) print z 1. 0. 0. 0. 1. 0. 0. 0. 1.9、diag函数,diagonal对角线。 z=np.diag(1,2,3,4,k=0) %k=0,以1,2,3,4为对角线 print z1
3、 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 z0 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 z0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 3 0 0 0 0 0 4 010、random模块的random函数,生成随机数 Z = np.random.random(3,3) print Z 0.95171
4、484 0.61394126 0.38864802 0.41943918 0.9398714 0.31608202 0.9993507 0.91717093 0.7300272311、创建一个8*8的“棋盘”矩阵。 z=np.zeros(8,8),dtype=int) z1:2,:2=1 %1、3、5、7行&0、2、4、6列的元素置为1 print z0 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
5、 1 0 z:2,1:2=1 print z0 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 012、min()、max()函数 z=np.random.random(10,10) zmin,zmax=z.min(),z.max() print zmin,zmax0.014230501632 0.9954876029913、函数tile(A,reps),reps即重复的次数,不仅可以是数字,还可
6、以是array。比如构造棋盘矩阵: z=np.tile(np.array(0,1,0,1),(4,4) print z0 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 114、归一化,将矩阵规格化到01,即最小的变成0,最大的变成1,最小与最大之间的等比缩放。 Z = np.random.random(5,5) Zmax,Zmin = Z.max(), Z.min() Z = (Z - Zmin
7、)/(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 15、矩阵点乘 z=np.dot(np.one
8、s(5,3),np.ones(3,2) print z 3. 3. 3. 3. 3. 3. 3. 3. 3. 3.16、矩阵相加,5*5矩阵+1*5的向量,相当于每一行都加上1*5矩阵 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.17、linspace函数,在给定区间中生成均匀分布的给定个数。函数原型 linspace(start, stop, num=50, endpoint=True,retstep=
9、False) Z = np.linspace(0,10,11,endpoint=True, retstep=False) print Z 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.生成010之间均匀分布的11个数,包括0和1。若endpoint=False,则10不包括在里面。若retstep=False,会同时返回均匀区间中每两个数的间隔。18、sort函数。调用random模块中的random函数生成10个随机数,然后sort排序。 Z = np.random.random(10) Z.sort() print Z 0.15978787 0.28050494 0.3
10、5865916 0.40047826 0.45141311 0.4828367 0.66133575 0.66775779 0.69278544 0.9809598919、allclose函数,判断两个array在误差范围内是否相等函数原型allclose(a, b, rtol=1e-05, atol=1e-08),若absolute(a -b) np.nan)# Falseprint(np.nan - np.nan)# nanprint(0.3 = 3 * 0.1)# False (18) Create a 5x5 matrix with values 1,2,3,4 just below
11、the diagonalZ = np.diag(1 + np.arange(4), k=-1)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 (19) Create a 8x8 matrix and fill it with a checkerboard patternZ = np.zeros(8, 8), dtype=int)Z1:2,:2 = 1Z:2,1:2 = 1print(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
12、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 (20) Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?print(np.unravel_index(100, (6, 7, 8)# (1, 5, 4) (21) Create a checkerboard 8x8 matrix using the tile functionZ = np.tile(np.array(0, 1, 1, 0),
13、(4, 4)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 (22) Normalize a 5x5 random matrixZ = np.random.random(5, 5)Zmax, Zmin = Z.max(), Z.min()Z = (Z - Zmin) / (Zmax - Zmin)print(Z) (23) Create a custom
14、dtype that describes a color as four unsigned bytes (RGBA)color = np.dtype(r, np.ubyte, 1), (g, np.ubyte, 1), (b, np.ubyte, 1), (a, np.ubyte, 1)print(color)# dtype(r, u1), (g, u1), (b, u1), (a, u1) (24) Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)Z = np.dot(np.ones(5, 3), np.ones(3, 2
15、)print(Z)# 3. 3.# 3. 3.# 3. 3.# 3. 3.# 3. 3. (25) Given a 1D array, negate all elements which are between 3 and 8, in place.Z = np.arange(11)Z(Z = 3) & (Z = 8) *= -1print(Z)# 0 1 2 -3 -4 -5 -6 -7 -8 9 10 (26) What is the output of the following script?print(sum(range(5), -1)# 9print(np.sum(range(5),
16、 -1)# from numpy import *# print(sum(range(5),-1)# 10, parameter axis=-1 (27) Consider an integer vector Z, which of these expressions are legal?Z = np.array(-1, 2, 3)print(Z * Z)# 1 4 27print(2 2)# 1 2 4print(Z -Z)# False False Falseprint(1j * Z)# complex number# 0.+1.j 0.+2.j 0.+3.jprint(Z / 1 / 1
17、)# 1. 2. 3.print(Z Z)# array(False, False, False, dtype=bool) (28) What are the result of the following expressions?print(np.array(0) / np.array(0)# python 2.7 0# python 3.5 nanprint(np.array(0) / np.array(0)# python 2.7 0# python 3.5 0 print(np.array(np.nan).astype(int).astype(float)# python 2.7 -2
18、.14748365e+09# python 3.5 -2.14748365e+09 (29) How to round away from zero a float array?Z = np.random.uniform(-10, 10, 10)print (np.copysign(np.ceil(np.abs(Z), Z) (30) How to find common values between two arrays?Z1 = np.random.randint(0, 10, 10)Z2 = np.random.randint(0, 10, 10)print(np.intersect1d
19、(Z1, Z2) (31) How to ignore all numpy warnings (not recommended)?# Suicide mode ondefaults = np.seterr(all=ignore)Z = np.ones(1) / 0# Back to sanity_ = np.seterr(*defaults)#An equivalent way, with a context manager:with np.errstate(divide=ignore): Z = np.ones(1) / 0 (32) Is the following expressions
20、 true?np.sqrt(-1) = np.emath.sqrt(-1)# False# left part - nan# right part - 1j (33) How to get the dates of yesterday, today and tomorrow?yesterday = np.datetime64(today, D) - np.timedelta64(1, D)today = np.datetime64(today, D)tomorrow = np.datetime64(today, D) + np.timedelta64(1, D) (34) How to get all the dates corresponding to the month of July 2016?Z = np.arange(2016-07, 2016-08, dtype=datetime64D)print(Z)# 2016-07-01 2016-07-02 2016-07-03 2016-07-04 2016-07-05# 2016-07-06 2016-07-07 2016-07-08 2016-07-09 2016-07-10# 2016-07-11 2016-07-12 2016-07-13 201
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1