Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.80 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {
  6. "slideshow": {
  7. "slide_type": "slide"
  8. }
  9. },
  10. "source": [
  11. "## numpy チュートリアル\n",
  12. "\n",
  13. "Tomonori Kodaira"
  14. ]
  15. },
  16. {
  17. "cell_type": "markdown",
  18. "metadata": {
  19. "slideshow": {
  20. "slide_type": "slide"
  21. }
  22. },
  23. "source": [
  24. "# numpy\n",
  25. "## 行列とか扱いやすくするやつ"
  26. ]
  27. },
  28. {
  29. "cell_type": "markdown",
  30. "metadata": {},
  31. "source": [
  32. "## ndarray"
  33. ]
  34. },
  35. {
  36. "cell_type": "code",
  37. "execution_count": 136,
  38. "metadata": {
  39. "collapsed": false,
  40. "slideshow": {
  41. "slide_type": "subslide"
  42. }
  43. },
  44. "outputs": [
  45. {
  46. "name": "stdout",
  47. "output_type": "stream",
  48. "text": [
  49. "[[0 1 2 3]\n",
  50. " [9 8 7 6]]\n"
  51. ]
  52. },
  53. {
  54. "data": {
  55. "text/plain": [
  56. "numpy.ndarray"
  57. ]
  58. },
  59. "execution_count": 136,
  60. "metadata": {},
  61. "output_type": "execute_result"
  62. }
  63. ],
  64. "source": [
  65. "x = numpy.array([[0,1,2,3], [9,8,7,6]]); print(x)\n",
  66. "x.__class__"
  67. ]
  68. },
  69. {
  70. "cell_type": "code",
  71. "execution_count": 137,
  72. "metadata": {
  73. "collapsed": false
  74. },
  75. "outputs": [
  76. {
  77. "name": "stdout",
  78. "output_type": "stream",
  79. "text": [
  80. "shape, size: (2, 4) 8\n",
  81. "any, all: True False\n",
  82. "sum: 36\n",
  83. "flatten: [0 1 2 3 9 8 7 6]\n",
  84. "Transpose: \n",
  85. "[[0 9]\n",
  86. " [1 8]\n",
  87. " [2 7]\n",
  88. " [3 6]]\n",
  89. "fill: \n",
  90. "[[10 10 10 10]\n",
  91. " [10 10 10 10]]\n"
  92. ]
  93. }
  94. ],
  95. "source": [
  96. "print(\"shape, size: \", x.shape, x.size)\n",
  97. "print(\"any, all: \", x.any(), x.all())\n",
  98. "print(\"sum: \", x.sum())\n",
  99. "print(\"flatten: \", x.flatten())\n",
  100. "print(\"Transpose: \", x.T, sep='\\n')\n",
  101. "x.fill(10)\n",
  102. "print(\"fill: \", x, sep='\\n')"
  103. ]
  104. },
  105. {
  106. "cell_type": "markdown",
  107. "metadata": {
  108. "slideshow": {
  109. "slide_type": "slide"
  110. }
  111. },
  112. "source": [
  113. "# よく使うコマンド"
  114. ]
  115. },
  116. {
  117. "cell_type": "markdown",
  118. "metadata": {},
  119. "source": [
  120. "# zeros, arange, ones, random.*, concatenate, stack etc.."
  121. ]
  122. },
  123. {
  124. "cell_type": "markdown",
  125. "metadata": {
  126. "slideshow": {
  127. "slide_type": "subslide"
  128. }
  129. },
  130. "source": [
  131. "## zeros, ones"
  132. ]
  133. },
  134. {
  135. "cell_type": "code",
  136. "execution_count": 232,
  137. "metadata": {
  138. "collapsed": false
  139. },
  140. "outputs": [
  141. {
  142. "data": {
  143. "text/plain": [
  144. "array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
  145. ]
  146. },
  147. "execution_count": 232,
  148. "metadata": {},
  149. "output_type": "execute_result"
  150. }
  151. ],
  152. "source": [
  153. "numpy.zeros((1, 10))"
  154. ]
  155. },
  156. {
  157. "cell_type": "code",
  158. "execution_count": 37,
  159. "metadata": {
  160. "collapsed": false
  161. },
  162. "outputs": [
  163. {
  164. "data": {
  165. "text/plain": [
  166. "array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
  167. ]
  168. },
  169. "execution_count": 37,
  170. "metadata": {},
  171. "output_type": "execute_result"
  172. }
  173. ],
  174. "source": [
  175. "numpy.ones(10)"
  176. ]
  177. },
  178. {
  179. "cell_type": "code",
  180. "execution_count": 233,
  181. "metadata": {
  182. "collapsed": false
  183. },
  184. "outputs": [
  185. {
  186. "data": {
  187. "text/plain": [
  188. "array([[[ 0., 0., 0.],\n",
  189. " [ 0., 0., 0.],\n",
  190. " [ 0., 0., 0.],\n",
  191. " [ 0., 0., 0.]],\n",
  192. "\n",
  193. " [[ 0., 0., 0.],\n",
  194. " [ 0., 0., 0.],\n",
  195. " [ 0., 0., 0.],\n",
  196. " [ 0., 0., 0.]]])"
  197. ]
  198. },
  199. "execution_count": 233,
  200. "metadata": {},
  201. "output_type": "execute_result"
  202. }
  203. ],
  204. "source": [
  205. "numpy.zeros((2, 4, 3))"
  206. ]
  207. },
  208. {
  209. "cell_type": "markdown",
  210. "metadata": {
  211. "slideshow": {
  212. "slide_type": "subslide"
  213. }
  214. },
  215. "source": [
  216. "## random, arange"
  217. ]
  218. },
  219. {
  220. "cell_type": "code",
  221. "execution_count": 239,
  222. "metadata": {
  223. "collapsed": false
  224. },
  225. "outputs": [
  226. {
  227. "data": {
  228. "text/plain": [
  229. "array([[ 0.21240213, 0.51893698, 0.37301441, 0.52153013, 0.4189147 ,\n",
  230. " 0.50376525, 0.58299756, 0.37359483, 0.88470023, 0.57319329],\n",
  231. " [ 0.22352932, 0.6454468 , 0.04963214, 0.57786814, 0.20154706,\n",
  232. " 0.8007831 , 0.49362569, 0.33227308, 0.32134634, 0.43553474]])"
  233. ]
  234. },
  235. "execution_count": 239,
  236. "metadata": {},
  237. "output_type": "execute_result"
  238. }
  239. ],
  240. "source": [
  241. "numpy.random.random((2, 10))\n",
  242. "# numpy.random.randint(0, 10, (2, 4))"
  243. ]
  244. },
  245. {
  246. "cell_type": "code",
  247. "execution_count": 177,
  248. "metadata": {
  249. "collapsed": false
  250. },
  251. "outputs": [
  252. {
  253. "data": {
  254. "text/plain": [
  255. "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
  256. ]
  257. },
  258. "execution_count": 177,
  259. "metadata": {},
  260. "output_type": "execute_result"
  261. }
  262. ],
  263. "source": [
  264. "numpy.arange(10)"
  265. ]
  266. },
  267. {
  268. "cell_type": "markdown",
  269. "metadata": {
  270. "slideshow": {
  271. "slide_type": "subslide"
  272. }
  273. },
  274. "source": [
  275. "# concat"
  276. ]
  277. },
  278. {
  279. "cell_type": "code",
  280. "execution_count": 245,
  281. "metadata": {
  282. "collapsed": false
  283. },
  284. "outputs": [
  285. {
  286. "name": "stdout",
  287. "output_type": "stream",
  288. "text": [
  289. "[[ 0 1 2 3 4 5 6 7 8 9]\n",
  290. " [10 11 12 13 14 15 16 17 18 19]]\n"
  291. ]
  292. }
  293. ],
  294. "source": [
  295. "x = numpy.arange(20).reshape(2, 10); print(x)"
  296. ]
  297. },
  298. {
  299. "cell_type": "code",
  300. "execution_count": 222,
  301. "metadata": {
  302. "collapsed": false
  303. },
  304. "outputs": [
  305. {
  306. "name": "stdout",
  307. "output_type": "stream",
  308. "text": [
  309. "[[ 0 1 2 3 4 5 6 7 8 9]\n",
  310. " [10 11 12 13 14 15 16 17 18 19]\n",
  311. " [ 0 1 2 3 4 5 6 7 8 9]\n",
  312. " [10 11 12 13 14 15 16 17 18 19]] (4, 10)\n",
  313. "\n",
  314. "[[ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]\n",
  315. " [10 11 12 13 14 15 16 17 18 19 10 11 12 13 14 15 16 17 18 19]] (2, 20)\n"
  316. ]
  317. }
  318. ],
  319. "source": [
  320. "cx = numpy.concatenate((x, x)); print(cx, cx.shape, end=\"\\n\\n\")\n",
  321. "cx = numpy.concatenate((x, x), axis=1); print(cx, cx.shape)"
  322. ]
  323. },
  324. {
  325. "cell_type": "markdown",
  326. "metadata": {
  327. "slideshow": {
  328. "slide_type": "subslide"
  329. }
  330. },
  331. "source": [
  332. "# stack"
  333. ]
  334. },
  335. {
  336. "cell_type": "code",
  337. "execution_count": 252,
  338. "metadata": {
  339. "collapsed": false
  340. },
  341. "outputs": [
  342. {
  343. "name": "stdout",
  344. "output_type": "stream",
  345. "text": [
  346. "[[[ 0 2 2 3 100 5 6 7 8 9]\n",
  347. " [ 10 11 12 13 14 15 16 17 18 19]]\n",
  348. "\n",
  349. " [[ 0 2 2 3 100 5 6 7 8 9]\n",
  350. " [ 10 11 12 13 14 15 16 17 18 19]]] (2, 2, 10)\n"
  351. ]
  352. }
  353. ],
  354. "source": [
  355. "sx = numpy.stack((x, x))\n",
  356. "print(sx, sx.shape)"
  357. ]
  358. },
  359. {
  360. "cell_type": "code",
  361. "execution_count": 225,
  362. "metadata": {
  363. "collapsed": false
  364. },
  365. "outputs": [
  366. {
  367. "name": "stdout",
  368. "output_type": "stream",
  369. "text": [
  370. "[[[ 0 1 2 3 4 5 6 7 8 9]\n",
  371. " [ 0 1 2 3 4 5 6 7 8 9]]\n",
  372. "\n",
  373. " [[10 11 12 13 14 15 16 17 18 19]\n",
  374. " [10 11 12 13 14 15 16 17 18 19]]] (2, 2, 10)\n"
  375. ]
  376. }
  377. ],
  378. "source": [
  379. "sx = numpy.stack((x, x), axis=1); print(sx, sx.shape)"
  380. ]
  381. },
  382. {
  383. "cell_type": "markdown",
  384. "metadata": {
  385. "slideshow": {
  386. "slide_type": "subslide"
  387. }
  388. },
  389. "source": [
  390. "# index\n",
  391. "- カンマ区切りで要素にアクセスできる\n",
  392. "- リストが入れられる."
  393. ]
  394. },
  395. {
  396. "cell_type": "code",
  397. "execution_count": 258,
  398. "metadata": {
  399. "collapsed": false,
  400. "slideshow": {
  401. "slide_type": "-"
  402. }
  403. },
  404. "outputs": [
  405. {
  406. "ename": "ValueError",
  407. "evalue": "cannot reshape array of size 40 into shape (2,2,5)",
  408. "output_type": "error",
  409. "traceback": [
  410. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  411. "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
  412. "\u001b[0;32m<ipython-input-258-afe23daab318>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m40\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  413. "\u001b[0;31mValueError\u001b[0m: cannot reshape array of size 40 into shape (2,2,5)"
  414. ]
  415. }
  416. ],
  417. "source": [
  418. "import numpy \n",
  419. "x = numpy.arange(40).reshape(2, 2, 5)\n",
  420. "print(x[:, 1, [1, 3, 5]])\n",
  421. "x"
  422. ]
  423. },
  424. {
  425. "cell_type": "markdown",
  426. "metadata": {
  427. "slideshow": {
  428. "slide_type": "subslide"
  429. }
  430. },
  431. "source": [
  432. "# where"
  433. ]
  434. },
  435. {
  436. "cell_type": "code",
  437. "execution_count": 266,
  438. "metadata": {
  439. "collapsed": false
  440. },
  441. "outputs": [
  442. {
  443. "name": "stdout",
  444. "output_type": "stream",
  445. "text": [
  446. "x =\n",
  447. " [[[ 0 1 2 3 4 5 6 7 8 9]\n",
  448. " [10 11 12 13 14 15 16 17 18 19]]\n",
  449. "\n",
  450. " [[20 21 22 23 24 25 26 27 28 29]\n",
  451. " [30 31 32 33 34 35 36 37 38 39]]]\n",
  452. "[[[2 2 2 2 2 2 2 2 2 2]\n",
  453. " [2 2 2 2 2 2 2 2 2 2]]\n",
  454. "\n",
  455. " [[1 1 1 1 1 1 1 1 1 1]\n",
  456. " [1 1 1 1 1 1 1 1 1 1]]] (2, 2, 10)\n"
  457. ]
  458. },
  459. {
  460. "data": {
  461. "text/plain": [
  462. "array([[[2, 0, 2, 0, 2, 0, 2, 0, 2, 0],\n",
  463. " [2, 0, 2, 0, 2, 0, 2, 0, 2, 0]],\n",
  464. "\n",
  465. " [[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],\n",
  466. " [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]]])"
  467. ]
  468. },
  469. "execution_count": 266,
  470. "metadata": {},
  471. "output_type": "execute_result"
  472. }
  473. ],
  474. "source": [
  475. "print(\"x =\\n\", x)\n",
  476. "# print(\"\\nx % 2 == 0,if True 1 else 0\\n\", numpy.where(x % 2 == 0, 1, 0))\n",
  477. "two = numpy.where(x % 2 == 0)\n",
  478. "# print(\"\\nwhere\", *two, sep='\\n')\n",
  479. "ones = numpy.ones_like(x);ones[0] += 1; zeros = numpy.zeros_like(x)\n",
  480. "print(ones, zeros.shape)\n",
  481. "numpy.where(x % 2 == 0, ones, zeros)"
  482. ]
  483. },
  484. {
  485. "cell_type": "markdown",
  486. "metadata": {
  487. "slideshow": {
  488. "slide_type": "subslide"
  489. }
  490. },
  491. "source": [
  492. "# 線形代数的な"
  493. ]
  494. },
  495. {
  496. "cell_type": "code",
  497. "execution_count": 269,
  498. "metadata": {
  499. "collapsed": false,
  500. "scrolled": true
  501. },
  502. "outputs": [
  503. {
  504. "name": "stdout",
  505. "output_type": "stream",
  506. "text": [
  507. "[1 2 3 4 5]\n",
  508. "[0 1 0 0 1]\n"
  509. ]
  510. },
  511. {
  512. "data": {
  513. "text/plain": [
  514. "7"
  515. ]
  516. },
  517. "execution_count": 269,
  518. "metadata": {},
  519. "output_type": "execute_result"
  520. }
  521. ],
  522. "source": [
  523. "xx = numpy.arange(5).reshape(5) + 1; print(xx)\n",
  524. "w = numpy.random.randint(0, 3, (5, )); print(w.flatten())\n",
  525. "w.dot(xx)"
  526. ]
  527. },
  528. {
  529. "cell_type": "markdown",
  530. "metadata": {
  531. "collapsed": true,
  532. "slideshow": {
  533. "slide_type": "slide"
  534. }
  535. },
  536. "source": [
  537. "# 加減乗除\n",
  538. "###### 基本的にどこかの次元と形が合っていれば勝手にやってくれる"
  539. ]
  540. },
  541. {
  542. "cell_type": "code",
  543. "execution_count": 83,
  544. "metadata": {
  545. "collapsed": false,
  546. "slideshow": {
  547. "slide_type": "-"
  548. }
  549. },
  550. "outputs": [
  551. {
  552. "data": {
  553. "text/plain": [
  554. "array([[[ 0, 1, 2, 3],\n",
  555. " [ 4, 5, 6, 7],\n",
  556. " [ 8, 9, 10, 11]],\n",
  557. "\n",
  558. " [[12, 13, 14, 15],\n",
  559. " [16, 17, 18, 19],\n",
  560. " [20, 21, 22, 23]]])"
  561. ]
  562. },
  563. "execution_count": 83,
  564. "metadata": {},
  565. "output_type": "execute_result"
  566. }
  567. ],
  568. "source": [
  569. "sample = numpy.arange(24).reshape(2, 3, 4) # shape(2, 3, 4)\n",
  570. "sample"
  571. ]
  572. },
  573. {
  574. "cell_type": "code",
  575. "execution_count": 80,
  576. "metadata": {
  577. "collapsed": false,
  578. "slideshow": {
  579. "slide_type": "subslide"
  580. }
  581. },
  582. "outputs": [
  583. {
  584. "data": {
  585. "text/plain": [
  586. "array([[[ 0, 3, 6, 9],\n",
  587. " [12, 15, 18, 21],\n",
  588. " [24, 27, 30, 33]],\n",
  589. "\n",
  590. " [[36, 39, 42, 45],\n",
  591. " [48, 51, 54, 57],\n",
  592. " [60, 63, 66, 69]]])"
  593. ]
  594. },
  595. "execution_count": 80,
  596. "metadata": {},
  597. "output_type": "execute_result"
  598. }
  599. ],
  600. "source": [
  601. "sample * 3 # shape(, )"
  602. ]
  603. },
  604. {
  605. "cell_type": "code",
  606. "execution_count": 173,
  607. "metadata": {
  608. "collapsed": false,
  609. "slideshow": {
  610. "slide_type": "subslide"
  611. }
  612. },
  613. "outputs": [
  614. {
  615. "name": "stdout",
  616. "output_type": "stream",
  617. "text": [
  618. "[[[ 0 4 12 24]\n",
  619. " [ 8 20 36 56]\n",
  620. " [ 16 36 60 88]]\n",
  621. "\n",
  622. " [[ 24 52 84 120]\n",
  623. " [ 32 68 108 152]\n",
  624. " [ 40 84 132 184]]]\n",
  625. "\n",
  626. "[[[ 0 3 6 9]\n",
  627. " [ 8 10 12 14]\n",
  628. " [ 8 9 10 11]]\n",
  629. "\n",
  630. " [[36 39 42 45]\n",
  631. " [32 34 36 38]\n",
  632. " [20 21 22 23]]]\n"
  633. ]
  634. }
  635. ],
  636. "source": [
  637. "print(sample * [2, 4, 6, 8], end='\\n\\n') # shape (4, )\n",
  638. "print(sample * [[3], [2], [1]]) # shape(3, 1)"
  639. ]
  640. },
  641. {
  642. "cell_type": "code",
  643. "execution_count": 81,
  644. "metadata": {
  645. "collapsed": false,
  646. "slideshow": {
  647. "slide_type": "subslide"
  648. }
  649. },
  650. "outputs": [
  651. {
  652. "data": {
  653. "text/plain": [
  654. "array([[[ 0, 3, 12, 27],\n",
  655. " [ 4, 20, 42, 70],\n",
  656. " [ 16, 45, 80, 121]],\n",
  657. "\n",
  658. " [[ 0, 39, 84, 135],\n",
  659. " [ 16, 68, 126, 190],\n",
  660. " [ 40, 105, 176, 253]]])"
  661. ]
  662. },
  663. "execution_count": 81,
  664. "metadata": {},
  665. "output_type": "execute_result"
  666. }
  667. ],
  668. "source": [
  669. "sample * [[0, 3, 6, 9], [1, 4, 7, 10], [2, 5, 8, 11]] # shape (3, 4)"
  670. ]
  671. },
  672. {
  673. "cell_type": "code",
  674. "execution_count": 92,
  675. "metadata": {
  676. "collapsed": false,
  677. "slideshow": {
  678. "slide_type": "subslide"
  679. }
  680. },
  681. "outputs": [
  682. {
  683. "ename": "ValueError",
  684. "evalue": "operands could not be broadcast together with shapes (2,3,4) (2,3) ",
  685. "output_type": "error",
  686. "traceback": [
  687. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  688. "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
  689. "\u001b[0;32m<ipython-input-92-0539d9cc838c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# sample.shape = 2, 3, 4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0msample\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m7\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# shape (2, 3)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  690. "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,4) (2,3) "
  691. ]
  692. }
  693. ],
  694. "source": [
  695. "# sample.shape = 2, 3, 4\n",
  696. "sample * numpy.array([[0, 3, 6], [1, 4, 7]]).reshape((2,3)) # shape (2, 3)"
  697. ]
  698. },
  699. {
  700. "cell_type": "markdown",
  701. "metadata": {
  702. "slideshow": {
  703. "slide_type": "slide"
  704. }
  705. },
  706. "source": [
  707. "# broadcast_to\n",
  708. "### 次元の拡張を行う."
  709. ]
  710. },
  711. {
  712. "cell_type": "code",
  713. "execution_count": 273,
  714. "metadata": {
  715. "collapsed": false
  716. },
  717. "outputs": [
  718. {
  719. "data": {
  720. "text/plain": [
  721. "array([[[ 0, 3, 6, 9],\n",
  722. " [ 1, 4, 7, 10],\n",
  723. " [ 2, 5, 8, 11]],\n",
  724. "\n",
  725. " [[ 0, 3, 6, 9],\n",
  726. " [ 1, 4, 7, 10],\n",
  727. " [ 2, 5, 8, 11]]])"
  728. ]
  729. },
  730. "execution_count": 273,
  731. "metadata": {},
  732. "output_type": "execute_result"
  733. }
  734. ],
  735. "source": [
  736. "temp = numpy.array([[0, 3, 6, 9], [1, 4, 7, 10], [2, 5, 8, 11]]) # shape (3, 4)\n",
  737. "numpy.broadcast_to(temp, (2, 3, 4)) \n"
  738. ]
  739. },
  740. {
  741. "cell_type": "markdown",
  742. "metadata": {
  743. "slideshow": {
  744. "slide_type": "subslide"
  745. }
  746. },
  747. "source": [
  748. "## broadcast_to関数が扱えるようなshapeに直せばいい"
  749. ]
  750. },
  751. {
  752. "cell_type": "code",
  753. "execution_count": 275,
  754. "metadata": {
  755. "collapsed": false
  756. },
  757. "outputs": [
  758. {
  759. "name": "stdout",
  760. "output_type": "stream",
  761. "text": [
  762. "[[[0]\n",
  763. " [3]\n",
  764. " [6]]\n",
  765. "\n",
  766. " [[1]\n",
  767. " [4]\n",
  768. " [7]]]\n"
  769. ]
  770. },
  771. {
  772. "data": {
  773. "text/plain": [
  774. "array([[[ 0, 0, 0, 0],\n",
  775. " [ 12, 15, 18, 21],\n",
  776. " [ 48, 54, 60, 66]],\n",
  777. "\n",
  778. " [[ 12, 13, 14, 15],\n",
  779. " [ 64, 68, 72, 76],\n",
  780. " [140, 147, 154, 161]]])"
  781. ]
  782. },
  783. "execution_count": 275,
  784. "metadata": {},
  785. "output_type": "execute_result"
  786. }
  787. ],
  788. "source": [
  789. "temp = numpy.array([[0, 3, 6], [1, 4, 7]]).reshape((2,3)) # shape (2, 3)\n",
  790. "temp = temp.reshape(2, 3, 1); print(temp)\n",
  791. "temp * sample\n",
  792. "# numpy.broadcast_to(temp, (2, 3, 4))"
  793. ]
  794. },
  795. {
  796. "cell_type": "markdown",
  797. "metadata": {
  798. "slideshow": {
  799. "slide_type": "slide"
  800. }
  801. },
  802. "source": [
  803. "# 各要素に関数を当てる"
  804. ]
  805. },
  806. {
  807. "cell_type": "code",
  808. "execution_count": 140,
  809. "metadata": {
  810. "collapsed": false,
  811. "scrolled": true
  812. },
  813. "outputs": [
  814. {
  815. "name": "stdout",
  816. "output_type": "stream",
  817. "text": [
  818. "[[[ 0 1 2 3]\n",
  819. " [ 4 5 6 7]\n",
  820. " [ 8 9 10 11]]\n",
  821. "\n",
  822. " [[12 13 14 15]\n",
  823. " [16 17 18 19]\n",
  824. " [20 21 22 23]]]\n"
  825. ]
  826. },
  827. {
  828. "data": {
  829. "text/plain": [
  830. "array([[ 6, 22, 38],\n",
  831. " [54, 70, 86]])"
  832. ]
  833. },
  834. "execution_count": 140,
  835. "metadata": {},
  836. "output_type": "execute_result"
  837. }
  838. ],
  839. "source": [
  840. "print(sample)\n",
  841. "numpy.apply_along_axis(numpy.sum, axis=2, arr=sample)"
  842. ]
  843. },
  844. {
  845. "cell_type": "code",
  846. "execution_count": 142,
  847. "metadata": {
  848. "collapsed": false
  849. },
  850. "outputs": [
  851. {
  852. "data": {
  853. "text/plain": [
  854. "array([[ 6, 22, 38],\n",
  855. " [54, 70, 86]])"
  856. ]
  857. },
  858. "execution_count": 142,
  859. "metadata": {},
  860. "output_type": "execute_result"
  861. }
  862. ],
  863. "source": [
  864. "numpy.sum(sample, axis=2)"
  865. ]
  866. },
  867. {
  868. "cell_type": "markdown",
  869. "metadata": {
  870. "slideshow": {
  871. "slide_type": "slide"
  872. }
  873. },
  874. "source": [
  875. "# axis\n"
  876. ]
  877. },
  878. {
  879. "cell_type": "code",
  880. "execution_count": 284,
  881. "metadata": {
  882. "collapsed": false
  883. },
  884. "outputs": [
  885. {
  886. "name": "stdout",
  887. "output_type": "stream",
  888. "text": [
  889. "[[[ 0 1 2 3]\n",
  890. " [ 4 5 6 7]\n",
  891. " [ 8 9 10 11]]\n",
  892. "\n",
  893. " [[12 13 14 15]\n",
  894. " [16 17 18 19]\n",
  895. " [20 21 22 23]]] (2, 3, 4)\n",
  896. "-----------------------\n",
  897. "Default 276 ()\n",
  898. "axis=1:\n",
  899. " [[12 15 18 21]\n",
  900. " [48 51 54 57]] (2, 4)\n",
  901. "axis=2:\n",
  902. " [[ 6 22 38]\n",
  903. " [54 70 86]] (2, 3)\n"
  904. ]
  905. }
  906. ],
  907. "source": [
  908. "print(sample, sample.shape)\n",
  909. "print(\"-----------------------\")\n",
  910. "sums = sample.sum(axis=None); print(\"Default\", sums, sums.shape)\n",
  911. "# sums = sample.sum(axis=0); print(\"axis=0:\\n\", sums, sums.shape)\n",
  912. "sums = sample.sum(axis=1); print(\"axis=1:\\n\", sums, sums.shape)\n",
  913. "sums = sample.sum(axis=2); print(\"axis=2:\\n\", sums, sums.shape)"
  914. ]
  915. },
  916. {
  917. "cell_type": "code",
  918. "execution_count": null,
  919. "metadata": {
  920. "collapsed": true,
  921. "scrolled": true,
  922. "slideshow": {
  923. "slide_type": "slide"
  924. }
  925. },
  926. "outputs": [],
  927. "source": []
  928. },
  929. {
  930. "cell_type": "code",
  931. "execution_count": null,
  932. "metadata": {
  933. "collapsed": true,
  934. "slideshow": {
  935. "slide_type": "slide"
  936. }
  937. },
  938. "outputs": [],
  939. "source": []
  940. }
  941. ],
  942. "metadata": {
  943. "celltoolbar": "Slideshow",
  944. "kernelspec": {
  945. "display_name": "Python 3",
  946. "language": "python",
  947. "name": "python3"
  948. },
  949. "language_info": {
  950. "codemirror_mode": {
  951. "name": "ipython",
  952. "version": 3
  953. },
  954. "file_extension": ".py",
  955. "mimetype": "text/x-python",
  956. "name": "python",
  957. "nbconvert_exporter": "python",
  958. "pygments_lexer": "ipython3",
  959. "version": "3.5.1"
  960. }
  961. },
  962. "nbformat": 4,
  963. "nbformat_minor": 1
  964. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement