Course 1.1Lesson 3 of 5
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.
Try the code and the simulations beside the lesson.
After this lesson you can
np.where and np.clip.Predict first. What does a[::-1] give? Then run it.
If you know lists, there is nothing new here.
a[0] is the first number, and a[-1] is the last.a[1:4] is a slice: the places 1, 2 and 3. The stop (4) is not included.a[:3] is the first three, and a[3:] is everything from place 3.a[::2] takes every second number, and a[::-1] goes backwards.a[1:3] = 0 puts zero in two places at once.For a list, a slice is a new list. For an array, a slice is a view: a window on the same numbers. When the program sets window[0] = 99, the first place of the window is the place 1 of the original, and the original changes too: [ 1 99 3 4 5].
This is by design. It saves memory, because a big array is not copied. But it means that you must be careful. If you want a slice that you can change without touching the original, ask for a copy: original[1:4].copy(). After that, changing safe does not change original.
A rule to remember: a slice is a view. A copy is a copy. Use .copy() when in doubt.
Predict first. Which marks are 50 or more? Then run it.
In the last lesson, marks >= 50 gave an array of True and False. Used inside the square brackets, this mask picks the numbers where it is True.
marks[marks >= 50] gives [82 67 91 75], the marks of those who passed.& (and), | (or), and ~ (not). Each condition must have brackets: (marks >= 60) & (marks < 90). The reason is that & binds tighter than >=, so without brackets Python does the wrong thing and gives an error. Do not use the words and and or with arrays. They work on one True or False, and raise a ValueError for an array.~(marks >= 50) is the opposite: the marks of those who did not pass, [45 30].marks_copy[marks_copy < 50] = 50 raises every low mark to 50. The original marks was not touched, because we worked on a copy.Masks are the tool for questions such as "How many readings are above the limit?" and "Remove the faulty values".
Use & and | between masks, with brackets round each comparison. The words and and or do not work on arrays.
.copy().marks[marks >= 60 & marks < 90]. Each condition needs its own brackets.and or or between arrays. Use & and |.a[1:4] has 3 numbers.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:
.copy() makes a real copy.a[a > 5] picks the numbers for which the condition is True.&, | and ~, and put each condition in brackets.np.where(c, a, b) chooses for every place. np.clip keeps numbers between two limits.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
a = np.array([5, 6, 7, 8]), what is a[1:3]?b = a[1:3] and we set b[0] = 0, what is a?a that are above 5 and below 8?np.clip(np.array([1, 5, 9]), 2, 8) 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.