Week 2 Exercise 02 - NumPy Indexing and Selection
Week 2 Exercise 02 - NumPy Indexing and Selection
Week 2 Exercise 02 - NumPy Indexing and Selection
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.
In [3] #Show
arr
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
#Show
arr
#Show
arr
#Show slice
slice_of_arr
In #Change Slice
[10] slice_of_arr[:]=99
In arr
[11]
Data is not copied, It’s a view of the original array! This avoids memory
problems.
arr_copy
In arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))
[13] arr_2d
In arr_2d[1]
[14]
In arr_2d[1][0]
[15]
Out[15] 20
In arr_2d[1,0]
[16]
Out[16] 20
In arr_2d[:2,1:]
[17]
In arr_2d[2]
[18]
In arr_2d[2,:]
[19]
In arr = np.arange(1,11)
[20] arr
In arr > 4
[21]
Out[21] array([False, False, False, False, True, True, True, True, True,
True])
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]
In arr[arr>2]
[24]
In x=2
[25] arr[arr>x]