Download this page as a jupyter notebook at Lab 9

Laboratory 9: Matrices a Blue Pill Approach

LAST NAME, FIRST NAME

R00000000

ENGR 1330 Laboratory 9 - In-Lab

Numpy

Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. The library’s name is short for “Numeric Python” or “Numerical Python”. If you are curious about NumPy, this cheat sheet is recommended: https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_Cheat_Sheet.pdf

Arrays

A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension. In other words, an array contains information about the raw data, how to locate an element and how to interpret an element.To make a numpy array, you can just use the np.array() function. All you need to do is pass a list to it. Don’t forget that, in order to work with the np.array() function, you need to make sure that the numpy library is present in your environment. If you want to read more about the differences between a Python list and NumPy array, this link is recommended: https://webcourses.ucf.edu/courses/1249560/pages/python-lists-vs-numpy-arrays-what-is-the-difference


Example- 1D Arrays

Let's create a 1D array from the 2000s (2000-2009):


Example- n-Dimensional Arrays

Let's create a 5x2 array from the 2000s (2000-2009):

Arrays Arithmetic

Once you have created the arrays, you can do basic Numpy operations. Numpy offers a variety of operations applicable on arrays. From basic operations such as summation, subtraction, multiplication and division to more advanced and essential operations such as matrix multiplication and other elementwise operations. In the examples below, we will go over some of these:


Example- 1D Array Arithmetic

You can see it is considerably easier than manipulating as a list - be aware that the multiply above is element-by-element and not the inner product of two matrices (as was shown in Lesson 08)


Example- n-Dimensional Array Arithmetic


Arrays Comparison

Comparing two NumPy arrays determines whether they are equivalent by checking if every element at each corresponding index are the same.


Example- 1D Array Comparison


Arrays Manipulation

numpy.copy() allows us to create a copy of an array. This is particularly useful when we need to manipulate an array while keeping an original copy in memory. The numpy.delete() function returns a new array with sub-arrays along an axis deleted. Here is a poctoral representation:

Here is another pictorial representation:

Let's have a look at the examples.


Example- Copying and Deleting Arrays and Elements


Sorting Arrays

Sorting means putting elements in an ordered sequence. Ordered sequence is any sequence that has an order corresponding to elements, like numeric or alphabetical, ascending or descending. If you use the sort() method on a 2-D array, both arrays will be sorted.


Example- Sorting 1D Arrays

Define a 1D array as ['FIFA 2020','Red Dead Redemption','Fallout','GTA','NBA 2018','Need For Speed'] and print it out. Then, sort the array alphabetically.


Example- Sorting n-Dimensional Arrays

Define a 3x3 array with 17,-6,2,86,-12,0,0,23,12 and print it out. Then, sort the array.


Partitioning (Slice) Arrays

Slicing in python means taking elements from one given index to another given index.

We can do slicing like this: [start:end].

We can also define the step, like this: [start:end:step].

If we don't pass start its considered 0

If we don't pass end its considered length of array in that dimension

If we don't pass step its considered 1


Example- Slicing 1D Arrays

Define a 1D array as [1,3,5,7,9], slice out the [3,5,7] and print it out.


Example- Slicing n-Dimensional Arrays

Define a 5x5 array with "Superman, Batman, Jim Hammond, Captain America, Green Arrow, Aquaman, Wonder Woman, Martian Manhunter, Barry Allen, Hal Jordan, Hawkman, Ray Palmer, Spider Man, Thor, Hank Pym, Solar, Iron Man, Dr. Strange, Daredevil, Ted Kord, Captian Marvel, Black Panther, Wolverine, Booster Gold, Spawn " and print it out. Then:


This is a Numpy Cheat Sheet- similar to the one you had on top of this notebook!

Check out this link for more:

https://blog.finxter.com/collection-10-best-numpy-cheat-sheets-every-python-coder-must-own/

Exercise: Python List vs. Numpy Arrays?

Put your answer in the empty markdown cell below; Make sure to cite any resources that you may use.

Use this markdown cell for your answer


Here are some of the resources used for creating this notebook:

Here are some great reads on this topic:

Here are some great videos on these topics: