【NumPy基础】100道numpy练习——进阶篇
@author:wepon
@blog:http://blog.csdn.net/u012162613/article/details/42846777
选自numpy-100,当作熟悉NumPy的练习。NumPy只是一个数值计算的工具包,在实际的算法实现中来熟悉NumPy才是有效的,因此后面不打算继续写了,到此文为止,基本的语法已经够用了,之后在实践中总结可能效果更好。而且遇到问题参考NumPy官网文档即可。
之前两篇:
【NumPy基础】100道numpy练习——Apprentice篇
1、读入文件中的数据,例如一个example.txt,里面的数据如下:
1,2,3,4 5,,,6 7,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
# Author: Nadav Horesh w,h = 16,16 I = 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)
>>> 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.45979323 7.26807049 6.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.63711332 0.55908235 0.46171627 0.5600424 0.5897361 0.54638888]
>>> 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.]
>>> 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.]]])
>>> 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]]
原文地址:http://blog.csdn.net/u012162613/article/details/42847011