CS计算机代考程序代写 python CS1-FULL-FALL2021

CS1-FULL-FALL2021

CS1
Week 13
Numpy

Maíra Marques Samary

NumPy

Foundation of the Python Scientific Stack

NumPy
• NumPy == Numeric Python

• It is a library that allows to work with numerical data in
multidimensional spaces.

• In practice, we use it to solve problems that require vector and
matrix operations.

• It can be very useful in Linear Algebra and in advanced
mathematical courses.

• To use it in our programs, we must include it:
from numpy import *

NumPy Basics
• NumPy introduces two new types of data that allows us to

represent vectors and matrices:

• Array: Used to store vectors.

• Matrix: Used to store matrix.

• For both types, the commonly used arithmetic
operations are defined.

• Also, as we will see, there are other useful functions that
save a lot of programming effort.

Arrays

• Basically an array is a
vector.

• It is possible to create
an array from a list

Lists and arrays
don’t look the same
when printed

Array

• It is very straight
forward to do basic
arithmetic
operations with
arrays

Arrays and Lists are not the same

• A list is a generic container for any data type

• An array is a container for numerical data and
accepts arithmetical operations in
multidimensional spaces

Array generation functions
• There are some functions that can help to generate automatic arrays.

• arange(max): creates an array with valued
arange(10) = [0 1 2 3 4 5 6 7 8 9]

• arange(min, max, step): creates an array with valued
arange(2,10,2) = [2 4 6 8]

• zeros(n) creates an array with n zeros
zeros(8) = [0 0 0 0 0 0 0 0]

• zeros((I,j)) creates an array of two dimensions (matrix) full of zeros
zeros((2,2)) = [[0 0] [0 0]]

• linspace(min, max, n) creates an array with n values divided equally
between min and max (both are included)

linspace(2,50, 10) =
[ 2. 7.33333333 12.66666667 18. 23.33333333

28.66666667 34. 39.33333333 44.66666667 50. ]

Subarrays
• The access to the elements of an array is similar to how it is done with lists:

the index of the desired position is put in brackets.

• Exists the slicing operator, the operator: (two points), which allows you to
obtain a subarray.

• However, unlike what happens with lists, a subarray always takes references
to the elements of the original list.

• Consequently, if the subarray is modified, the original list is modified. In
lists this only happened in a list of lists.

• But it is possible to create a copy of an array using the copy () operation

arr y arr2 are references to
the same array

With the copy() operation we can
obtain what we want (real copy)

Matrixes

• A matrix can be understood as a two-dimensional array.

• The way to create a matrix is similar to the creation of
an array.

• We can create an array using a list of lists.

• Each list goes to form a row of the matrix.