The first item its the number of elements that appear

This preview shows page 15 - 18 out of 32 pages.

of the first item. It’s the number of elements that appearbeforethat item. So 3 is the index of the 4th item.Here are some more examples.In the examples, we’ve given names to the things we get out ofpopulation_amounts. Read and run each cell.[71]:# The 13th element in the array is the population# in 1962 (which is 1950 + 12).population_1962=population_amounts.item(12)population_1962[71]:3140093217[72]:# The 66th element is the population in 2015.population_2015=population_amounts.item(65)population_2015[72]:7256490011[73]:# The array has only 66 elements, so this doesn't work.# (There's no element with 66 other elements before it.)population_2016=population_amounts.item(66)population_201615
,---------------------------------------------------------------------------IndexErrorTraceback (most recent call,last)<ipython-input-73-20d13c231c2b> in <module>1 # The array has only 66 elements, so this doesn't work.2 # (There's no element with 66 other elements before it.)----> 3 population_2016 = population_amounts.item(66)4 population_2016IndexError: index 66 is out of bounds for axis 0 with size 66Sincemake_arrayreturns an array, we can call.item(3)on its output to get its 4th element, justlike we ”chained” together calls to the methodreplaceearlier.[74]:make_array(-1,-3,4,-2).item(3)[74]:-2Question 2.2.1.Setpopulation_1973to the world population in 1973, by getting the appropriateelement frompopulation_amountsusingitem.[75]:population_1973=population_amounts.item(23)population_1973[75]:3942096442[76]:ok.grade("q221");~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Running tests---------------------------------------------------------------------Test summaryPassed: 1Failed: 0[ooooooooook] 100.0% passed3.32.3. Doing something to every element of an arrayArrays are primarily useful for doing the same operation many times, so we don’t often have to use.itemand work with single elements.16
LogarithmsHere is one simple question we might ask about world population:How big was the population inorders of magnitudein each year?Orders of magnitude quantify how big a number is by representing it as the power of anothernumber (for example, representing 104 as102.017033). One way to do this is by using the logarithmfunction. The logarithm (base 10) of a number increases by 1 every time we multiply the numberby 10. It’s like a measure of how many decimal digits the number has, or how big it is in orders ofmagnitude.We could try to answer our question like this, using thelog10function from themathmodule andtheitemmethod you just saw:[77]:population_1950_magnitude=math.log10(population_amounts.item(0))population_1951_magnitude=math.log10(population_amounts.item(1))population_1952_magnitude=math.log10(population_amounts.item(2))population_1953_magnitude=math.log10(population_amounts.item(3))make_array(population_1950_magnitude, population_1951_magnitude,,population_1952_magnitude, population_1953_magnitude)[77]:array([9.40783749, 9.4141273 , 9.42107263, 9.42846742])But this is tedious and doesn’t really take advantage of the fact that we are using a computer.Instead, NumPy provides its own version oflog10that takes the logarithm of each element of anarray. It takes a single array of numbers as its argument. It returns an array of the same length,where the first element of the result is the logarithm of the first element of the argument, and soon.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 32 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
Array, World population

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture