12/10/2018 Sample solutions | Solution: Notebook 10 | CSE6040x Courseware | edX
https://courses.edx.org/courses/course-v1:GTx+CSE6040x+2T2019/courseware/7fe7aa0a1022469f9160c552e723a1ee/39ae4347e5584071a7cbe4ff… 1/23
C
...
12/10/2018 Sample solutions | Solution: Notebook 10 | CSE6040x Courseware | edX
https://courses.edx.org/courses/course-v1:GTx+CSE6040x+2T2019/courseware/7fe7aa0a1022469f9160c552e723a1ee/39ae4347e5584071a7cbe4ff… 1/23
Course Modul… Solutio… Sample…
Sample solutions
Part 0: Intro to Numpy/Scipy
Numpy (http://www.numpy.org/) is a Python module that provides fast primitives for multidimensional arrays. It's well-suited to implementing numerical linear
algebra algorithms, and for those can be much faster than Python's native list and dictionary types when you only need to store and operate on numerical data.
Some of the material from this lesson is copied from the following, and more comprehensive, tutorial: link (http://www.scipylectures.org/intro/numpy/index.html)
Quick demo. The recommended importing idiom is:
In [1]: import numpy as np
print(np.__version__)
Creating a simple numpy array
In [2]: a = np.array([1,2,3,4])
print(a)
Why bother with Numpy? A motivating example
We already have lists and dictionary types, which are pretty easy to use and very flexible. So why bother with this special type?
Exercise 0 (ungraded). One reason to consider Numpy is that it "can be much faster," as noted above. But how much faster is that? Run the experiment below
to see.
In [3]: n = 1000000
In [4]: L = range(n)
%timeit [i**2 for i in L]
In [5]: np.arange(10) # Moral equivalent to `range`
In [6]: A = np.arange(n)
%timeit A**2
Creating multidimensional arrays
Beyond simple arrays, Numpy supports multidimensional arrays. To do more than one dimension, call numpy.array() but nest each new dimension within a
list. It's easiest to see by example.
In [7]: # Create a two-dimensional array of size 3 rows x 4 columns:
B = np.array([[0, 1, 2, 3],
1.14.0
[1 2 3 4]
273 ms ± 3.44 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Out[5]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
690 µs ± 2.34 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
12/10/2018 Sample solutions | Solution: Notebook 10 | CSE6040x Courseware | edX
https://courses.edx.org/courses/course-v1:GTx+CSE6040x+2T2019/courseware/7fe7aa0a1022469f9160c552e723a1ee/39ae4347e5584071a7cbe4ff… 2/23
[4, 5, 6, 7],
[8, 9, 10, 11]])
print(B)
In [8]: print(B.ndim) # What does this do?
print(B.shape) # What does this do?
print(len (B)) # What does this do?
In [9]: C1 = [[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]]
C2 = [[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]
C = np.array([C1, C2])
print(C)
print(C.ndim)
print(C.shape)
print(len (C))
There are routines for creating various kinds of structured matrices as well, which are similar to those found in MATLAB
(http://www.mathworks.com/products/matlab/) and Octave (https://www.gnu.org/software/octave/).
In [10]: print(np.zeros((3, 4)))
In [11]: print(np.ones((3, 4)))
In [12]: print(np.eye(3))
In [13]: print(np.diag([1, 2, 3]))
You can also create empty (uninitialized) arrays. What does the following produce?
In [14]: A = np.empty((3, 4)) # An "empty" 3 x 4 matrix
print(A)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
2
(3, 4)
3
[Show More]