Science works with many numbers at once. A list can hold them, but it cannot do maths on them. The NumPy array can. In this course you make arrays with array, arange and linspace, write a formula once and use it on a whole array, pick numbers with indexes, slices and boolean masks, and find sums, averages and standard deviations of rows and columns of tables. You learn to be careful with slices, which are views and not copies, and with broadcasting, which lets arrays of different shapes work together. A project finds the best angle for a throw on Earth and on the Moon. The practice runs your code and tells you which results are wrong.
5 lessons, about 3 h 5 min5 simulations10 problems10 practice questionsPython in every lesson
NumPy is the library that scientific Python is built on. Its main object is the array, which holds many numbers of one type and does maths on all of them at once. np.array turns a list into an array, and an array adds, multiplies and divides place by place, where a list joins and repeats. An array has a dtype, a shape, a number of dimensions and a length. np.arange works like range and leaves out the stop, np.linspace gives a fixed number of points with both ends included, and np.zeros, np.ones and np.full make arrays of one value. One type serves all the numbers, so one decimal turns the whole array into decimals, and astype changes the type. An array does a million operations far faster than a loop.
An array works with a number or with another array of the same length place by place, so one formula serves all the values at once. Writing v equal to g times t for an array of times gives an array of speeds, with no loop. Functions of NumPy such as np.sqrt, np.sin, np.exp, np.abs and np.round work on every number of an array, and np.deg2rad changes degrees to radians because the trigonometric functions need radians. The math module takes one number only, so math.sqrt of an array is an error. Arrays of different lengths do not fit, and give a ValueError. A comparison such as temps greater than 32 gives an array of True and False, and sum counts the True values.
Indexing and slicing work for arrays as for lists: a[0] is the first, a[-1] the last, a[1:4] is a slice, a[::2] takes every second one and a[::-1] reverses. A slice of an array is a view of the same numbers and not a copy, so changing the slice changes the original, and copy makes a real copy. A boolean mask such as a[a greater than 50] picks the numbers for which the condition is True. Conditions combine with & for and, | for or and ~ for not, and each condition needs brackets. Assigning to a mask changes only the chosen places. np.where chooses between two values for every place, or gives the places where a condition is True, and np.clip keeps the numbers between two limits.
An array has methods that give one number from many: sum, min, max, mean, std, argmax and argmin, and np.median, cumsum and diff. The standard deviation of NumPy divides by n by default and by n minus 1 when ddof is 1, which is the choice for a sample. A table is a two-dimensional array with a shape of rows and columns, indexed as a[row, column], where a[:, 0] is the first column and a[0] the first row. The argument axis equal to 0 works down the columns and axis equal to 1 works along the rows. reshape changes the shape and keeps the numbers, T flips rows and columns, and minus 1 lets NumPy find a size. Broadcasting lets arrays of different shapes work together, for example a column times a row gives a table.
A project that uses the whole chapter: a ball is thrown at 20 metres per second at several angles, and arrays give the time of flight, the range and the greatest height for every angle with one formula each. The range is v squared times sin of 2 theta over g, the time of flight is 2 v sin theta over g, and the height is the square of v sin theta over twice g. np.deg2rad changes the angles to radians, argmax finds the angle of the longest range, which is 45 degrees, and a mask finds the angles that give a range above a limit. A column of angles with a row of values of g gives a whole table by broadcasting, and the same angle gives a range six times longer on the Moon than on Earth.
Log in to join the discussion.
Loading…