Python NumPy For Your Grandma - 3.6 infinity

In this section, we’ll see how you can represent infinite values in NumPy. Like nan, numpy reserves floating point constants for infinity and negative infinity that behave specially. If you want to insert these values directly, you can use np.inf and np.NINF import numpy as np np.array([np.inf, np.NINF]) ## array([ inf, -inf]) More commonly, these values occur when you divide by 0. np.array([-1, 1])/0 ## array([-inf, inf]) ## ## <string>:1: RuntimeWarning: divide by zero encountered in true_divide All the special behaviors you might expect for these values exist such as,

Python NumPy For Your Grandma - 3.7 random

In this section, we’ll see how you can use NumPy’s random module to shuffle arrays, sample values from arrays, and draw values from a host of probability distributions. And then we’ll see why everything I just showed you is deprecated, and how to updated it to modern standards. Let’s see an example of how you might simulate rolling a 6-sided die 3 times. In other words, we want to draw three integers from the range 1 to 6, with replacement.

Python NumPy For Your Grandma - 3.8 Challenge: Love Distance

Setup You’re a relationship scientist and you’ve developed a questionnaire that determines a person’s love score, a real-valued number between 0 and 100. Your theory is that two people with similar love scores should make a good match. Given the love scores for 10 different people, create a matrix where (i,j) gives the absolute difference of the love scores for person i and person j. import numpy as np generator = np.

Python NumPy For Your Grandma - 3.9 Challenge: Professor Prick

Setup You’re a vindictive, hateful professor and one of your pet peeves is when students rush through their exams. To teach them a lesson, you decide to give 0s to the first three students who score less than sixty, in the order they turned in their exams. So, given a 1d array of integers, identify the first three values less than sixty and replace them with zero. import numpy as np generator = np.

Python NumPy For Your Grandma - 4.1 where()

In this section, we’ll see how you can use NumPy’s where() function as a vectorized approach to writing if-else statements. Suppose you have two 1d arrays, foo and bar, each with 2M elements. import numpy as np generator = np.random.default_rng() foo = generator.integers(6, size=10**7) print(foo) ## [4 0 1 ... 3 2 1] bar = generator.integers(6, size=10**7) print(bar) ## [5 3 0 ... 2 4 3] You want to create a third array called baz such that, where bar is even, you double the corresponding value of foo, otherwise, you take half the corresponding value of foo.

Python NumPy For Your Grandma - 4.10 Challenge: Taco Truck

Setup You own a taco truck that’s open 24/7 and you manage five employees who run it. Employees work solo, eight hour shifts. You decide the best way to set their schedule for the upcoming week is to create a bunch of random schedules and select one that looks best. So you build a 1000x21 array of random employee ids where element (i,j) gives the employee id working shift j for schedule i.