Guest User

Untitled

a guest
Nov 18th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.03 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Numpy -Data Science Library Part 1\n"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "#### Creating Numpy arrays"
  15. ]
  16. },
  17. {
  18. "cell_type": "code",
  19. "execution_count": 1,
  20. "metadata": {
  21. "scrolled": true
  22. },
  23. "outputs": [
  24. {
  25. "name": "stdout",
  26. "output_type": "stream",
  27. "text": [
  28. "[0 1 2 3]\n"
  29. ]
  30. }
  31. ],
  32. "source": [
  33. "import numpy as np\n",
  34. "a=np.array([0,1,2,3])\n",
  35. "print(a)"
  36. ]
  37. },
  38. {
  39. "cell_type": "markdown",
  40. "metadata": {},
  41. "source": [
  42. "#### Dimensions of array"
  43. ]
  44. },
  45. {
  46. "cell_type": "code",
  47. "execution_count": 10,
  48. "metadata": {},
  49. "outputs": [
  50. {
  51. "data": {
  52. "text/plain": [
  53. "1"
  54. ]
  55. },
  56. "execution_count": 10,
  57. "metadata": {},
  58. "output_type": "execute_result"
  59. }
  60. ],
  61. "source": [
  62. "#print dimensions\n",
  63. "\n",
  64. "a.ndim"
  65. ]
  66. },
  67. {
  68. "cell_type": "code",
  69. "execution_count": 11,
  70. "metadata": {},
  71. "outputs": [
  72. {
  73. "data": {
  74. "text/plain": [
  75. "(4,)"
  76. ]
  77. },
  78. "execution_count": 11,
  79. "metadata": {},
  80. "output_type": "execute_result"
  81. }
  82. ],
  83. "source": [
  84. "#shape\n",
  85. "\n",
  86. "a.shape"
  87. ]
  88. },
  89. {
  90. "cell_type": "code",
  91. "execution_count": 19,
  92. "metadata": {},
  93. "outputs": [
  94. {
  95. "data": {
  96. "text/plain": [
  97. "4"
  98. ]
  99. },
  100. "execution_count": 19,
  101. "metadata": {},
  102. "output_type": "execute_result"
  103. }
  104. ],
  105. "source": [
  106. "len(a)"
  107. ]
  108. },
  109. {
  110. "cell_type": "code",
  111. "execution_count": 15,
  112. "metadata": {},
  113. "outputs": [
  114. {
  115. "data": {
  116. "text/plain": [
  117. "array([[0, 1, 2],\n",
  118. " [3, 4, 5]])"
  119. ]
  120. },
  121. "execution_count": 15,
  122. "metadata": {},
  123. "output_type": "execute_result"
  124. }
  125. ],
  126. "source": [
  127. "# 2-D, 3-D....\n",
  128. "\n",
  129. "b = np.array([[0, 1, 2], [3, 4, 5]])\n",
  130. "\n",
  131. "b"
  132. ]
  133. },
  134. {
  135. "cell_type": "code",
  136. "execution_count": 16,
  137. "metadata": {},
  138. "outputs": [
  139. {
  140. "data": {
  141. "text/plain": [
  142. "2"
  143. ]
  144. },
  145. "execution_count": 16,
  146. "metadata": {},
  147. "output_type": "execute_result"
  148. }
  149. ],
  150. "source": [
  151. "len(b)"
  152. ]
  153. },
  154. {
  155. "cell_type": "code",
  156. "execution_count": 17,
  157. "metadata": {},
  158. "outputs": [
  159. {
  160. "data": {
  161. "text/plain": [
  162. "(2, 3)"
  163. ]
  164. },
  165. "execution_count": 17,
  166. "metadata": {},
  167. "output_type": "execute_result"
  168. }
  169. ],
  170. "source": [
  171. "b.shape"
  172. ]
  173. },
  174. {
  175. "cell_type": "markdown",
  176. "metadata": {},
  177. "source": [
  178. "#### Functions for creating arrays"
  179. ]
  180. },
  181. {
  182. "cell_type": "markdown",
  183. "metadata": {},
  184. "source": [
  185. "###### Arange function is similar to range in python"
  186. ]
  187. },
  188. {
  189. "cell_type": "code",
  190. "execution_count": 6,
  191. "metadata": {},
  192. "outputs": [
  193. {
  194. "data": {
  195. "text/plain": [
  196. "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
  197. ]
  198. },
  199. "execution_count": 6,
  200. "metadata": {},
  201. "output_type": "execute_result"
  202. }
  203. ],
  204. "source": [
  205. "np.arange(10)"
  206. ]
  207. },
  208. {
  209. "cell_type": "markdown",
  210. "metadata": {},
  211. "source": [
  212. "Creates an array from 0 to 9"
  213. ]
  214. },
  215. {
  216. "cell_type": "code",
  217. "execution_count": 7,
  218. "metadata": {},
  219. "outputs": [
  220. {
  221. "data": {
  222. "text/plain": [
  223. "array([0, 2, 4, 6, 8])"
  224. ]
  225. },
  226. "execution_count": 7,
  227. "metadata": {},
  228. "output_type": "execute_result"
  229. }
  230. ],
  231. "source": [
  232. "b=np.arange(0,10,2)\n",
  233. "b"
  234. ]
  235. },
  236. {
  237. "cell_type": "markdown",
  238. "metadata": {},
  239. "source": [
  240. "Function signature is arange(start,end, step_size)"
  241. ]
  242. },
  243. {
  244. "cell_type": "markdown",
  245. "metadata": {},
  246. "source": [
  247. "###### Linspace"
  248. ]
  249. },
  250. {
  251. "cell_type": "code",
  252. "execution_count": 13,
  253. "metadata": {},
  254. "outputs": [
  255. {
  256. "data": {
  257. "text/plain": [
  258. "array([0. , 0.2, 0.4, 0.6, 0.8, 1. ])"
  259. ]
  260. },
  261. "execution_count": 13,
  262. "metadata": {},
  263. "output_type": "execute_result"
  264. }
  265. ],
  266. "source": [
  267. "a=np.linspace(0,1,6)\n",
  268. "a"
  269. ]
  270. },
  271. {
  272. "cell_type": "markdown",
  273. "metadata": {},
  274. "source": [
  275. " Function signature is np.linspace(start,end,number_of_points_required)"
  276. ]
  277. },
  278. {
  279. "cell_type": "markdown",
  280. "metadata": {},
  281. "source": [
  282. "##### Zeros"
  283. ]
  284. },
  285. {
  286. "cell_type": "code",
  287. "execution_count": 15,
  288. "metadata": {},
  289. "outputs": [
  290. {
  291. "data": {
  292. "text/plain": [
  293. "array([0., 0., 0., 0., 0.])"
  294. ]
  295. },
  296. "execution_count": 15,
  297. "metadata": {},
  298. "output_type": "execute_result"
  299. }
  300. ],
  301. "source": [
  302. "np.zeros(5)"
  303. ]
  304. },
  305. {
  306. "cell_type": "code",
  307. "execution_count": 17,
  308. "metadata": {},
  309. "outputs": [
  310. {
  311. "data": {
  312. "text/plain": [
  313. "array([[0., 0., 0.],\n",
  314. " [0., 0., 0.]])"
  315. ]
  316. },
  317. "execution_count": 17,
  318. "metadata": {},
  319. "output_type": "execute_result"
  320. }
  321. ],
  322. "source": [
  323. "np.zeros((2,3))"
  324. ]
  325. },
  326. {
  327. "cell_type": "markdown",
  328. "metadata": {},
  329. "source": [
  330. "##### Ones"
  331. ]
  332. },
  333. {
  334. "cell_type": "code",
  335. "execution_count": 18,
  336. "metadata": {},
  337. "outputs": [
  338. {
  339. "data": {
  340. "text/plain": [
  341. "array([[1., 1., 1.],\n",
  342. " [1., 1., 1.],\n",
  343. " [1., 1., 1.]])"
  344. ]
  345. },
  346. "execution_count": 18,
  347. "metadata": {},
  348. "output_type": "execute_result"
  349. }
  350. ],
  351. "source": [
  352. "np.ones((3,3))"
  353. ]
  354. },
  355. {
  356. "cell_type": "markdown",
  357. "metadata": {},
  358. "source": [
  359. "##### Identity Matrix"
  360. ]
  361. },
  362. {
  363. "cell_type": "code",
  364. "execution_count": 19,
  365. "metadata": {},
  366. "outputs": [
  367. {
  368. "data": {
  369. "text/plain": [
  370. "array([[1., 0., 0.],\n",
  371. " [0., 1., 0.],\n",
  372. " [0., 0., 1.]])"
  373. ]
  374. },
  375. "execution_count": 19,
  376. "metadata": {},
  377. "output_type": "execute_result"
  378. }
  379. ],
  380. "source": [
  381. "np.eye(3)"
  382. ]
  383. },
  384. {
  385. "cell_type": "code",
  386. "execution_count": 20,
  387. "metadata": {},
  388. "outputs": [
  389. {
  390. "data": {
  391. "text/plain": [
  392. "array([[1., 0.],\n",
  393. " [0., 1.],\n",
  394. " [0., 0.]])"
  395. ]
  396. },
  397. "execution_count": 20,
  398. "metadata": {},
  399. "output_type": "execute_result"
  400. }
  401. ],
  402. "source": [
  403. "d = np.eye(3, 2) #3 is number of rows, 2 is number of columns, index of diagonal start with 0\n",
  404. "\n",
  405. "d"
  406. ]
  407. },
  408. {
  409. "cell_type": "markdown",
  410. "metadata": {},
  411. "source": [
  412. "##### Diagonal Matrix"
  413. ]
  414. },
  415. {
  416. "cell_type": "code",
  417. "execution_count": 23,
  418. "metadata": {},
  419. "outputs": [
  420. {
  421. "data": {
  422. "text/plain": [
  423. "array([[1, 0, 0, 0],\n",
  424. " [0, 2, 0, 0],\n",
  425. " [0, 0, 3, 0],\n",
  426. " [0, 0, 0, 4]])"
  427. ]
  428. },
  429. "execution_count": 23,
  430. "metadata": {},
  431. "output_type": "execute_result"
  432. }
  433. ],
  434. "source": [
  435. "a=np.diag([1,2,3,4])\n",
  436. "a"
  437. ]
  438. },
  439. {
  440. "cell_type": "code",
  441. "execution_count": 25,
  442. "metadata": {},
  443. "outputs": [
  444. {
  445. "data": {
  446. "text/plain": [
  447. "array([1, 2, 3, 4])"
  448. ]
  449. },
  450. "execution_count": 25,
  451. "metadata": {},
  452. "output_type": "execute_result"
  453. }
  454. ],
  455. "source": [
  456. "np.diag(a)#extract the diagonal matrix"
  457. ]
  458. },
  459. {
  460. "cell_type": "markdown",
  461. "metadata": {},
  462. "source": [
  463. "##### Random Arrays"
  464. ]
  465. },
  466. {
  467. "cell_type": "code",
  468. "execution_count": 26,
  469. "metadata": {},
  470. "outputs": [
  471. {
  472. "data": {
  473. "text/plain": [
  474. "array([0.38095827, 0.05454193, 0.5649424 , 0.35600484])"
  475. ]
  476. },
  477. "execution_count": 26,
  478. "metadata": {},
  479. "output_type": "execute_result"
  480. }
  481. ],
  482. "source": [
  483. "#create array using random\n",
  484. "\n",
  485. "#Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).\n",
  486. "a = np.random.rand(4) \n",
  487. "\n",
  488. "a"
  489. ]
  490. },
  491. {
  492. "cell_type": "code",
  493. "execution_count": 27,
  494. "metadata": {},
  495. "outputs": [
  496. {
  497. "data": {
  498. "text/plain": [
  499. "array([ 0.288684 , -0.32566062, 1.09628212, -0.30523579])"
  500. ]
  501. },
  502. "execution_count": 27,
  503. "metadata": {},
  504. "output_type": "execute_result"
  505. }
  506. ],
  507. "source": [
  508. "a = np.random.randn(4)#Return a sample (or samples) from the “standard normal” distribution. ***Gausian***\n",
  509. "\n",
  510. "a"
  511. ]
  512. },
  513. {
  514. "cell_type": "markdown",
  515. "metadata": {},
  516. "source": [
  517. "##### Note: Numpy arrays are faster than normal python lists"
  518. ]
  519. },
  520. {
  521. "cell_type": "code",
  522. "execution_count": 2,
  523. "metadata": {},
  524. "outputs": [
  525. {
  526. "name": "stdout",
  527. "output_type": "stream",
  528. "text": [
  529. "277 µs ± 6.15 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
  530. ]
  531. }
  532. ],
  533. "source": [
  534. "L = range(1000)\n",
  535. "%timeit [i**2 for i in L]"
  536. ]
  537. },
  538. {
  539. "cell_type": "code",
  540. "execution_count": 5,
  541. "metadata": {},
  542. "outputs": [
  543. {
  544. "name": "stdout",
  545. "output_type": "stream",
  546. "text": [
  547. "916 ns ± 14.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
  548. ]
  549. }
  550. ],
  551. "source": [
  552. "a = np.arange(1000)\n",
  553. "%timeit a**2"
  554. ]
  555. },
  556. {
  557. "cell_type": "markdown",
  558. "metadata": {},
  559. "source": [
  560. "### Data Types"
  561. ]
  562. },
  563. {
  564. "cell_type": "code",
  565. "execution_count": 29,
  566. "metadata": {},
  567. "outputs": [
  568. {
  569. "name": "stdout",
  570. "output_type": "stream",
  571. "text": [
  572. "[0 1 2 3 4 5 6 7 8 9]\n"
  573. ]
  574. },
  575. {
  576. "data": {
  577. "text/plain": [
  578. "dtype('int64')"
  579. ]
  580. },
  581. "execution_count": 29,
  582. "metadata": {},
  583. "output_type": "execute_result"
  584. }
  585. ],
  586. "source": [
  587. "a=np.arange(10)\n",
  588. "print(a)\n",
  589. "a.dtype"
  590. ]
  591. },
  592. {
  593. "cell_type": "code",
  594. "execution_count": 31,
  595. "metadata": {},
  596. "outputs": [
  597. {
  598. "name": "stdout",
  599. "output_type": "stream",
  600. "text": [
  601. "[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n"
  602. ]
  603. },
  604. {
  605. "data": {
  606. "text/plain": [
  607. "dtype('float64')"
  608. ]
  609. },
  610. "execution_count": 31,
  611. "metadata": {},
  612. "output_type": "execute_result"
  613. }
  614. ],
  615. "source": [
  616. "#You can explicitly specify which data-type you want:\n",
  617. "\n",
  618. "a = np.arange(10, dtype='float64')\n",
  619. "print(a)\n",
  620. "a.dtype"
  621. ]
  622. },
  623. {
  624. "cell_type": "code",
  625. "execution_count": 32,
  626. "metadata": {},
  627. "outputs": [
  628. {
  629. "name": "stdout",
  630. "output_type": "stream",
  631. "text": [
  632. "[[0. 0. 0.]\n",
  633. " [0. 0. 0.]\n",
  634. " [0. 0. 0.]]\n"
  635. ]
  636. },
  637. {
  638. "data": {
  639. "text/plain": [
  640. "dtype('float64')"
  641. ]
  642. },
  643. "execution_count": 32,
  644. "metadata": {},
  645. "output_type": "execute_result"
  646. }
  647. ],
  648. "source": [
  649. "#The default data type is float for zeros and ones function\n",
  650. "\n",
  651. "a = np.zeros((3, 3))\n",
  652. "\n",
  653. "print(a)\n",
  654. "\n",
  655. "a.dtype"
  656. ]
  657. },
  658. {
  659. "cell_type": "markdown",
  660. "metadata": {},
  661. "source": [
  662. "**Each built-in data type has a character code that uniquely identifies it.**\n",
  663. "\n",
  664. "'b' − boolean\n",
  665. "\n",
  666. "'i' − (signed) integer\n",
  667. "\n",
  668. "'u' − unsigned integer\n",
  669. "\n",
  670. "'f' − floating-point\n",
  671. "\n",
  672. "'c' − complex-floating point\n",
  673. "\n",
  674. "'m' − timedelta\n",
  675. "\n",
  676. "'M' − datetime\n",
  677. "\n",
  678. "'O' − (Python) objects\n",
  679. "\n",
  680. "'S', 'a' − (byte-)string\n",
  681. "\n",
  682. "'U' − Unicode\n",
  683. "\n",
  684. "'V' − raw data (void)"
  685. ]
  686. },
  687. {
  688. "cell_type": "code",
  689. "execution_count": 34,
  690. "metadata": {},
  691. "outputs": [
  692. {
  693. "name": "stdout",
  694. "output_type": "stream",
  695. "text": [
  696. "complex128\n"
  697. ]
  698. }
  699. ],
  700. "source": [
  701. "d = np.array([1+2j, 2+4j]) #Complex datatype\n",
  702. "\n",
  703. "print(d.dtype)"
  704. ]
  705. },
  706. {
  707. "cell_type": "code",
  708. "execution_count": 35,
  709. "metadata": {},
  710. "outputs": [
  711. {
  712. "name": "stdout",
  713. "output_type": "stream",
  714. "text": [
  715. "bool\n"
  716. ]
  717. }
  718. ],
  719. "source": [
  720. "b = np.array([True, False, True, False]) #Boolean datatype\n",
  721. "\n",
  722. "print(b.dtype)"
  723. ]
  724. },
  725. {
  726. "cell_type": "code",
  727. "execution_count": 36,
  728. "metadata": {},
  729. "outputs": [
  730. {
  731. "data": {
  732. "text/plain": [
  733. "dtype('<U6')"
  734. ]
  735. },
  736. "execution_count": 36,
  737. "metadata": {},
  738. "output_type": "execute_result"
  739. }
  740. ],
  741. "source": [
  742. "s = np.array(['Ram', 'Robert', 'Rahim'])\n",
  743. "\n",
  744. "s.dtype"
  745. ]
  746. },
  747. {
  748. "cell_type": "markdown",
  749. "metadata": {},
  750. "source": [
  751. "### Indexing and slicing an array"
  752. ]
  753. },
  754. {
  755. "cell_type": "code",
  756. "execution_count": 37,
  757. "metadata": {},
  758. "outputs": [
  759. {
  760. "name": "stdout",
  761. "output_type": "stream",
  762. "text": [
  763. "[0 1 2 3 4 5 6 7 8 9]\n"
  764. ]
  765. },
  766. {
  767. "data": {
  768. "text/plain": [
  769. "5"
  770. ]
  771. },
  772. "execution_count": 37,
  773. "metadata": {},
  774. "output_type": "execute_result"
  775. }
  776. ],
  777. "source": [
  778. "a=np.arange(10)\n",
  779. "print(a)\n",
  780. "a[5]"
  781. ]
  782. },
  783. {
  784. "cell_type": "markdown",
  785. "metadata": {},
  786. "source": [
  787. "indices begin at 0, like other python arrays"
  788. ]
  789. },
  790. {
  791. "cell_type": "code",
  792. "execution_count": 40,
  793. "metadata": {},
  794. "outputs": [
  795. {
  796. "name": "stdout",
  797. "output_type": "stream",
  798. "text": [
  799. "3\n"
  800. ]
  801. }
  802. ],
  803. "source": [
  804. "# For multidimensional arrays, indexes are tuples of integers:\n",
  805. "\n",
  806. "a = np.diag([1, 2, 3])\n",
  807. "\n",
  808. "print(a[2, 2])"
  809. ]
  810. },
  811. {
  812. "cell_type": "code",
  813. "execution_count": 41,
  814. "metadata": {},
  815. "outputs": [
  816. {
  817. "data": {
  818. "text/plain": [
  819. "array([[1, 0, 0],\n",
  820. " [0, 2, 0],\n",
  821. " [0, 5, 3]])"
  822. ]
  823. },
  824. "execution_count": 41,
  825. "metadata": {},
  826. "output_type": "execute_result"
  827. }
  828. ],
  829. "source": [
  830. "a[2, 1] = 5 #assigning value\n",
  831. "\n",
  832. "a"
  833. ]
  834. },
  835. {
  836. "cell_type": "code",
  837. "execution_count": 46,
  838. "metadata": {},
  839. "outputs": [
  840. {
  841. "data": {
  842. "text/plain": [
  843. "array([1, 3, 5, 7])"
  844. ]
  845. },
  846. "execution_count": 46,
  847. "metadata": {},
  848. "output_type": "execute_result"
  849. }
  850. ],
  851. "source": [
  852. "# slicing\n",
  853. "a = np.arange(10)\n",
  854. "\n",
  855. "a[1:8:2]"
  856. ]
  857. },
  858. {
  859. "cell_type": "markdown",
  860. "metadata": {},
  861. "source": [
  862. "It is kind of like syaing get me all values from index 1 to 7 at intervals of 2..which gets the odd numbers"
  863. ]
  864. },
  865. {
  866. "cell_type": "code",
  867. "execution_count": 47,
  868. "metadata": {},
  869. "outputs": [
  870. {
  871. "data": {
  872. "text/plain": [
  873. "array([ 0, 1, 2, 3, 4, 10, 10, 10, 10, 10])"
  874. ]
  875. },
  876. "execution_count": 47,
  877. "metadata": {},
  878. "output_type": "execute_result"
  879. }
  880. ],
  881. "source": [
  882. "#we can also combine assignment and slicing:\n",
  883. "\n",
  884. "a = np.arange(10)\n",
  885. "a[5:] = 10\n",
  886. "a"
  887. ]
  888. },
  889. {
  890. "cell_type": "markdown",
  891. "metadata": {},
  892. "source": [
  893. "### Sharing memory"
  894. ]
  895. },
  896. {
  897. "cell_type": "code",
  898. "execution_count": 49,
  899. "metadata": {},
  900. "outputs": [
  901. {
  902. "data": {
  903. "text/plain": [
  904. "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
  905. ]
  906. },
  907. "execution_count": 49,
  908. "metadata": {},
  909. "output_type": "execute_result"
  910. }
  911. ],
  912. "source": [
  913. "a=np.arange(10)\n",
  914. "a"
  915. ]
  916. },
  917. {
  918. "cell_type": "code",
  919. "execution_count": 62,
  920. "metadata": {},
  921. "outputs": [
  922. {
  923. "data": {
  924. "text/plain": [
  925. "array([0, 2, 4, 6, 8])"
  926. ]
  927. },
  928. "execution_count": 62,
  929. "metadata": {},
  930. "output_type": "execute_result"
  931. }
  932. ],
  933. "source": [
  934. "b=a[::2]\n",
  935. "b\n"
  936. ]
  937. },
  938. {
  939. "cell_type": "code",
  940. "execution_count": 63,
  941. "metadata": {},
  942. "outputs": [
  943. {
  944. "data": {
  945. "text/plain": [
  946. "False"
  947. ]
  948. },
  949. "execution_count": 63,
  950. "metadata": {},
  951. "output_type": "execute_result"
  952. }
  953. ],
  954. "source": [
  955. "id(a)==id(b)\n"
  956. ]
  957. },
  958. {
  959. "cell_type": "code",
  960. "execution_count": 64,
  961. "metadata": {},
  962. "outputs": [
  963. {
  964. "data": {
  965. "text/plain": [
  966. "4548358352"
  967. ]
  968. },
  969. "execution_count": 64,
  970. "metadata": {},
  971. "output_type": "execute_result"
  972. }
  973. ],
  974. "source": [
  975. "id(a)"
  976. ]
  977. },
  978. {
  979. "cell_type": "code",
  980. "execution_count": 65,
  981. "metadata": {},
  982. "outputs": [
  983. {
  984. "data": {
  985. "text/plain": [
  986. "4548359632"
  987. ]
  988. },
  989. "execution_count": 65,
  990. "metadata": {},
  991. "output_type": "execute_result"
  992. }
  993. ],
  994. "source": [
  995. "id(b)"
  996. ]
  997. },
  998. {
  999. "cell_type": "code",
  1000. "execution_count": 66,
  1001. "metadata": {},
  1002. "outputs": [
  1003. {
  1004. "data": {
  1005. "text/plain": [
  1006. "True"
  1007. ]
  1008. },
  1009. "execution_count": 66,
  1010. "metadata": {},
  1011. "output_type": "execute_result"
  1012. }
  1013. ],
  1014. "source": [
  1015. "np.shares_memory(a,b)"
  1016. ]
  1017. },
  1018. {
  1019. "cell_type": "code",
  1020. "execution_count": 67,
  1021. "metadata": {},
  1022. "outputs": [
  1023. {
  1024. "data": {
  1025. "text/plain": [
  1026. "array([10, 2, 4, 6, 8])"
  1027. ]
  1028. },
  1029. "execution_count": 67,
  1030. "metadata": {},
  1031. "output_type": "execute_result"
  1032. }
  1033. ],
  1034. "source": [
  1035. "b[0]=10\n",
  1036. "b"
  1037. ]
  1038. },
  1039. {
  1040. "cell_type": "code",
  1041. "execution_count": 68,
  1042. "metadata": {},
  1043. "outputs": [
  1044. {
  1045. "name": "stdout",
  1046. "output_type": "stream",
  1047. "text": [
  1048. "[10 1 2 3 4 5 6 7 8 9]\n"
  1049. ]
  1050. }
  1051. ],
  1052. "source": [
  1053. "print(a)"
  1054. ]
  1055. },
  1056. {
  1057. "cell_type": "markdown",
  1058. "metadata": {},
  1059. "source": [
  1060. "**Change in b causes change in a even though we modified only b**"
  1061. ]
  1062. },
  1063. {
  1064. "cell_type": "code",
  1065. "execution_count": 69,
  1066. "metadata": {},
  1067. "outputs": [
  1068. {
  1069. "data": {
  1070. "text/plain": [
  1071. "array([0, 2, 4, 6, 8])"
  1072. ]
  1073. },
  1074. "execution_count": 69,
  1075. "metadata": {},
  1076. "output_type": "execute_result"
  1077. }
  1078. ],
  1079. "source": [
  1080. "a = np.arange(10)\n",
  1081. "\n",
  1082. "c = a[::2].copy() #force a copy\n",
  1083. "c"
  1084. ]
  1085. },
  1086. {
  1087. "cell_type": "code",
  1088. "execution_count": 70,
  1089. "metadata": {},
  1090. "outputs": [
  1091. {
  1092. "data": {
  1093. "text/plain": [
  1094. "False"
  1095. ]
  1096. },
  1097. "execution_count": 70,
  1098. "metadata": {},
  1099. "output_type": "execute_result"
  1100. }
  1101. ],
  1102. "source": [
  1103. "np.shares_memory(a, c)"
  1104. ]
  1105. },
  1106. {
  1107. "cell_type": "code",
  1108. "execution_count": 71,
  1109. "metadata": {},
  1110. "outputs": [
  1111. {
  1112. "data": {
  1113. "text/plain": [
  1114. "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
  1115. ]
  1116. },
  1117. "execution_count": 71,
  1118. "metadata": {},
  1119. "output_type": "execute_result"
  1120. }
  1121. ],
  1122. "source": [
  1123. "c[0] = 10\n",
  1124. "\n",
  1125. "a"
  1126. ]
  1127. },
  1128. {
  1129. "cell_type": "markdown",
  1130. "metadata": {},
  1131. "source": [
  1132. "##### Indexing using masks"
  1133. ]
  1134. },
  1135. {
  1136. "cell_type": "code",
  1137. "execution_count": 72,
  1138. "metadata": {},
  1139. "outputs": [],
  1140. "source": [
  1141. "a=np.random.randint(0,20,15)"
  1142. ]
  1143. },
  1144. {
  1145. "cell_type": "code",
  1146. "execution_count": 73,
  1147. "metadata": {},
  1148. "outputs": [
  1149. {
  1150. "data": {
  1151. "text/plain": [
  1152. "array([ 2, 1, 4, 0, 15, 10, 16, 9, 7, 7, 7, 2, 3, 14, 4])"
  1153. ]
  1154. },
  1155. "execution_count": 73,
  1156. "metadata": {},
  1157. "output_type": "execute_result"
  1158. }
  1159. ],
  1160. "source": [
  1161. "a"
  1162. ]
  1163. },
  1164. {
  1165. "cell_type": "code",
  1166. "execution_count": 75,
  1167. "metadata": {},
  1168. "outputs": [
  1169. {
  1170. "data": {
  1171. "text/plain": [
  1172. "array([ True, False, True, True, False, True, True, False, False,\n",
  1173. " False, False, True, False, True, True])"
  1174. ]
  1175. },
  1176. "execution_count": 75,
  1177. "metadata": {},
  1178. "output_type": "execute_result"
  1179. }
  1180. ],
  1181. "source": [
  1182. "mask=(a%2==0)\n",
  1183. "mask"
  1184. ]
  1185. },
  1186. {
  1187. "cell_type": "code",
  1188. "execution_count": 77,
  1189. "metadata": {},
  1190. "outputs": [
  1191. {
  1192. "data": {
  1193. "text/plain": [
  1194. "array([ 2, 4, 0, 10, 16, 2, 14, 4])"
  1195. ]
  1196. },
  1197. "execution_count": 77,
  1198. "metadata": {},
  1199. "output_type": "execute_result"
  1200. }
  1201. ],
  1202. "source": [
  1203. "extract_from_a=a[mask]\n",
  1204. "extract_from_a"
  1205. ]
  1206. },
  1207. {
  1208. "cell_type": "markdown",
  1209. "metadata": {},
  1210. "source": [
  1211. "##### Indexing with an array of Integers"
  1212. ]
  1213. },
  1214. {
  1215. "cell_type": "code",
  1216. "execution_count": 78,
  1217. "metadata": {},
  1218. "outputs": [
  1219. {
  1220. "data": {
  1221. "text/plain": [
  1222. "array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])"
  1223. ]
  1224. },
  1225. "execution_count": 78,
  1226. "metadata": {},
  1227. "output_type": "execute_result"
  1228. }
  1229. ],
  1230. "source": [
  1231. "a = np.arange(0, 100, 10)\n",
  1232. "\n",
  1233. "a"
  1234. ]
  1235. },
  1236. {
  1237. "cell_type": "code",
  1238. "execution_count": 79,
  1239. "metadata": {},
  1240. "outputs": [
  1241. {
  1242. "data": {
  1243. "text/plain": [
  1244. "array([20, 30, 20, 40, 20])"
  1245. ]
  1246. },
  1247. "execution_count": 79,
  1248. "metadata": {},
  1249. "output_type": "execute_result"
  1250. }
  1251. ],
  1252. "source": [
  1253. "#Indexing can be done with an array of integers, where the same index is repeated several time:\n",
  1254. "\n",
  1255. "a[[2, 3, 2, 4, 2]]"
  1256. ]
  1257. },
  1258. {
  1259. "cell_type": "code",
  1260. "execution_count": 80,
  1261. "metadata": {},
  1262. "outputs": [
  1263. {
  1264. "data": {
  1265. "text/plain": [
  1266. "array([ 0, 10, 20, 30, 40, 50, 60, -200, 80, -200])"
  1267. ]
  1268. },
  1269. "execution_count": 80,
  1270. "metadata": {},
  1271. "output_type": "execute_result"
  1272. }
  1273. ],
  1274. "source": [
  1275. "# New values can be assigned \n",
  1276. "\n",
  1277. "a[[9, 7]] = -200\n",
  1278. "\n",
  1279. "a"
  1280. ]
  1281. },
  1282. {
  1283. "cell_type": "code",
  1284. "execution_count": null,
  1285. "metadata": {},
  1286. "outputs": [],
  1287. "source": []
  1288. }
  1289. ],
  1290. "metadata": {
  1291. "kernelspec": {
  1292. "display_name": "Python 3",
  1293. "language": "python",
  1294. "name": "python3"
  1295. },
  1296. "language_info": {
  1297. "codemirror_mode": {
  1298. "name": "ipython",
  1299. "version": 3
  1300. },
  1301. "file_extension": ".py",
  1302. "mimetype": "text/x-python",
  1303. "name": "python",
  1304. "nbconvert_exporter": "python",
  1305. "pygments_lexer": "ipython3",
  1306. "version": "3.7.0"
  1307. }
  1308. },
  1309. "nbformat": 4,
  1310. "nbformat_minor": 1
  1311. }
Add Comment
Please, Sign In to add comment