Arrays.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. ---
  2. redirect_from:
  3. - "/chapters/05/1/arrays"
  4. interact_link: content/chapters/05/1/Arrays.ipynb
  5. kernel_name: python3
  6. has_widgets: false
  7. title: |-
  8. Arrays
  9. prev_page:
  10. url: /chapters/05/Sequences.html
  11. title: |-
  12. Sequences
  13. next_page:
  14. url: /chapters/05/2/Ranges.html
  15. title: |-
  16. Ranges
  17. comment: "***PROGRAMMATICALLY GENERATED, DO NOT EDIT. SEE ORIGINAL FILES IN /content***"
  18. ---
  19. <div class="jb_cell tag_remove_input">
  20. <div class="cell border-box-sizing code_cell rendered">
  21. </div>
  22. </div>
  23. <div class="jb_cell">
  24. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  25. <div class="text_cell_render border-box-sizing rendered_html">
  26. <h1 id="Arrays">Arrays<a class="anchor-link" href="#Arrays"> </a></h1><p>While there are many kinds of collections in Python, we will work primarily with arrays in this class. We've already seen that the <code>make_array</code> function can be used to create arrays of numbers.</p>
  27. <p>Arrays can also contain strings or other types of values, but a single array can only contain a single kind of data. (It usually doesn't make sense to group together unlike data anyway.) For example:</p>
  28. </div>
  29. </div>
  30. </div>
  31. </div>
  32. <div class="jb_cell">
  33. <div class="cell border-box-sizing code_cell rendered">
  34. <div class="input">
  35. <div class="inner_cell">
  36. <div class="input_area">
  37. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">english_parts_of_speech</span> <span class="o">=</span> <span class="n">make_array</span><span class="p">(</span><span class="s2">&quot;noun&quot;</span><span class="p">,</span> <span class="s2">&quot;pronoun&quot;</span><span class="p">,</span> <span class="s2">&quot;verb&quot;</span><span class="p">,</span> <span class="s2">&quot;adverb&quot;</span><span class="p">,</span> <span class="s2">&quot;adjective&quot;</span><span class="p">,</span> <span class="s2">&quot;conjunction&quot;</span><span class="p">,</span> <span class="s2">&quot;preposition&quot;</span><span class="p">,</span> <span class="s2">&quot;interjection&quot;</span><span class="p">)</span>
  38. <span class="n">english_parts_of_speech</span>
  39. </pre></div>
  40. </div>
  41. </div>
  42. </div>
  43. <div class="output_wrapper">
  44. <div class="output">
  45. <div class="jb_output_wrapper }}">
  46. <div class="output_area">
  47. <div class="output_text output_subarea output_execute_result">
  48. <pre>array([&#39;noun&#39;, &#39;pronoun&#39;, &#39;verb&#39;, &#39;adverb&#39;, &#39;adjective&#39;, &#39;conjunction&#39;,
  49. &#39;preposition&#39;, &#39;interjection&#39;], dtype=&#39;&lt;U12&#39;)</pre>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. </div>
  57. <div class="jb_cell">
  58. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  59. <div class="text_cell_render border-box-sizing rendered_html">
  60. <p>Returning to the temperature data, we create arrays of average daily <a href="http://berkeleyearth.lbl.gov/auto/Regional/TMAX/Text/global-land-TMAX-Trend.txt">high temperatures</a> for the decades surrounding 1850, 1900, 1950, and 2000.</p>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. <div class="jb_cell">
  66. <div class="cell border-box-sizing code_cell rendered">
  67. <div class="input">
  68. <div class="inner_cell">
  69. <div class="input_area">
  70. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">baseline_high</span> <span class="o">=</span> <span class="mf">14.48</span>
  71. <span class="n">highs</span> <span class="o">=</span> <span class="n">make_array</span><span class="p">(</span><span class="n">baseline_high</span> <span class="o">-</span> <span class="mf">0.880</span><span class="p">,</span>
  72. <span class="n">baseline_high</span> <span class="o">-</span> <span class="mf">0.093</span><span class="p">,</span>
  73. <span class="n">baseline_high</span> <span class="o">+</span> <span class="mf">0.105</span><span class="p">,</span>
  74. <span class="n">baseline_high</span> <span class="o">+</span> <span class="mf">0.684</span><span class="p">)</span>
  75. <span class="n">highs</span>
  76. </pre></div>
  77. </div>
  78. </div>
  79. </div>
  80. <div class="output_wrapper">
  81. <div class="output">
  82. <div class="jb_output_wrapper }}">
  83. <div class="output_area">
  84. <div class="output_text output_subarea output_execute_result">
  85. <pre>array([13.6 , 14.387, 14.585, 15.164])</pre>
  86. </div>
  87. </div>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. <div class="jb_cell">
  94. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  95. <div class="text_cell_render border-box-sizing rendered_html">
  96. <p>Arrays can be used in arithmetic expressions to compute over their contents. When an array is combined with a single number, that number is combined with each element of the array. Therefore, we can convert all of these temperatures to Fahrenheit by writing the familiar conversion formula.</p>
  97. </div>
  98. </div>
  99. </div>
  100. </div>
  101. <div class="jb_cell">
  102. <div class="cell border-box-sizing code_cell rendered">
  103. <div class="input">
  104. <div class="inner_cell">
  105. <div class="input_area">
  106. <div class=" highlight hl-ipython3"><pre><span></span><span class="p">(</span><span class="mi">9</span><span class="o">/</span><span class="mi">5</span><span class="p">)</span> <span class="o">*</span> <span class="n">highs</span> <span class="o">+</span> <span class="mi">32</span>
  107. </pre></div>
  108. </div>
  109. </div>
  110. </div>
  111. <div class="output_wrapper">
  112. <div class="output">
  113. <div class="jb_output_wrapper }}">
  114. <div class="output_area">
  115. <div class="output_text output_subarea output_execute_result">
  116. <pre>array([56.48 , 57.8966, 58.253 , 59.2952])</pre>
  117. </div>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123. </div>
  124. <div class="jb_cell">
  125. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  126. <div class="text_cell_render border-box-sizing rendered_html">
  127. <p><img src="../../../images/array_arithmetic.png" /></p>
  128. </div>
  129. </div>
  130. </div>
  131. </div>
  132. <div class="jb_cell">
  133. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  134. <div class="text_cell_render border-box-sizing rendered_html">
  135. <p>Arrays also have <em>methods</em>, which are functions that operate on the array values. The <code>mean</code> of a collection of numbers is its average value: the sum divided by the length. Each pair of parentheses in the examples below is part of a call expression; it's calling a function with no arguments to perform a computation on the array called <code>highs</code>.</p>
  136. </div>
  137. </div>
  138. </div>
  139. </div>
  140. <div class="jb_cell">
  141. <div class="cell border-box-sizing code_cell rendered">
  142. <div class="input">
  143. <div class="inner_cell">
  144. <div class="input_area">
  145. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">highs</span><span class="o">.</span><span class="n">size</span>
  146. </pre></div>
  147. </div>
  148. </div>
  149. </div>
  150. <div class="output_wrapper">
  151. <div class="output">
  152. <div class="jb_output_wrapper }}">
  153. <div class="output_area">
  154. <div class="output_text output_subarea output_execute_result">
  155. <pre>4</pre>
  156. </div>
  157. </div>
  158. </div>
  159. </div>
  160. </div>
  161. </div>
  162. </div>
  163. <div class="jb_cell">
  164. <div class="cell border-box-sizing code_cell rendered">
  165. <div class="input">
  166. <div class="inner_cell">
  167. <div class="input_area">
  168. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">highs</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
  169. </pre></div>
  170. </div>
  171. </div>
  172. </div>
  173. <div class="output_wrapper">
  174. <div class="output">
  175. <div class="jb_output_wrapper }}">
  176. <div class="output_area">
  177. <div class="output_text output_subarea output_execute_result">
  178. <pre>57.736000000000004</pre>
  179. </div>
  180. </div>
  181. </div>
  182. </div>
  183. </div>
  184. </div>
  185. </div>
  186. <div class="jb_cell">
  187. <div class="cell border-box-sizing code_cell rendered">
  188. <div class="input">
  189. <div class="inner_cell">
  190. <div class="input_area">
  191. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">highs</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
  192. </pre></div>
  193. </div>
  194. </div>
  195. </div>
  196. <div class="output_wrapper">
  197. <div class="output">
  198. <div class="jb_output_wrapper }}">
  199. <div class="output_area">
  200. <div class="output_text output_subarea output_execute_result">
  201. <pre>14.434000000000001</pre>
  202. </div>
  203. </div>
  204. </div>
  205. </div>
  206. </div>
  207. </div>
  208. </div>
  209. <div class="jb_cell">
  210. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  211. <div class="text_cell_render border-box-sizing rendered_html">
  212. <h4 id="Functions-on-Arrays">Functions on Arrays<a class="anchor-link" href="#Functions-on-Arrays"> </a></h4><p>The <code>numpy</code> package, abbreviated <code>np</code> in programs, provides Python programmers with convenient and powerful functions for creating and manipulating arrays.</p>
  213. </div>
  214. </div>
  215. </div>
  216. </div>
  217. <div class="jb_cell">
  218. <div class="cell border-box-sizing code_cell rendered">
  219. <div class="input">
  220. <div class="inner_cell">
  221. <div class="input_area">
  222. <div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
  223. </pre></div>
  224. </div>
  225. </div>
  226. </div>
  227. </div>
  228. </div>
  229. <div class="jb_cell">
  230. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  231. <div class="text_cell_render border-box-sizing rendered_html">
  232. <p>For example, the <code>diff</code> function computes the difference between each adjacent pair of elements in an array. The first element of the <code>diff</code> is the second element minus the first.</p>
  233. </div>
  234. </div>
  235. </div>
  236. </div>
  237. <div class="jb_cell">
  238. <div class="cell border-box-sizing code_cell rendered">
  239. <div class="input">
  240. <div class="inner_cell">
  241. <div class="input_area">
  242. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">np</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">highs</span><span class="p">)</span>
  243. </pre></div>
  244. </div>
  245. </div>
  246. </div>
  247. <div class="output_wrapper">
  248. <div class="output">
  249. <div class="jb_output_wrapper }}">
  250. <div class="output_area">
  251. <div class="output_text output_subarea output_execute_result">
  252. <pre>array([0.787, 0.198, 0.579])</pre>
  253. </div>
  254. </div>
  255. </div>
  256. </div>
  257. </div>
  258. </div>
  259. </div>
  260. <div class="jb_cell">
  261. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  262. <div class="text_cell_render border-box-sizing rendered_html">
  263. <p>The <a href="http://docs.scipy.org/doc/numpy/reference/">full Numpy reference</a> lists these functions exhaustively, but only a small subset are used commonly for data processing applications. These are grouped into different packages within <code>np</code>. Learning this vocabulary is an important part of learning the Python language, so refer back to this list often as you work through examples and problems.</p>
  264. <p>However, you <strong>don't need to memorize these</strong>. Use this as a reference.</p>
  265. <p>Each of these functions takes an array as an argument and returns a single value.</p>
  266. <table>
  267. <thead><tr>
  268. <th><strong>Function</strong></th>
  269. <th>Description</th>
  270. </tr>
  271. </thead>
  272. <tbody>
  273. <tr>
  274. <td><code>np.prod</code></td>
  275. <td>Multiply all elements together</td>
  276. </tr>
  277. <tr>
  278. <td><code>np.sum</code></td>
  279. <td>Add all elements together</td>
  280. </tr>
  281. <tr>
  282. <td><code>np.all</code></td>
  283. <td>Test whether all elements are true values (non-zero numbers are true)</td>
  284. </tr>
  285. <tr>
  286. <td><code>np.any</code></td>
  287. <td>Test whether any elements are true values (non-zero numbers are true)</td>
  288. </tr>
  289. <tr>
  290. <td><code>np.count_nonzero</code></td>
  291. <td>Count the number of non-zero elements</td>
  292. </tr>
  293. </tbody>
  294. </table>
  295. <p>Each of these functions takes an array as an argument and returns an array of values.</p>
  296. <table>
  297. <thead><tr>
  298. <th><strong>Function</strong></th>
  299. <th>Description</th>
  300. </tr>
  301. </thead>
  302. <tbody>
  303. <tr>
  304. <td><code>np.diff</code></td>
  305. <td>Difference between adjacent elements</td>
  306. </tr>
  307. <tr>
  308. <td><code>np.round</code></td>
  309. <td>Round each number to the nearest integer (whole number)</td>
  310. </tr>
  311. <tr>
  312. <td><code>np.cumprod</code></td>
  313. <td>A cumulative product: for each element, multiply all elements so far</td>
  314. </tr>
  315. <tr>
  316. <td><code>np.cumsum</code></td>
  317. <td>A cumulative sum: for each element, add all elements so far</td>
  318. </tr>
  319. <tr>
  320. <td><code>np.exp</code></td>
  321. <td>Exponentiate each element</td>
  322. </tr>
  323. <tr>
  324. <td><code>np.log</code></td>
  325. <td>Take the natural logarithm of each element</td>
  326. </tr>
  327. <tr>
  328. <td><code>np.sqrt</code></td>
  329. <td>Take the square root of each element</td>
  330. </tr>
  331. <tr>
  332. <td><code>np.sort</code></td>
  333. <td>Sort the elements</td>
  334. </tr>
  335. </tbody>
  336. </table>
  337. <p>Each of these functions takes an array of strings and returns an array.</p>
  338. <table>
  339. <thead><tr>
  340. <th><strong>Function</strong></th>
  341. <th><strong>Description</strong></th>
  342. </tr>
  343. </thead>
  344. <tbody>
  345. <tr>
  346. <td><code>np.char.lower</code></td>
  347. <td>Lowercase each element</td>
  348. </tr>
  349. <tr>
  350. <td><code>np.char.upper</code></td>
  351. <td>Uppercase each element</td>
  352. </tr>
  353. <tr>
  354. <td><code>np.char.strip</code></td>
  355. <td>Remove spaces at the beginning or end of each element</td>
  356. </tr>
  357. <tr>
  358. <td><code>np.char.isalpha</code></td>
  359. <td>Whether each element is only letters (no numbers or symbols)</td>
  360. </tr>
  361. <tr>
  362. <td><code>np.char.isnumeric</code></td>
  363. <td>Whether each element is only numeric (no letters) </td>
  364. </tr>
  365. </tbody>
  366. </table>
  367. <p>Each of these functions takes both an array of strings and a <em>search string</em>; each returns an array.</p>
  368. <table>
  369. <thead><tr>
  370. <th><strong>Function</strong></th>
  371. <th><strong>Description</strong></th>
  372. </tr>
  373. </thead>
  374. <tbody>
  375. <tr>
  376. <td><code>np.char.count</code></td>
  377. <td>Count the number of times a search string appears among the elements of an array</td>
  378. </tr>
  379. <tr>
  380. <td><code>np.char.find</code></td>
  381. <td>The position within each element that a search string is found first</td>
  382. </tr>
  383. <tr>
  384. <td><code>np.char.rfind</code></td>
  385. <td>The position within each element that a search string is found last</td>
  386. </tr>
  387. <tr>
  388. <td><code>np.char.startswith</code></td>
  389. <td>Whether each element starts with the search string </td>
  390. </tr>
  391. </tbody>
  392. </table>
  393. </div>
  394. </div>
  395. </div>
  396. </div>