Week 2 Exercise 02 - NumPy Indexing and Selection

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

CS158-1L: Artificial Intelligence Laboratory

Week 2 Exercise 02 - NumPy Indexing and Selection

Name: Cruz, Elline Joyce Score


:

Sectio C5 Date: 06/16


n:

Objectives:
● Understand the programming fundamentals and the Python language.
● Write Python programs that utilize variables, data types, and operators.

Instructions:
1. To complete this exercise, please follow the sample commands in Python provided to
you. Once you have completed the assignment, please submit the IPython file and
this document to me. You have one week to complete the exercise from the assigned
date. Please let me know if you have any questions or concerns regarding the
assignment.

2. When submitting your completed assignment, please name the IPython file as
follows: "surname_firstname_MP1Exercise". Replace "surname" with your last name,
"firstname" with your first name, and "MP2Exercise" with the name of the machine
problem.

For example, if your name is John Smith and the machine problem is
"PythonExercise2", the file name should be "smith_john_PythonExercise1.ipynb".

Please adhere to this naming convention when submitting your file. This will ensure I
can quickly identify your submission and provide appropriate feedback.

NumPy Indexing and Selection

In [1] import numpy as np

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Week 2 Exercise 02 - NumPy Indexing and Selection

In [2] #Creating sample array


arr = np.arange(0,11)

In [3] #Show
arr

Out[3] array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

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

In [4] arr[8]

Out[4] 8

In [5] #Get values in a range


arr[1:5]

Out[5] array([1, 2, 3, 4])

In [6] #Get values in a range


arr[0:5]

Out[6] array([0, 1, 2, 3, 4])

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. 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:

In [7] #Setting a value with index range (Broadcasting)


arr[0:5]=100

#Show
arr

Out[7] array([100, 100, 100, 100, 100, 5, 6, 7, 8, 9, 10])

In [8] arr = np.arange(0,11)

#Show
arr

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Week 2 Exercise 02 - NumPy Indexing and Selection

Out[8] array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

In [9] #Important notes on Slices


slice_of_arr = arr[0:6]

#Show slice
slice_of_arr

Out[9] array([0, 1, 2, 3, 4, 5])

In #Change Slice
[10] slice_of_arr[:]=99

#Show Slice again


slice_of_arr

Out[10] array([99, 99, 99, 99, 99, 99])

Now note the changes also occur in our original array!

In arr
[11]

Out[11] array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])

Data is not copied, It’s a view of the original array! This avoids memory
problems.

In #To get a copy, need to be explicit


[12] arr_copy = arr.copy()

arr_copy

Out[12] array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])

Indexing a 2D array (matrices): The general format is arr_2d[row][col] or


arr_2d[row,col].

In arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))
[13] arr_2d

Out[13] array([[ 5, 10, 15],


[20, 25, 30],
[35, 40, 45]])

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Week 2 Exercise 02 - NumPy Indexing and Selection

In arr_2d[1]
[14]

Out[14] array([20, 25, 30])

In arr_2d[1][0]
[15]

Out[15] 20

In arr_2d[1,0]
[16]

Out[16] 20

In arr_2d[:2,1:]
[17]

Out[17] array([[10, 15],


[25, 30]])

In arr_2d[2]
[18]

Out[18] array([35, 40, 45])

In arr_2d[2,:]
[19]

Out[19] array([35, 40, 45])

Conditional Selection: This is a very fundamental concept that will directly


translate to pandas later on.

In arr = np.arange(1,11)
[20] arr

Out[20] array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

In arr > 4
[21]

Out[21] array([False, False, False, False, True, True, True, True, True,
True])

Prepared by: Raymond Sedilla, Mapua University


CS158-1L: Artificial Intelligence Laboratory
Week 2 Exercise 02 - NumPy Indexing and Selection

In bool_arr = arr>4
[22] bool_arr

Out[22] array([False, False, False, False, True, True, True, True, True,
True])

In arr[bool_arr]
[23]

Out[23] array([ 5, 6, 7, 8, 9, 10])

In arr[arr>2]
[24]

Out[24] array([ 3, 4, 5, 6, 7, 8, 9, 10])

In x=2
[25] arr[arr>x]

Out[25] array([ 3, 4, 5, 6, 7, 8, 9, 10])

Prepared by: Raymond Sedilla, Mapua University

You might also like