Course 1.1Lesson 4 of 5
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.
Try the code and the simulations beside the lesson.
After this lesson you can
axis, andPredict first. Which place holds the tallest height? Then run it.
The heights of six friends are in centimetres.
h.sum(), h.min() and h.max() give the total (923), the smallest (148) and the biggest (160).h.mean() is the average, 153.83. np.median(h) is the middle value, 153.5. (The median is a function of np, and not a method of the array.)h.std() is the standard deviation, a measure of how spread out the heights are. NumPy divides by by default. The statistics of a sample divide by , and you ask for this with ddof=1: 4.67 and not 4.26. When your numbers are a sample of a larger population, which is usual in an experiment, use ddof=1.h.argmax() is the place of the biggest number: 2 (the value 160). h.argmin() is the place of the smallest: 1 (the value 148). Use argmax when you want to know which, and max when you want to know how much.h.cumsum() is the running total: 152, 152 + 148 = 300, and so on.np.diff(h) is the difference between each number and the one before: [-4 12 -5 -5 8]. It has one number fewer than h.np.sort(h)[::-1][:3] sorts, reverses, and takes the first three: the three tallest.Predict first. What is marks[1, 2]? Then run it.
A table of numbers is a two-dimensional array. The marks of three students (the rows) in four tests (the columns) have the shape (3, 4): 3 rows and 4 columns. marks.size is the total number of numbers: 12.
marks[1, 2] is the number in row 1, column 2. Counting starts at 0, so it is the second student in the third test: 55. The row comes first, then the column.marks[0] is the whole first row. marks[:, 0] means "all the rows, column 0": the first column, the first test of all the students. The colon means "everything".marks[:2, 1:3] is a smaller table: rows 0 and 1, columns 1 and 2.Now the important idea, the axis.
marks.mean(axis=1) gives one average for each row (each student): it works along the columns of each row. The result is [75. 60. 88.5].marks.mean(axis=0) gives one average for each column (each test): it works down the rows. The result has four numbers.marks.max() is 95, the best mark in the whole table.A way to remember: axis=0 collapses the rows, leaving one number per column. axis=1 collapses the columns, leaving one number per row.
The axis you name is the one that disappears: axis=0 adds over the rows, so only the columns are left.
The rule in short: compare the shapes from the right. Two sizes fit if they are equal or one of them is 1 (or missing). Then the size 1 is stretched. If neither is the case, you get the ValueError that you saw in the last lesson.
axis=0 gives one value per column, axis=1 one value per row.marks[1][2] and marks[1, 2] as if they were different. For an array they give the same number, but marks[1, 2] is the usual way.ddof=1 for a sample.argmax and max. argmax is the place. max is the value.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
sum, min, max, mean, std, argmax, argmin, np.median, cumsum, np.diff.std() divides by . For a sample, use std(ddof=1).a[row, column], a[:, 0] for a column.axis=0 gives one value per column. axis=1 gives one value per row.reshape, .T and broadcasting.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
shape of a table with 4 rows and 6 columns?t = np.array([[1, 2], [3, 4]]), what is t.sum(axis=0)?t.sum(axis=1)?np.array([4, 9, 2]).argmax() give?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.