CS代考

Live Coding Wk1 – Lecture2 – Introduction to NumPy¶

While we covered a small amount of Python in the last lecture (Wk1-1), now we can really get into the thick of things since you’ve been formally introduced to Python.

Copyright By PowCoder代写 加微信 powcoder

NumPy is a powerful linear algebra library for Python. What makes it so important is that almost all of the libraries in the PyData ecosystem (pandas, scipy, scikit-learn, etc.) rely on NumPy as one of their main building blocks. Plus we will use it to generate data for our analysis examples later on!

NumPy is also incredibly fast, as it has bindings to C libraries. For more info on why you would want to use arrays instead of lists, check out this great StackOverflow post.

We will only learn the basics of NumPy.

Using NumPy¶
Once you’ve installed NumPy you can import it as a library:

NumPy has many built-in functions and capabilities. We won’t cover them all but instead we will focus on some of the most important aspects of NumPy: vectors, arrays, matrices and number generation. Let’s start by discussing arrays.

NumPy arrays are the main way we will use NumPy throughout the course. NumPy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-dimensional (1D) arrays and matrices are 2D (but you should note a matrix can still have only one row or one column).

Let’s begin our introduction by exploring how to create NumPy arrays.

Creating Num ¶
From a Python List¶
We can create an array by directly converting a list or list of lists:

Built-in Methods¶
There are lots of built-in ways to generate arrays.

Return evenly spaced values within a given interval. [reference]

zeros and ones¶
Generate arrays of zeros or ones. [reference]

Numpy also has lots of ways to create random number arrays:

Creates an array of the given shape and populates it with random samples from a uniform distribution over [0, 1). [reference]

Num and Selection¶
In this lecture we will discuss how to select elements or groups of elements from an array.

Bracket Indexing and Selection¶
The simplest way to pick one or some elements of an array looks very similar to python lists:

Broadcasting¶
NumPy arrays differ from normal Python lists because of their ability to broadcast. With lists, you can only reassign parts of a list with new parts of the same size and shape. That is, if you wanted to replace the first 5 elements in a list with a new value, you would have to pass in a new 5 element list. With NumPy arrays, you can broadcast a single value across a larger set of values:

Indexing a 2D array (matrices)¶
The general format is arr_2d[row][col] or arr_2d[row,col]. I recommend using the comma notation for clarity.

More Indexing Help¶
Indexing a 2D matrix can be a bit confusing at first, especially when you start to add in step size. Try google image searching NumPy indexing to find useful images, like this one:

Image source: https://scipy-lectures.org/intro/numpy/array_object.html

Other things¶

Array operations¶
Basic operations apply element-wise.
Each operation results a new array

a = np.arange(5) # [0, 1, 2, 3, 4]
b = np.arange(5) # [0, 1, 2, 3, 4]
print(a+b) # array([0, 2, 4, 6, 8])
print(a-b) # array([0, 0, 0, 0, 0])
print(a**2)# array([ 0, 1, 4, 9, 16])
print(a>3)
# array([False, False, False, False, True], dtype=bool)
print(10*np.sin(a))
# array([0.,8.41470985,9.09297427,1.41120008,7.56802495])
print(a*b) # array([ 0, 1, 4, 9, 16])

Multiplication¶
Basic multiplication is element-wise. To do matrix multiplication
you need to call the dot function.

Note: We can achieve this by applying a loop also, but it’s way slower

x = np.arange(4)
y = np.arange(4,8)
x.shape = (2,2) # [[0, 1], [2, 3]]
y.shape = (2,2) # [[4, 5], [6, 7]]
print(x*y) # array([[0, 5], [12, 21]])
print(np.dot(x,y)) # array([[6, 7], [26, 31]])
print(np.dot(y,x)) # array([[10, 19], [14, 27]])

Other Functions (just a few…)¶
Numpy provides several other functions like exp, sin, max, min,
etc, that operates on arrays directly

x = np.random.random((2,3))
print(x.sum())
print(x.max())
print(x.min(axis=0))
print(x.max(axis=1))

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com