pythonã§å¤æ¬¡å é åãæ±ã
pythonã§é åãæ±ãã©ã¤ãã©ãªã¨ãã¦æåãªã®ã¯numpyã¨ãããã®ã§ãã
Python ã¯ã¤ã³ã¿ããªã¿è¨èªã§ãããæ°å¦ã®ã¢ã«ã´ãªãºã ã¯Cè¨èªãªã©ã®ã³ã³ãã¤ã«è¨èªãJavaãªã©ã¨æ¯ã¹ã¦ä½éã«åä½ããå ´åãå¤ããNumPy ã¯ããããåé¡ããå¤æ¬¡å é åã¨ãé åãæä½ããå¤æ°ã®é¢æ°ãæ¼ç®åãæä¾ãããã¨ã§ãã®åé¡ã解ããã¨ãã¦ãããããã«ãããé åãè¡åã®æä½ã¨ãã¦è¨è¿°ã§ããã¢ã«ã´ãªãºã ã¯ãç価㪠C ã®ã³ã¼ãã¨ã»ã¼åçã®é度ã§åä½ããã
NumPy - Wikipedia
ã¨ããããã«ãé常ã«é«éã«ãã¤ç°¡åã«è¡åè¨ç®ãè¡ãªããã¨ãã§ãã¾ãã
matplotlibã¯ãnumpyã®è¡åãå¦çãã¦ã°ã©ãã®ä½æããã¦ãã¾ãã
ã¾ãããã¼ã¿å¦çã«ãæ´»èºãã¦ããããã¨ã§ãããã
ã¨ããããã§ãèªåããã使ãé¢æ°çãã¾ã¨ãã¦ããã¾ãã
åèè³æ
åèã«ãªããåèã«ããWebãµã¤ãã®ãªã¹ããæãã¦ããã¾ãã
ç¹ã«ãã®ä¸ã®numpy tutorialãåèã«ãã¦ãã¾ãã
é åã®çæ
numpyã¯å¤æ¬¡å
é
åãæä½ããã©ã¤ãã©ãªã§ãã
主ã¨ãã¦ãarrayãªãã¸ã§ã¯ããæä½ãã¦ãããã¨ã«ãªãã¾ãã
matrixã¨ããã¯ã©ã¹ãç¨æããã¦ããããã§ãããåºæ¬çã«ã¯arrayã使ã£ã¦ãããã¨ã«ãªãã¾ãã
arrayã®çæã«ã¯ããã¤ãæ¹æ³ããããèªåãè¯ã使ãã®ã¯ã
- ãªã¹ãããå¤æ
- arange, linspaceé¢æ°
- ãã¡ã¤ã«ããloadtxt (ããã«ä¸¦ã¹ã¦è¯ããããããªããã©)
ãããä»ã«ãææ³ã¯ããããã§ãã
ãªã¹ãããå¤æ
Pythonçµã¿è¾¼ã¿ã®ãªã¹ãããarrayãçæãããã¨ãã§ãã¾ãã
>>> from numpy import * >>> array([1,2,3]) array([1, 2, 3]) >>> >>> array([1., 2., 3.]) array([ 1., 2., 3.]) >>> >>> array([1, 2, 3], dtype=float) # dtypeãæå®ãããã¨ã§arrayã®åã決ã¾ã array([ 1., 2., 3.])
ãªã¹ãããã®çæã«ã¯array()ãç¨ãã¾ãã
ãã¼ã¿åã¯ãªã¹ãå
ã®åãã決å®ããã¾ãããdtypeãæå®ãããã¨ã§ãarrayã®åã決å®ãããã¨ã§ãã¾ãã
arange(), linspace()
arange()ã¯range()ã¨ä¼¼ã¦ãã¾ãããrangeããªã¹ããè¿ãã®ã«å¯¾ãarangeã¯arrayãè¿ãã¾ãã
å¼æ°ãæµ®åå°æ°ã§ãè¯ãã®ãç¹å¾´ã§ããããã
>>> arange(0, 10, 0.1) # 0ãã10ã¾ã§0.1å»ã¿ã§ã array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8, 6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9. , 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9])
arangeé¢æ°ã¯ããç¯å²å
ããæå®ããã¹ãããã§åºåã£ã¦ããã¾ãã
å¾ã£ã¦ãarrayã®ä¸ã®è¦ç´ ãããã¤ã«ãªããã¯è¨ç®ããªãã¨åããã¾ããã
ããç¯å²ããæå®ããè¦ç´ æ°ã«ãªãããã«åºåãã«ã¯linspaceé¢æ°ã使ãã¾ãã
>>> linspace(0, 2, 20) # 0ãã2ã¾ã§ã®éã§20åã®è¦ç´ ãä½ãã㨠>>> a = linspace(0, 2, 20) >>> a array([ 0. , 0.10526316, 0.21052632, 0.31578947, 0.42105263, 0.52631579, 0.63157895, 0.73684211, 0.84210526, 0.94736842, 1.05263158, 1.15789474, 1.26315789, 1.36842105, 1.47368421, 1.57894737, 1.68421053, 1.78947368, 1.89473684, 2. ]) >>> >>> len(a) 20
ãã¡ã¤ã«ãã
ãã¡ã¤ã«ããarrayãä½æããæ¹æ³ã«ãããã¤ãããããã§ããã
ããã§ã¯ã空ç½åºåãã®ããã¹ããã¡ã¤ã«ãèªã¿è¾¼ã¿ã¾ãã
0 10 20
1 20 30
2 40 90
3 50 120
4 80 240
ããã«ã¯loadtxté¢æ°ã使ãã¾ãã
>>> from numpy import * >>> data = loadtxt("data.txt") # ä¸ã®ãã¼ã¿ã¯data.txtã«ä¿åãã¦ããã¾ãã >>> data array([[ 0., 10., 20.], [ 1., 20., 30.], [ 2., 40., 90.], [ 3., 50., 120.], [ 4., 80., 240.]]) >>> data[1,:] # 2è¡ç®ã®åå¾(æ·»åã¯0ããå§ã¾ãã®ã§) array([ 1., 20., 30.]) >>> data[:,1] # 2åç®ã®åå¾ array([ 10., 20., 40., 50., 80.])
以ä¸ã¯Routines — NumPy v1.16 Manualããã®å¼ç¨ã§ãã
>>> from numpy import * >>> >>> data = loadtxt("myfile.txt") # myfile.txt contains 4 columns of numbers >>> t,z = data[:,0], data[:,3] # data is 2D numpy array >>> >>> t,x,y,z = loadtxt("myfile.txt", unpack=True) # to unpack all columns >>> t,z = loadtxt("myfile.txt", usecols = (0,3), unpack=True) # to select just a few columns >>> data = loadtxt("myfile.txt", skiprows = 7) # to skip 7 rows from top of file >>> data = loadtxt("myfile.txt", comments = '!') # use '!' as comment char instead of '#' >>> data = loadtxt("myfile.txt", delimiter=';') # use ';' as column separator instead of whitespace >>> data = loadtxt("myfile.txt", dtype = int) # file contains integers instead of floats
é©å½ã«è¨³ãã¨(è¶ æ訳)
>>> from numpy import * >>> >>> data = loadtxt("myfile.txt") # myfile.txtã¯4ã¤ã®åãå«ã¿ã¾ã >>> t,z = data[:,0], data[:,3] # dataã¯2次å ã®numpyã®arrayã¨ãªã£ã¦ãã¾ã >>> >>> t,x,y,z = loadtxt("myfile.txt", unpack=True) # å ¨ã¦ã®åãã¾ã¨ããã«åãåºãã¾ã >>> t,z = loadtxt("myfile.txt", usecols = (0,3), unpack=True) # 使ç¨ããåãæå®ãã¦åãã ãã¾ã >>> data = loadtxt("myfile.txt", skiprows = 7) # ãã¡ã¤ã«ã®7è¡ç®ã¯ç¡è¦ãã¦ä»ãåãåºãã¾ã >>> data = loadtxt("myfile.txt", comments = '!') # é常ã¯#ãã³ã¡ã³ãã ãã!ãã³ã¡ã³ãã¨ãã(ãã¡ã¤ã«å ã®) >>> data = loadtxt("myfile.txt", delimiter=';') # ã¹ãã¼ã¹ã§ãªãã;ããã¼ã¿ã®åºåãã«ä½¿ç¨ >>> data = loadtxt("myfile.txt", dtype = int) # æµ®åå°æ°ã§ãªããæ´æ°ã®ãã¼ã¿ãåãåºã
ã¨ãã£ãå
·åã«ãã¼ã¿ãåãåºããã¨ãã§ãã¾ãã
loadtxtã¯å®é¨ãã¼ã¿ã®å¦ççã«ä¾¿å©ã§ããã
ã¹ã©ã¤ã¹
ã¹ã©ã¤ã¹ã¯ãPythonã®ãªã¹ãã¨åæ§ã«ã§ãã¾ãã
2次å
é
åã®å ´å[è¡, å]ã§åãåºãã¾ãã
:ãä¸æã«ä½¿ããã¨ã§ãè¡ãåã®åãåºããèªç±ã§ãã
>>> a = array([[1,2,3], [4,5,6], [7,8,9]]) >>> a array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> a[1,2] 6 >>> a[0,:] # 1è¡ç®ã®åãåºã array([1, 2, 3]) >>> a[1,:] # 2è¡ç®ã®åãåºã array([4, 5, 6]) >>> a[:,1] # 2åç®ã®åãåºã array([2, 5, 8])
è¨ç®
ç®è¡æ¼ç®ã¯ãè¦ç´ ãã¨ã«è¡ãªããã¾ãã
ä¾ãã°A+Bã®å ´åãA[0,0]ã¨B[0,0]ãA[0,1]ã¨B[0,1]....ã¨ãã£ãæãã§ãã
ããç®ã®å ´åã§ãåæ§ã§ãã(è¡åã®ããç®ã«ã¯ãªãã¾ããã)
æè¬è¡åã®ããç®ããããæã¯dot()é¢æ°ã使ã£ã¦ãã ããã
ã¾ããè¡åç¨ã®æ°å¦é¢æ°ãnumpyã§ã¯ãããããç¨æããã¦ãã¾ãã
numpy.cosãªã©ã¯è¡åã®è¨ç®ãè¡ãªããã¨ãã§ãã¾ãã
>>> from numpy import * >>> a = random.random((3,3)) # numpy.random.randomã3*3ã®arrayãä½æ >>> b = array([[1,2,3], [4,5,6], [7,8,9]]) >>> a array([[ 0.70235191, 0.48829014, 0.33720279], [ 0.64187382, 0.63667402, 0.4107362 ], [ 0.79077403, 0.80895472, 0.5039849 ]]) >>> b array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> a + b array([[ 1.70235191, 2.48829014, 3.33720279], [ 4.64187382, 5.63667402, 6.4107362 ], [ 7.79077403, 8.80895472, 9.5039849 ]]) >>> a * b # è¡åã®ããç®ã§ã¯ãªã array([[ 0.70235191, 0.97658027, 1.01160836], [ 2.56749528, 3.18337011, 2.46441719], [ 5.53541819, 6.47163772, 4.53586408]]) >>> cos(a) array([[ 0.76332493, 0.88313627, 0.94368381], [ 0.80097531, 0.80407758, 0.91682712], [ 0.70329527, 0.69025514, 0.87566514]]) >>> exp(a*1j) array([[ 0.76332493+0.64601475j, 0.88313627+0.46911653j, 0.94368381+0.33084871j], [ 0.80097531+0.59869737j, 0.80407758+0.59452439j, 0.91682712+0.3992844j ], [ 0.70329527+0.71089786j, 0.69025514+0.72356606j, 0.87566514+0.4829188j ]])
ä»ã«ãç§ããã使ãã¡ã½ããã
>>> a array([[ 0.70235191, 0.48829014, 0.33720279], [ 0.64187382, 0.63667402, 0.4107362 ], [ 0.79077403, 0.80895472, 0.5039849 ]]) >>> a.sum() # åè¨ 5.3208425191396165 >>> a.max() # æå¤§å¤ 0.80895471516033424 >>> a.min() # æå°å¤ 0.33720278811620497
ç¶ãã¯ã¾ãä»åº¦