Numpy100
やってみた
雰囲気はつかめた
Craftsmanあたりからよくわからない
>>> import numpy as np >>> print np.__version__ 1.9.0 >>> np.__config__.show()
Neophyte
初学者
null vector
>>> Z = np.zeros(10) >>> print Z [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] >>> Z[4] = 1 >>> print Z [ 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
range
>>> Z = np.arange(10,50) >>> print Z [10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]
matrix
>>> Z = np.arange(9).reshape(3,3) >>> print Z [[0 1 2] [3 4 5] [6 7 8]]
indices of non-zero
>>> nz = np.nonzero([1,2,0,0,4,0]) >>> print nz (array([0, 1, 4]),)
identity matrix
>>> Z = np.eye(3) >>> print Z [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]]
diagonal 対角
>>> Z = 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]]
random values
>>> Z = np.random.random((3,3,3)) >>> print Z [[[ 0.30936979 0.79015259 0.05904413] [ 0.61099424 0.61819796 0.46896755] [ 0.60953166 0.32139445 0.39996169]] [[ 0.87130793 0.64048757 0.89079441] [ 0.7869118 0.53365886 0.87841675] [ 0.90432816 0.90662224 0.51572711]] [[ 0.79899535 0.54259759 0.3755051 ] [ 0.59657526 0.91693672 0.27771755] [ 0.36829915 0.74820769 0.36179704]]]
Novice
checkerboard pattern
>>> Z = np.zeros((8,8),dtype=int) >>> Z[1::2,::2] = 1 >>> Z[::2,1::2] = 1 >>> 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]]
minimum / maximum values
>>> Z = np.random.random((10,10)) >>> Zmin, Zmax = Z.min(), Z.max() >>> print Zmin, Zmax 0.00425612310069 0.997602814595
tile function
>>> Z = np.tile( np.array([[0,1],[1,0]]), (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]]
Normalize 標準化
>>> Z = np.random.random((5,5)) >>> Zmax,Zmin = Z.max(), Z.min() >>> Z = (Z - Zmin)/(Zmax - Zmin) >>> print Z [[ 0.94259636 0.08234796 0. 0.63882964 0.69767425] [ 0.83754753 0.07655042 0.11640155 0.51882497 0.45422194] [ 0.27553212 0.99792494 0.37865626 0.55906933 0.6912325 ] [ 0.90030913 0.14966885 0.30505372 0.37294772 1. ] [ 0.19624698 0.37071625 0.19395725 0.34983129 0.74560749]]
real matrix product 積
>>> Z = np.dot(np.ones((5,3)), np.ones((3,2))) >>> print Z [[ 3. 3.] [ 3. 3.] [ 3. 3.] [ 3. 3.] [ 3. 3.]]
row values + range
>>> 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.]]
linspace
0〜1の12分割点から両端を除く
>>> Z = np.linspace(0,1,12,endpoint=True)[1:-1] >>> print Z [ 0.09090909 0.18181818 0.27272727 0.36363636 0.45454545 0.54545455 0.63636364 0.72727273 0.81818182 0.90909091]
sort
>>> Z = np.random.random(10) >>> Z.sort() >>> print Z [ 0.05225772 0.18059796 0.46729663 0.46848719 0.54317599 0.62262125 0.68476129 0.74545976 0.81606937 0.878892 ]
allclose
arrayのequal
>>> A = np.random.randint(0,2,5) >>> B = np.random.randint(0,2,5) >>> equal = np.allclose(A,B) >>> print equal False
mean 平均
>>> Z = np.random.random(30) >>> m = Z.mean() >>> print m 0.50206126416
Apprentice
an array immutable (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
極座標系 Polar coordinates
>>> Z = np.random.random((10,2)) >>> X,Y = Z[:,0], Z[:,1] >>> R = np.sqrt(X**2+Y**2) >>> T = np.arctan2(Y,X) >>> print R [ 0.56903224 0.99850157 0.20784923 0.73735091 0.77963375 0.68499796 1.06005442 0.25116638 0.87157151 0.57301377] >>> print T [ 1.06056848 0.28220172 0.97748396 0.22205002 1.14486119 0.17453188 0.4603034 1.33204072 0.91704211 1.0644907 ]
argmax
maximum value
replace the maximum value by 0
>>> Z = np.random.random(10) >>> Z[Z.argmax()] = 0 >>> print Z [ 0.7949367 0.01687779 0.21086095 0.65115652 0. 0.56356753 0.16321282 0.42611198 0.75543268 0.05336931]
structured array
>>> 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 [[(0.0, 0.0) (0.1111111111111111, 0.0) (0.2222222222222222, 0.0) (0.3333333333333333, 0.0) (0.4444444444444444, 0.0) (0.5555555555555556, 0.0) (0.6666666666666666, 0.0) (0.7777777777777777, 0.0) (0.8888888888888888, 0.0) (1.0, 0.0)] [(0.0, 0.1111111111111111) (0.1111111111111111, 0.1111111111111111) (0.2222222222222222, 0.1111111111111111) ...
minimum and maximum (int, float)
>>> 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
structured array representing a position (x,y) and a color (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))]
find point by point distances
>>> 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 [[ 0. 0.1961759 0.34524241 0.74028341 0.56442739 0.33981635 0.65219462 0.6636379 0.2854357 0.38098371] [ 0.1961759 0. 0.19068074 0.92374084 0.67696428 0.38104286 0.74675358 0.7694505 0.26552149 0.51873143] [ 0.34524241 0.19068074 0. 0.99892021 0.66567124 0.34162307 0.71135303 0.74456985 0.44874128 0.55046626] [ 0.74028341 0.92374084 0.99892021 0. 0.47109242 0.70385989 0.54138628 0.49638037 0.97509519 0.46262763] [ 0.56442739 0.67696428 0.66567124 0.47109242 0. 0.32423594 0.10646925 0.10039469 0.84983476 0.19364062] [ 0.33981635 0.38104286 0.34162307 0.70385989 0.32423594 0. 0.37417645 0.40428067 0.60414345 0.24158526] [ 0.65219462 0.74675358 0.71135303 0.54138628 0.10646925 0.37417645 0. 0.0501653 0.93696147 0.29496916] [ 0.6636379 0.7694505 0.74456985 0.49638037 0.10039469 0.40428067 0.0501653 0. 0.94906748 0.29371268] [ 0.2854357 0.26552149 0.44874128 0.97509519 0.84983476 0.60414345 0.93696147 0.94906748 0. 0.66455735] [ 0.38098371 0.51873143 0.55046626 0.46262763 0.19364062 0.24158526 0.29496916 0.29371268 0.66455735 0. ]]
Much faster with scipy (find point by point distances)
>>> import scipy >>> Z = np.random.random((10,2)) >>> D = scipy.spatial.distance.cdist(Z,Z) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'spatial' >>> import scipy.spatial >>> D = scipy.spatial.distance.cdist(Z,Z) >>> print D [[ 0. 0.8710603 0.59660442 1.06289049 0.93339079 0.68938218 0.9041566 0.82294367 0.81307238 0.73440283] [ 0.8710603 0. 0.4815468 0.25983479 0.41255784 0.52337121 0.26962081 0.08480006 0.06070816 0.19478326] [ 0.59660442 0.4815468 0. 0.55219119 0.34491199 0.73041213 0.35920009 0.49465788 0.42691021 0.49209316] [ 1.06289049 0.25983479 0.55219119 0. 0.30116753 0.78269121 0.19769189 0.34437163 0.29115894 0.45373442] [ 0.93339079 0.41255784 0.34491199 0.30116753 0. 0.87381499 0.14393449 0.47936796 0.39466007 0.55025252] [ 0.68938218 0.52337121 0.73041213 0.78269121 0.87381499 0. 0.7514873 0.43858071 0.50473353 0.33489962] [ 0.9041566 0.26962081 0.35920009 0.19769189 0.14393449 0.7514873 0. 0.34003414 0.25708539 0.42036535] [ 0.82294367 0.08480006 0.49465788 0.34437163 0.47936796 0.43858071 0.34003414 0. 0.08548165 0.11344352] [ 0.81307238 0.06070816 0.42691021 0.29115894 0.39466007 0.50473353 0.25708539 0.08548165 0. 0.16984671] [ 0.73440283 0.19478326 0.49209316 0.45373442 0.55025252 0.33489962 0.42036535 0.11344352 0.16984671 0. ]]
generic 2D Gaussian-like array
>>> 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 >>> 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]]
tell if a given 2D array has null columns
>>> Z = np.random.randint(0,3,(3,10)) >>> print (~Z.any(axis=0)).any() False
Find the nearest value
>>> Z = np.random.uniform(0,1,10) >>> z = 0.5 >>> m = Z.flat[np.abs(Z - z).argmin()] >>> print m 0.459228518938
Journeyman
read txt(csv) file
>>> Z = np.genfromtxt("missing.dat", delimiter=",") >>> print Z [[ 1. 2. 3. 4. 5.] [ 6. nan nan 7. 8.] [ nan nan 9. 10. 11.]]
generator function
>>> 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.]
add 1 to each element indexed by a second vector
>>> Z = np.ones(10) >>> I = np.random.randint(0,len(Z),20) >>> Z += np.bincount(I, minlength=len(Z)) >>> print Z [ 1. 1. 5. 1. 3. 5. 3. 3. 3. 5.] >>> print I [7 8 2 5 2 6 5 9 7 4 9 8 5 6 5 4 2 9 2 9]
accumulate elements of a vector
>>> 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.]
compute the number of unique colors
>>> 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) [0 1]
get sum of a four dimensions array
>>> A = np.random.randint(0,10,(3,4,3,4)) >>> sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1) >>> print sum [[52 46 67 43] [65 37 61 48] [56 62 66 70]] >>> print A [[[[6 5 5 9] [4 2 2 2] [7 0 8 2]] [[3 8 6 1] [4 1 3 9] [1 1 9 0]] ...
compute means of subsets
>>> 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) >>> D_means = D_sums / D_counts >>> print D_means [ 0.36137433 0.53391941 0.49315385 0.75660738 0.35519685 0.48302458 0.4141889 0.51082301 0.48832815 0.47498762]
build a new vector with 3 consecutive zeros interleaved
>>> 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.]
mulitply it by an array with dimensions (5,5)
>>> A = np.ones((5,5,3)) >>> B = 2*np.ones((5,5)) >>> print A * B[:,:,None] [[[ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.] [ 2. 2. 2.]] ... x5
swap two rows of an array
>>> 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]] = 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]]
Craftsman
職人
stride_tricks
>>> from numpy.lib import stride_tricks >>> def rolling(a, window): ... shape = (a.size - window + 1, window) ... strides = (a.itemsize, a.itemsize) ... return stride_tricks.as_strided(a, shape=shape, strides=strides) ... >>> Z = rolling(np.arange(10), 3) >>> print Z [[0 1 2] [1 2 3] [2 3 4] [3 4 5] [4 5 6] [5 6 7] [6 7 8] [7 8 9]]
set of unique line segments composing all the triangles
>>> faces = np.random.randint(0,100,(10,3)) >>> F = np.roll(faces.repeat(2,axis=1),-1,axis=1) >>> F = F.reshape(len(F)*3,2) >>> F = np.sort(F,axis=1) >>> G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] ) >>> G = np.unique(G) >>> print G [(5, 55) (5, 80) (11, 15) (11, 59) (14, 17) (14, 71) (15, 59) (16, 17) (16, 18) (17, 18) (17, 71) (19, 28) (19, 49) (22, 43) (22, 56) (27, 38) (27, 99) (28, 49) (32, 71) (32, 91) (36, 68) (36, 87) (38, 99) (43, 56) (44, 60) (44, 67) (55, 80) (60, 67) (68, 87) (71, 91)]
np.bincount(A) == C
>>> C = np.bincount([1,1,2,3,4,4,6]) >>> A = np.repeat(np.arange(len(C)), C) >>> print A [1 1 2 3 4 4 6]
sliding window , moving average
>>> def moving_average(a, n=3) : ... ret = np.cumsum(a, dtype=float) ... ret[n:] = ret[n:] - ret[:-n] ... return ret[n - 1:] / n ... >>> Z = np.arange(20) >>> print moving_average(Z, n=3) [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.]
Artisan
extract rows with unequal values
>>> Z = np.random.randint(0,5,(10,3)) >>> E = np.logical_and.reduce(Z[:,1:] == Z[:,:-1], axis=1) >>> U = Z[~E] >>> print Z [[2 1 0] [0 0 3] [3 0 2] [4 0 0] [4 2 2] [3 2 1] [2 0 0] [3 1 4] [1 2 1] [1 1 0]] >>> print U [[2 1 0] [0 0 3] [3 0 2] [4 0 0] [4 2 2] [3 2 1] [2 0 0] [3 1 4] [1 2 1] [1 1 0]]
matrix binary representation
>>> I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128]) >>> B = ((I.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int) >>> print B[:,::-1] [[0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 1] [0 0 0 0 0 0 1 0] [0 0 0 0 0 0 1 1] [0 0 0 0 1 1 1 1] [0 0 0 1 0 0 0 0] [0 0 1 0 0 0 0 0] [0 1 0 0 0 0 0 0] [1 0 0 0 0 0 0 0]] >>> I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128], dtype=np.uint8) >>> print np.unpackbits(I[:, np.newaxis], axis=1) [[0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 1] [0 0 0 0 0 0 1 0] [0 0 0 0 0 0 1 1] [0 0 0 0 1 1 1 1] [0 0 0 1 0 0 0 0] [0 0 1 0 0 0 0 0] [0 1 0 0 0 0 0 0] [1 0 0 0 0 0 0 0]]
compute distance from p to each line
>>> def distance(P0, P1, p): ... T = P1 - P0 ... L = (T**2).sum(axis=1) ... U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L ... U = U.reshape(len(U),1) ... D = P0 + U*T - p ... return np.sqrt((D**2).sum(axis=1)) ... >>> P0 = np.random.uniform(-10,10,(10,2)) >>> P1 = np.random.uniform(-10,10,(10,2)) >>> p = np.random.uniform(-10,10,( 1,2)) >>> print distance(P0, P1, p) [ 3.14695646 0.9487721 3.20734219 0.91364278 3.61200668 9.62651959 0.89570714 5.69892643 6.28151472 2.6838738 ]
Adept
達人、名人
部分を切り出して、上下左右にシフトして、余った部分は0埋めする
>>> Z = np.random.randint(0,10,(10,10)) >>> shape = (5,5) >>> fill = 0 >>> position = (1,1) >>> R = np.ones(shape, dtype=Z.dtype)*fill >>> P = np.array(list(position)).astype(int) >>> Rs = np.array(list(R.shape)).astype(int) >>> Zs = np.array(list(Z.shape)).astype(int) >>> R_start = np.zeros((len(shape),)).astype(int) >>> R_stop = np.array(list(shape)).astype(int) >>> Z_start = (P-Rs//2) >>> Z_stop = (P+Rs//2)+Rs%2 >>> R_start = (R_start - np.minimum(Z_start,0)).tolist() >>> Z_start = (np.maximum(Z_start,0)).tolist() >>> R_stop = np.maximum(R_start, (R_stop - np.maximum(Z_stop-Zs,0))).tolist() >>> Z_stop = (np.minimum(Z_stop,Zs)).tolist() >>> r = [slice(start,stop) for start,stop in zip(R_start,R_stop)] >>> z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)] >>> R[r] = Z[z] >>> print Z [[6 0 4 5 1 7 5 8 8 4] [7 8 4 5 3 5 1 5 9 0] [3 4 4 6 1 6 2 6 2 4] [8 0 5 7 1 8 2 0 5 2] [8 4 4 1 6 7 3 0 4 7] [1 6 1 5 6 3 4 3 1 8] [1 4 5 4 3 8 1 4 6 7] [6 3 6 3 8 3 1 1 3 2] [8 8 7 6 6 9 9 8 1 3] [5 2 2 7 2 7 1 0 0 1]] >>> print R [[0 0 0 0 0] [0 6 0 4 5] [0 7 8 4 5] [0 3 4 4 6] [0 8 0 5 7]]
配列から、行列のようなものを生成
>>> Z = np.arange(1,15,dtype=np.uint32) >>> R = stride_tricks.as_strided(Z,(11,4),(4,4)) >>> print R [[ 1 2 3 4] [ 2 3 4 5] [ 3 4 5 6] [ 4 5 6 7] [ 5 6 7 8] [ 6 7 8 9] [ 7 8 9 10] [ 8 9 10 11] [ 9 10 11 12] [10 11 12 13] [11 12 13 14]]
Expert
専門家
含まれるものを見つけるみたいなものだが、意味がわからない
>>> A = np.random.randint(0,5,(8,3)) >>> B = np.random.randint(0,5,(2,2)) >>> C = (A[..., np.newaxis, np.newaxis] == B) >>> rows = (C.sum(axis=(1,2,3)) >= B.shape[1]).nonzero()[0] >>> print rows [2 3 4 6 7] >>> A array([[3, 0, 3], [2, 3, 3], [0, 4, 3], [4, 3, 0], [4, 0, 2], [3, 3, 2], [4, 1, 3], [4, 4, 2]]) >>> B array([[4, 0], [1, 2]])
10x10から3x3の部分を抜き出す
>>> Z = np.random.randint(0,5,(10,10)) >>> n = 3 >>> i = 1 + (Z.shape[0]-3) >>> j = 1 + (Z.shape[1]-3) >>> C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides) >>> print C [[[[2 1 0] [3 2 4] [3 2 4]] [[1 0 1] [2 4 0] [2 4 1]] ...
対角に対象な2次元配列
>>> class Symetric(np.ndarray): ... def __setitem__(self, (i,j), value): ... super(Symetric, self).__setitem__((i,j), value) ... super(Symetric, self).__setitem__((j,i), value) ... >>> def symetric(Z): ... return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric) ... >>> S = symetric(np.random.randint(0,10,(5,5))) >>> S[2,3] = 42 >>> print S [[ 8 10 15 12 9] [10 9 5 8 8] [15 5 6 42 10] [12 8 42 9 11] [ 9 8 10 11 3]]
マトリクス積
>>> p, n = 10, 20 >>> M = np.ones((p,n,n)) >>> V = np.ones((p,n,1)) >>> S = np.tensordot(M, V, axes=[[0, 2], [0, 1]]) >>> print S [[ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.] [ 200.]]
Master
行uniq
>>> Z = np.random.randint(0,2,(20,3)) >>> T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1]))) >>> _, idx = np.unique(T, return_index=True) >>> uZ = Z[idx] >>> print uZ [[0 0 0] [0 0 1] [0 1 0] [0 1 1] [1 0 1] [1 1 0] [1 1 1]]