ãã¼ã¿åæã¬ãåå¼·ã¢ããã³ãã«ã¬ã³ãã¼2æ¥ç®ã æ°å¼ãæ±ããã¨ãå¤ããªãã®ã§ãnumpyã®å¾©ç¿ããããã¨æãã使ã£ãã®ã¯100 numpy exercise
numpyãç¨ãããã¾ãã¾ãªåé¡ãç¨æããã¦ãã¦ã大å¤åå¼·ã«ãªãã ä»åã¯èªåã®å®åã試ãããã«ãèªåã§è§£ãã¤ã¤ããã使ã£ããã®/æ°ããå¦ãã ãã¨ãåæãã¦ããã
ã¾ããåé¡ã®å訳ã¨é£æ度ãæ²è¼ãã¦ããã®ã§ãèªåã®å®åã試ããã人ã¯ã©ããã èªåãæ¸ããã³ã¼ãã¨ããã¡ãã«è¼ãã
2æ¥ç®ã¯ãåç´ã»ä¸ç´ãæ²è¼ãããä¸ç´ã¯ææ¥ææ¦ãã¦æ²è¼äºå®ã
- â ââ : åç´
- â â â : ä¸ç´
- â â â : ä¸ç´
- åé¡
- çµæ
- numpyé åã®åºæ¬
- åå¼·ã«ãªã£ãåé¡(ä¸é¨æç²)
- 24. 5Ã3ã®è¡åã¨3Ã2ã®è¡åãæãåããã(å®æ°)
- 25. 1次å é åã§ã3以ä¸8以ä¸ã®å¤ã¯è² å¤ã«ãã
- 36. ä¹±æ°è¡åã®æ´æ°é¨åã ãåãåºããã¤ã5種é¡
- 41. å°ããè¡åãnp.sum()ããæ©ãè¨ç®ããæ¹æ³ã¯ï¼
- 43. é åãimmutable(read-only)ã«ãã
- 49. é åã®å ¨ã¦ã®å¤ãçç¥ããã«ããªã³ããã
- 55. numpyã®é åã®enumerateã¨åçã®è¡¨ç¾ã¯ï¼
- ã¾ã¨ã
åé¡
以ä¸ã«ãåç´ä¸ç´ã®åé¡ãæ²è¼ãããé·ãã®ã§ããã¿ã³ãæ¼ãã¨è¦ãããããã«ãã¦ããã
- â : 模ç¯è§£çã©ãã
- â³ : ãã£ã¨å¹ççãªããæ¹ããã£ã
- â : ããããªãã£ã
çªå· | åé¡ | é£æ度 | çµæ |
---|---|---|---|
1 | numpyã`np`ã¨ããååã§import | â ââ | â |
2 | numpyã®ãã¼ã¸ã§ã³ã¨configã表示 | â ââ | â |
3 | ãµã¤ãº10ã®nullãã¯ãã«ãä½æ | â ââ | â |
4 | ä½ã£ãé åã®ã¡ã¢ãªãµã¤ãºãç¢ºèª | â ââ | â |
5 | ã³ãã³ãã©ã¤ã³ããnumpyã®ããã¥ã¡ã³ããè¦ã | â ââ | â |
6 | ãµã¤ãºã10ã§5çªç®ã®å¤ã ã1ã®é åãä½ã | â ââ | â |
7 | å¤ã10ãã49ã¾ã§é ã«ä¸¦ãã é åãä½ã | â ââ | â |
8 | é åã®å¤ãã²ã£ããè¿ã | â ââ | â |
9 | ãããã0~8ã¾ã§å ¥ã£ã3Ã3ã®è¡åãä½ã | â ââ | â |
10 | [1,2,0,0,4,0]ãã0ãããªãç®æãè¦ã¤ãã | â ââ | â³ |
11 | 3Ã3ã®åä½è¡åãã¤ãã | â ââ | â³ |
12 | 3Ã3Ã3ã®ä¹±æ°è¡åãä½ã | â ââ | â |
13 | 10Ã10ã®ä¹±æ°è¡åãä½ã£ã¦ãæ大å¤ã¨æå°å¤ãè¦ã¤ãã | â ââ | â |
14 | ãµã¤ãº30ã®ä¹±æ°ãã¯ãã«ãä½ã£ã¦å¹³åå¤ãåºã | â ââ | â |
15 | å¢çã1ã§ãä¸èº«ã0ã®2次å é åãã¤ãã | â ââ | â |
16 | ä»ããé åã«ã0ã§åããå¢çãä½ã | â ââ | â |
17 | np.nanã¨np.infãå°æ°ã®æ±ã | â ââ | â |
18 | 5Ã5ã®è¡åãä½ã£ã¦ã対è§æåã®ä¸ã¤ä¸ã ã1,2,3,4ã§åãã | â ââ | â³ |
19 | 8Ã8ã®è¡åãä½ãããã§ãã«ã¼ãã¼ããã¿ã¼ã³ãä½ã | â ââ | â |
20 | (6,7,8)ã®é åã§ãã¤ã³ããã¯ã¹(x,y,z)ã«ããã100çªç®ã®è¦ç´ ã¯ï¼ | â ââ | â |
21 | ile functionã使ã£ã¦8Ã8ã®ãã§ãã«ã¼ãã¼ããã¿ã¼ã³ãä½ã | â ââ | â |
22 | 5Ã5ã®ä¹±æ°è¡åãæ£è¦åãã | â ââ | â |
23 | 8ãããæ´æ°ã使ã£ã¦è²ãè¨è¿°ããdtypeãä½ã | â ââ | â |
24 | 5Ã3ã®è¡åã¨3Ã2ã®è¡åãæãåããã(å®æ°) | â ââ | â |
25 | 1次å é åã§ã3以ä¸8以ä¸ã®å¤ã¯è² å¤ã«ãã | â ââ | â |
26 | numpyæç¡ã§ã®sum()ã®çµæã¯ãããï¼ | â ââ | â |
27 | æ´æ°ãã¯ãã«ã®ãªãã§ã©ã®è¨è¿°ãééã£ã¦ããï¼ | â ââ | â |
28 | é åã®ã¼ãé¤ç® | â ââ | â |
29 | é åã®å°æ°ç¹ä»¥ä¸åãä¸ã | â ââ | â³ |
30 | 2ã¤ã®é åã®ä¸ããåãå¤ãè¦ã¤ãã | â ââ | â |
31 | numpyã®warningãç¡è¦ãã | â ââ | â |
32 | np.sqrtã¨np.emath.sqrtã®éã | â ââ | â |
33 | æ¨æ¥ãä»æ¥ãææ¥ã®æ¥ä» | â ââ | â |
34 | 2016å¹´7æã®æ¥ä»é å | â ââ | â |
35 | è¦ç´ ã®è¨ç®( (A+B)*(-A/2) )ãã³ãã¼ãªãã§è¨ç® | â â â | â |
36 | ä¹±æ°è¡åã®æ´æ°é¨åã ãåãåºããã¤5ç¨®é¡ | â â â | â |
37 | è¡ã®å¤ã[0,1,2,3,4]ã¨ãªãã5Ã5ã®è¡å | â â â | â |
38 | ã¸ã§ãã¬ã¼ã¿ã使ã£ã¦0ãã9ã¾ã§ã®é åãä½ã | â â â | â |
39 | 0ãã大ãï¼ããå°ã®ãµã¤ãº10ã®rangingãã¯ãã«ãä½ã | â â â | â³ |
40 | ãµã¤ãº10ã®ã©ã³ãã é åãä½ã£ã¦sortãã | â â â | â |
41 | å°ããè¡åãnp.sumããæ©ãè¨ç®ããæ¹æ³ã¯ï¼ | â â â | â |
42 | 2ã¤ã®ã©ã³ãã ãã¯ãã«ãçãããã©ãããå¤å®ãã | â â â | â |
43 | é åãimmutable(read-only)ã«ãã | â â â | â |
44 | ç´äº¤åº§æ¨ã®10Ã2ã®è¡åã極座æ¨å¤æ | â â â | â |
45 | ãµã¤ãº10ã®ãã¯ãã«ãä½ã£ã¦æ大å¤ã0ã«å¤æ | â â â | â |
46 | x,y座æ¨ã[0,1]Ã[0,1]é åã«å¤æããstructured arrayãä½ã | â â â | â |
47 | 2ã¤ã®ãã¯ãã«ãç¨ãã¦ã³ã¼ã·ã¼é å(Cij =1/(xi - yj))ãä½ã | â â â | â |
48 | ããããã®numpy scalar typeã§ã®æå°ãæ大å¤ãprintãã | â â â | â |
49 | é åã®å ¨ã¦ã®å¤ãçç¥ããã«ããªã³ããã | â â â | â |
50 | ãã¯ãã«å ã®ä¸ããããvalueã®ä¸ã§æãè¿ãå¤ãåºå | â â â | â |
51 | ä½ç½®åº§æ¨ã¨colorã®RGBãä¿åãããstructured arrayãä½ã | â â â | â |
52 | 10Ã2ã®ã©ã³ãã è¡åãä½ã£ã¦ãç¹å士ã®è·é¢ãæ±ãã | â â â | â |
53 | float32ã®è¡åãint32ã®è¡åã«å¤æ | â â â | â |
54 | ã©ããã£ã¦ãã¡ã¤ã«ãé åã«å¤æããï¼ | â â â | â |
55 | numpyã®é åã®enumerateã¨åçã®è¡¨ç¾ã¯ï¼ | â â â | â |
56 | 2Dã®Gaussian-like arrayãçæãã | â â â | â |
57 | 2次å é åã«ã©ã³ãã ã«påãå¤ãåãè¾¼ã | â â â | â |
58 | è¡åã®åè¡ã«å¯¾ãããã®è¡ã®å¹³åãå¼ãã¦ãã | â â â | â |
59 | nåç®ã®colomunãã½ã¼ã | â â â | â |
60 | 2D arrayã«nullããããã©ãããå¤å®ãã | â â â | â |
61 | æå®ã®å¤ã«æãè¿ãå¤ãè¦ã¤ãã | â â â | â |
62 | ã¤ãã¬ã¼ã¿ã使ã£ã¦1Ã3è¡åã¨3Ã1è¡åã®sumãè¨ç® | â â â | â |
63 | ååã®å±æ§ãä»ä¸ããarrayã¯ã©ã¹ãã¤ãã | â â â | â |
çµæ
ãªããªãå³ããçµæã¨ãªã£ããå³ããã åç´ããçµæ§ç¥ããªããã®ãåºã¦ããã®ã§ãã»ã¼ãã¨æããªããã¨ããã
ä¸ç´ä»¥éã¯ããã¡ãèªåãããã«ããªããã£ããªæä½ããã§ãã¦ããªããã ãªã¨ããã®ãçæããã
é£æ度 | åé¡æ° | æ£è§£ | æ£çç |
---|---|---|---|
â ââ | 34 | 23 | 67% |
â â â | 29 | 12 | 41% |
numpyé åã®åºæ¬
ãã£ãããªã®ã§ããã使ãnpã®é¢æ°ã¨ãæ°ããç¥ã£ããå¾æ©è½ãã¡ã¢ãã¦ããããããã®ããããç¥ã£ã¦ããã°ãåºæ¬çãªæä½ã¯ã§ããã¨æãããã
é åãä½ã
ã¨ããããé åãä½ããªãã¨ã話ã«ãªããªãã®ã§ãä½ãã¨ãã«ä¾¿å©ãªé¢æ°ãã¡ãåæ
np.array([1,2,3,4,5]) #>>>array([1, 2, 3, 4, 5]) np.zeros((3,3)) #>>>array([[ 0., 0., 0.], # [ 0., 0., 0.], # [ 0., 0., 0.]]) np.ones((3,3)) #>>>array([[ 1., 1., 1.], # [ 1., 1., 1.], # [ 1., 1., 1.]]) np.random.random(5) #0ãã1ã¾ã§ã®ã©ã³ãã å¤æ°ããµã¤ãº5ã®ãã¯ãã«ã§ #>>>array([ 0.33732001, 0.73696762, 0.69890914, 0.46966465, 0.91019171]) np.random.randint(0,5,10) #0ãã4ã¾ã§ã®ã©ã³ãã æ´æ°ããµã¤ãº10ã®ãã¯ãã«ã§ #>>>array([4, 3, 4, 1, 1, 4, 1, 3, 2, 0]) np.random.uniform(0,1,5) #0ãã1ã¾ã§ã®ä¸æ§å®æ°ããµã¤ãº5ã®ãã¯ãã«ã§ #>>>array([ 0.72515655, 0.09389927, 0.49393525, 0.11564103, 0.31317932]) np.random.normal(0,1,(3,3)) #å¹³å0, åæ£1ã®æ£è¦åå¸ã3Ã3ã®è¡å㧠#>>>array([[ 0.64296467, -0.14865526, -0.24650383], # [ 0.30479151, -1.61713417, -0.15371185], # [ 0.92736104, 1.58778307, -1.86301156]]) np.arange(10) #0ãã9ã¾ã§é çªã« #>>>array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) np.arange(1,10,2) #1ãã10ãè¶ ããç´åã®æ°åã¾ã§2åé£ã°ã㧠#>>>array([1, 3, 5, 7, 9]) np.linspace(0,10,10) #0ãã10ã¾ã§ã5åå² #>>>array([ 0. , 2.5, 5. , 7.5, 10. ]) # 5Ã5ã®é åã®ä¸ã«x,y座æ¨ãåãè¾¼ã A = np.zeros((5,5), [('x',float),('y',float)]) A['x'], A['y'] = np.meshgrid(np.linspace(0,1,5), np.linspace(0,1,5)) print(A) # >>> [[( 0. , 0. ) ( 0.25, 0. ) ( 0.5 , 0. ) ( 0.75, 0. ) ( 1. , 0. )] # [( 0. , 0.25) ( 0.25, 0.25) ( 0.5 , 0.25) ( 0.75, 0.25) ( 1. , 0.25)] # [( 0. , 0.5 ) ( 0.25, 0.5 ) ( 0.5 , 0.5 ) ( 0.75, 0.5 ) ( 1. , 0.5 )] # [( 0. , 0.75) ( 0.25, 0.75) ( 0.5 , 0.75) ( 0.75, 0.75) ( 1. , 0.75)] # [( 0. , 1. ) ( 0.25, 1. ) ( 0.5 , 1. ) ( 0.75, 1. ) ( 1. , 1. )]] # ã¤ãã¬ã¼ã¿ã使ã£ã¦0ãã9ã¾ã§ã®ãµã¤ãº10ã®é å def generate(): for x in range(10): yield x a = np.fromiter(generate(),dtype=float,count=-1) print(a) #>>> [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] # ãã§ãã«ã¼ãã©ãã°ã®ããã«ã4Ã4ã®è¡åã«0,1ã並ã¹ã np.tile(np.array([[0,1],[1,0]]),(2,2)) #>>> array([[0, 1, 0, 1], # [1, 0, 1, 0], # [0, 1, 0, 1], # [1, 0, 1, 0]])
å½¢ãå¤ãã
reshape
ã使ã
a = np.arange(10) print(a) >>> a.reshape(2,5) >>>
endpointã®æç¡
linspaceã«ã¤ãã¦ãæå®ããå¤ã以ä¸
ã¨ãã¦æ±ããæªæº
ã¨ãã¦æ±ãã
np.linspace(0,10,10) #>>> array([ 0. , 1.11111111, 2.22222222, 3.33333333, # 4.44444444, 5.55555556, 6.66666667, 7.77777778, # 8.88888889, 10. ]) np.linspace(0,10,10,endpoint=False) #>>>array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
hstack, vstack, concatenate
listã®appendã¿ãããªãã®ããã¯ãã«å士ã®çµå
a = np.arange(4) b = np.arange(4)[::-1] print(a,b) #>>> [0 1 2 3] [3 2 1 0] print(np.hstack((a,b))) #>>> [0 1 2 3 3 2 1 0] print(np.vstack((a,b))) #>>> [[0 1 2 3] # [3 2 1 0]]
ã¹ã«ã©ã¼å¤ã足ãã¨ã©ããªãï¼
å ¨é¨ã«é©ç¨ããã
a = np.arange(4) print(a) #>>> [0 1 2 3] print(a+1) #>>> [1 2 3 4]
åå¼·ã«ãªã£ãåé¡(ä¸é¨æç²)
å¤ãã®åé¡ãå¦ã³ã®ãããã®ã ã£ãããå人çã«åå¼·ã«ãªã£ãåé¡ãæç²ãã ãªããå ¨åé¡ã¨åçã¯githubã«ããã¦ããã ã¨ã¯ãããä¸ç´ã¯ããªãåå¼·ã«ãªã£ãã®ã§ãå ¨é¨è©¦ãã¦ã¿ãã¨å¦ã³ã大ãããããããªãã githubã«ã¯åé¡ã®å訳ã¨åçãããã¦ã¿ããèªåãªãã®åçããã¦ãããã®ãããã
24. 5Ã3ã®è¡åã¨3Ã2ã®è¡åãæãåããã(å®æ°)
np.dotã ããããªãã¦ã@
ãªãæ¼ç®åããããããã
A = np.arange(0,12).reshape(4,3) B = np.arange(0,6).reshape(3,2) print(A) print(B) print(A@B)
åºå
[[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] [[0 1] [2 3] [4 5]] [[10 13] [28 40] [46 67]
25. 1次å é åã§ã3以ä¸8以ä¸ã®å¤ã¯è² å¤ã«ãã
æ¡ä»¶æã§æ¤ç´¢ãããããã¨ãã§ãã
a = np.arange(15) a[(a>3)&(a<=8)] *= -1 print(a)
åºå
[ 0 1 2 3 -4 -5 -6 -7 -8 9 10 11 12 13 14]
36. ä¹±æ°è¡åã®æ´æ°é¨åã ãåãåºããã¤ã5種é¡
//
ã¨ããæ¼ç®åãã¯ããã¦ç¥ã£ãã
Z = np.random.uniform(0,10,10) print("36:") print(Z - Z%1) # ä½ããå¼ã print(np.floor(Z)) # åãæ¨ã¦ã®é¢æ° print(np.ceil(Z)-1) # åãä¸ãã¦1ãå¼ã print(Z.astype(int).astype(float)) # æ´æ°ã«ãã¡ãã print(np.trunc(Z)) # æ´æ°é¨ã ãåãåºã print(Z//1) # åãæ¨ã¦æ¼ç®å
41. å°ããè¡åãnp.sum()ããæ©ãè¨ç®ããæ¹æ³ã¯ï¼
add.reduce()
ã使ãã¨è¯ãããã
a = np.arange(100) print(np.add.reduce(a))
43. é åãimmutable(read-only)ã«ãã
writeableãfalseã®è¨å®ã«ãã¦ãæ¸è¾¼ã¿ç¦æ¢ã«ãããnumpyã¯ãã®è¨å®ãã§ãã
a43 = np.zeros(10) a43.flags.writeable = False # a43[0] = 1 ã¨ã©ã¼ãåºã
49. é åã®å ¨ã¦ã®å¤ãçç¥ããã«ããªã³ããã
printoptionsãå©ç¨ãããã¨ã§ã çç¥ãããã«é åã®å ¨è¦ç´ ãè¦ããã¨ãã§ããã
np.set_printoptions(threshold=np.nan) x = np.zeros((16,16))
55. numpyã®é åã®enumerateã¨åçã®è¡¨ç¾ã¯ï¼
é常ã®enumerate
ã®ãããªãndenumerate
, ndindex
ã¨ããã®ãããããã£ã¡ã®ã»ããæ©ãã®ããªï¼ããã¾ãéããããã£ã¦ããªãã
A = np.arange(9).reshape(3,3) for index, value in np.ndenumerate(A): print(index, value) for index in np.ndindex(A.shape): print(index, A[index])
ã¾ã¨ã
å ¨ç¶ããããªããã®ããããããã£ã¦ãã¨ã¦ãåå¼·ã«ãªã£ãã ææ¥ã¯ä¸ç´(â â â )ã«ãã£ã¬ã³ã¸äºå®ã ãã0ç¹ãªã®ã§ã¯ãªããã¨å±æ§ãã¦ããã ã§ããnumpyã¯ã©ãããå ´é¢ã§ã使ãã®ã§ãåºæ¬ããã£ããæãã¦ãããããªã¨æãã
ããã§ã¯ã¾ãææ¥ï¼