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

来源:互联网 发布:mac appstore更换账号 编辑:程序博客网 时间:2024/05/22 10:44

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

@author:wepon

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


选自numpy-100,当作熟悉NumPy的练习。NumPy只是一个数值计算的工具包,在实际的算法实现中来熟悉NumPy才是有效的,因此后面不打算继续写了,到此文为止,基本的语法已经够用了,之后在实践中总结可能效果更好。而且遇到问题参考NumPy官网文档即可。

之前两篇:

【NumPy基础】100道numpy练习——初学与入门篇

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




1、读入文件中的数据,例如一个example.txt,里面的数据如下:

1,2,3,45,,,67,8,,9
读入该文件的代码:
>>> Z = np.genfromtxt("example.txt", delimiter=",")>>> print Z[[ 1. 2. 3. 4.][ 5. nan nan 6.][ 7. 8. nan 9.]]

note:np.genfromtxt()函数第一个参数表示文件路径名,delimiter是分隔符,在我们的exampl.txt中分隔符是逗号“,”,故设置为逗号。顺便提一下,很多数据文件都是以csv后缀格式给出的,csv就是逗号分隔符文件。



2、numpy的高级特性:生成器。利用生成器函数创建数组。

>>> def generate():... for x in xrange(10):... yield x ... >>> Z = np.fromiter(generate(),dtype=float,count=-1)>>> print Z[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]

note:其中的yield关键字使得generate()变成生成器,这是numpy的高级特性。


3、bincount()函数,输出数组中每个数出现的次数。output的下标代表原数组中的数,output的值就是该数出现的次数,举例:

>>> Z=[0,2,4,8,8]>>> np.bincount(Z)array([1, 0, 1, 0, 1, 0, 0, 0, 2])

可以看到8出现了两次。


4、 bincount(x, weights=None, minlength=None),其中weights表示权重,用法如下:

>>> X = [1,2,3,4,5,6]>>> I = [1,3,9,3,4,1]>>> F = np.bincount(I,X)>>> print F[ 0. 7. 0. 6. 5. 0. 0. 0. 0. 3.]

note:以I为输入,X是权重。分析一下:F[0]=0,因为I中没有出现过0,F[1]=7,因为I中1出现了两次,并且它们的权重为1、6,故F[1]=1*1+1*6



5、Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors
# Author: Nadav Horeshw,h = 16,16I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)F = I[...,0]*256*256 + I[...,1]*256 +I[...,2]n = len(np.unique(F))print np.unique(I)




6、一个四维的数组,以后两维度为单位,计算它们的和,比如一个1*2*3*4的数组,则以后面3*4维度为单位,输出1*2的sum,举例:
>>> A = np.random.randint(0,10,(1,2,3,4))>>> print A[[[[2 7 9 7][6 6 8 2][0 0 9 3]][[5 4 1 4][5 7 9 7][8 4 1 4]]]]>>> A.reshape(A.shape[:-2] + (-1,))array([[[2, 7, 9, 7, 6, 6, 8, 2, 0, 0, 9, 3],[5, 4, 1, 4, 5, 7, 9, 7, 8, 4, 1, 4]]])>>> sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)>>> print sum[[59 59]]



7、Considering a one-dimensional vector D, how to compute  means of subsets of D using a vector S of same size describing subset indices ?

>>> D = np.random.uniform(0,1,100)>>> S = np.random.randint(0,10,100)>>> D_sums = np.bincount(S, weights=D)>>> D_counts = np.bincount(S)>>> print D_sums[ 3.37619132 6.13452126 3.84121952 5.27577033 4.459793237.268070496.00231146 6.72050881 4.1281527 6.55666661]>>> print D_counts[ 6 12 10 8 7 13 13 12 7 12]>>> D_means = D_sums / D_counts>>> print D_means[ 0.56269855 0.5112101 0.38412195 0.65947129 0.637113320.559082350.46171627 0.5600424 0.5897361 0.54638888]



8、在数组[1, 2, 3, 4, 5]中相邻两个数字中间插入两个0
>>> Z = np.array([1,2,3,4,5])>>> nz = 3>>> Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))>>> Z0[::nz+1] = Z>>> print Z0[ 1. 0. 0. 0. 2. 0. 0. 0. 3. 0. 0. 0. 4. 0. 0. 0. 5.]


9、二维矩阵与三维矩阵如何相乘?
>>> A = np.ones((3,3,2))>>> B = 2*np.ones((3,3))>>> print A * B[:,:,None][[[ 2. 2.][ 2. 2.][ 2. 2.]][[ 2. 2.][ 2. 2.][ 2. 2.]][[ 2. 2.][ 2. 2.][ 2. 2.]]]>>> B[:,:,None]array([[[ 2.],[ 2.],[ 2.]],[[ 2.],[ 2.],[ 2.]],[[ 2.],[ 2.],[ 2.]]])



10、怎么交换矩阵的其中两行?比如交换第一二行
>>> A = np.arange(25).reshape(5,5)>>> print A[[ 0 1 2 3 4][ 5 6 7 8 9][10 11 12 13 14][15 16 17 18 19][20 21 22 23 24]]>>> A[[0,1]]array([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]])>>> A[[1,0]]array([[5, 6, 7, 8, 9],[0, 1, 2, 3, 4]])>>> A[[0,1]] = A[[1,0]]>>> print A[[ 5 6 7 8 9][ 0 1 2 3 4][10 11 12 13 14][15 16 17 18 19][20 21 22 23 24]]



1 0