These exercises come from the NumPy tutorial examples.
Instructions: Create a new notebook called NumPyExercises
in your NumPy
directory and solve the following problems inside it. Be sure to include the problem statements in a markdown cell above your solution. You don't need to put the "helper" code in the markdown cell, just implement the helper code in your code cell with your solution.
Preliminaries: At the top of your notebook, include a "Heading 1" cell with the title NumPy Exercises. Then include the SciPy and NumPy inline functions by adding a code cell that invokes the %pylab inline
magic:
In [ ]:
%pylab inline
import numpy as np
Create the following arrays with correct data types:
[[1 1 1 1]
[1 1 1 1]
[1 1 1 2]
[1 6 1 1]]
[[0. 0. 0. 0. 0.]
[2. 0. 0. 0. 0.]
[0. 3. 0. 0. 0.]
[0. 0. 4. 0. 0.]
[0. 0. 0. 5. 0.]
[0. 0. 0. 0. 6.]]
You should be able to accomplish this in 3 lines of code or less for each one. The documentation for the diag
method may be helpful.
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
Read the documentation for np.tile
and use this function to construct the following array:
[[4 3 4 3 4 3]
[2 1 2 1 2 1]
[4 3 4 3 4 3]
[2 1 2 1 2 1]]
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
Let’s create a prime number sieve. We will use the sieve to see what numbers between 0 and 100 are prime.
(a) First, construct an array of booleans called is prime with shape (100,)
, filled with
True
in all the elements.
is_prime = np.ones((100,), dtype=bool)
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(b) The index of each boolean element represents the number. “Cross out” 0
and 1
, which are not primes. You can either set them to False
or 0
(which python recognizes as equivalent to False
for boolean types).
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(c) For each subsequent integer j
starting from 2
, cross out its higher multiples:
N_max = int(np.sqrt(len(is_prime)))
for j in range(2, N_max):
is_prime[2*j::j] = False
Make sure you understand what this code is doing. What does that slicing of the array mean?
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(d) Look up the documentation for np.nonzero
(try help(np.nonzero)
or np.nonzero?
) and use it to print the prime numbers.
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(e) Finally, create a new function, called eratosthenes_sieve()
that takes one argument, maximum
, the maximum number to test for primes, and returns an array containing the prime numbers between 2
and maximum
. Use the sieve algorithm developed by Eratosthenes and described on wikipedia. Print the result for the test cases where maximum
= 10
and 100
. (Prime numbers and prime factorization are common topics in the Project Euler exercises.)
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
Let’s do some manipulations on NumPy arrays by starting with a stock image provided in SciPy. It turns out that digital images are just arrays of numbers representing the relative intensity (for B/W images) or RGB values (for color images) of the pixels in the image, similar to ipythonblocks
. We can use the array operations we have learned to do digital image processing!
(a) Import the SciPy stock image into an array (since it is coming from SciPy, it is a NumPy array):
from scipy import misc
lena = misc.lena()
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(b) Try viewing the image in both false color and grayscale:
imshow(lena)
imshow(lena, cmap=cm.gray)
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(c) Let’s crop the image by removing 30 pixels on all sides:
crop_lena = lena[30:-30,30:-30]
imshow(crop_lena)
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(d) Next, let’s make a frame by blacking out all pixels except those in a circle centered on the face. We can do this with a mask. The equation for a circle is just $x^2 + y^2 = R^2$. If we want it centered on the middle of the image at (256,256)
we can create a mask with
y, x = np.ogrid[0:512,0:512]
centerx, centery = (256, 256)
mask = ((y - centery)**2 + (x - centerx)**2) > 230**2
To black out the desired pixels, we just set them equal to 0
or False
:
lena[mask] = 0
imshow(lena)
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell
(e) Create a new frame for the image that is an ellipse (long axis on the vertical) centered on the face.
In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell