Week 9 - Introduction To Numpy - Part 2
Week 9 - Introduction To Numpy - Part 2
Week 9
Program Studi Teknik Informatika
Fakultas Teknik – Universitas Surabaya
Create Your Own ufunc
• You can create own ufunc, you have to define a function, like you do with
normal functions in Python, then you add it to your NumPy ufunc library
with the frompyfunc() method.
• The frompyfunc() method takes the following arguments:
– function - the name of the function.
– inputs - the number of input arguments (arrays).
– outputs - the number of output arrays.
myadd = np.frompyfunc(myadd, 2, 1)
print(myadd([1, 2, 3, 4], [5, 6, 7, 8])) # Output: [6 8 10 12]
Working with Boolean Arrays
Counting entries
• Given a Boolean array, there are a host of useful operations
you can do.
• To count the number of True entries in a Boolean array,
np.count_nonzero is useful
• If we’re interested in quickly checking whether any or all the values are
true, we can use (you guessed it) np.any() or np.all()
# are there any values greater than 8?
print(np.any(x > 8)) #Output: True
x = np.random.randint(100, size=10)
print(x) # Output: [97 2 8 94 77 38 18 49 91 50]
• Suppose we want to access three different elements. we can pass a single list
or array of indices to obtain the result
ind = [3, 7, 4]
print(x[ind]) # Output: [94 49 77]
Exploring Fancy Indexing
• With fancy indexing, the shape of the result reflects the shape of the
index arrays rather than the shape of the array being indexed.
ind = np.array([[3, 7],[4, 5]])
print(x[ind]) Output:
• Like with standard indexing, the first index refers to the row, and the
second to the column row = np.array([0, 1, 2])
col = np.array([2, 1, 3])
print(X[row, col]) # Output: [ 2 5 11]
Exploring Fancy Indexing
• Notice that the first value in the result is X[0, 2], the second is X[1, 1],
and the third is X[2, 3].
• If we combine a column vector and a row vector within the indices, we
get a two-dimensional result.
print(X[row[:, np.newaxis], col]) Output:
• For even more powerful operations, fancy indexing can be combined with the
other indexing schemes.
# We can combine fancy and simple indices
print(X[2, [2, 0, 1]]) # Output: [10 8 9]
x = np.array([2, 1, 4, 3, 5])
print(selection_sort(x)) # Output: [ 1 2 3 4 5 ]
Sorting
• The selection sort is useful for its simplicity, but is much too slow to be
useful for larger arrays.
• For a list of N values, it requires N loops, each of which does on the
order of ~ N comparisons to find the swap value.
• In terms of the “big-O” notation often used to characterize these
algorithms, selection sort averages O(N2).
• If you double the number of items in the list, the execution time will go
up by about a factor of four.
• Although Python has built-in sort and sorted functions to work with lists, we
won’t discuss them here because NumPy’s np.sort function turns out to be
much more efficient and useful for our purposes.
Sorting
• By default np.sort uses an O(N log N) , quicksort algorithm, though
mergesort and heapsort are also available.
• For most applications, the default quicksort is more than sufficient.
x = np.array([2, 1, 4, 3, 5])
print(np.sort(x)) # Output: [ 1 2 3 4 5 ]
• The first element of that result gives the index of the smallest element, the
second value gives the index of the second smallest, and so on.
Sorting
• A useful feature of NumPy’s sorting algorithms is the ability to sort
along specific rows or columns of a multidimensional array using the
axis argument.
X = np.random.randint(0, 10, (4, 6))
print(X) Output:
x = np.array([7, 2, 3, 1, 6, 5, 4])
print(np.partition(x, 3)) # Output: [2 1 3 4 6 5 7]
• Note that the first three values in the resulting array are the three smallest in
the array, and the remaining array positions contain the remaining values.
• Within the two partitions, the elements have arbitrary order.
Searching
• We can search an array for a certain value, and return the indexes
that get a match. To search an array, use the where() method.
arr = np.array([1, 2, 3, 4, 5, 4, 4])
x = np.where(arr == 4)
print(x) # Output: (array([3, 5, 6], dtype=int64),)
• Another example: Find the indexes where the values are even or
odd
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
x = np.where(arr%2 == 0)
y = np.where(arr%2 == 1)
print(x) # Output: (array([1, 3, 5, 7], dtype=int64),)
print(y) # Output: (array([0, 2, 4, 6], dtype=int64),)
Searching
• There is a method called searchsorted() which performs a binary
search in the array, and returns the index where the specified value
would be inserted to maintain the search order.
arr = np.array([2, 7, 9, 12, 12])
# Find the indexes where the value 10 should be inserted, starting from the right.
print(np.searchsorted(arr, 10, side='right')) # Output: 3
• The example above will return [41 43], why? Because the new
filter contains only the values where the filter array had the
value True, in this case, index 0 and 2.
Filtering
• Another example:
# Create a filter array that will return only even elements from the original array
arr = np.array([1, 2, 3, 4, 5, 6, 7])
filter_arr = arr % 2 == 0
newarr = arr[filter_arr]
print(filter_arr) #Output: [False True False True False True False]
print(newarr) #Output: [2 4 6]
# Create a filter array that will return only values higher than 42
arr = np.array([41, 42, 43, 44])
filter_arr = arr > 42
newarr = arr[filter_arr]
print(filter_arr) #Output: [False False True True]
print(newarr) #Output: [43 44]
Questions??
Exercise
• Create NRP_Nickname_ExWeek9.ipynb file.
Question 1
Create a 5X2 integer array from the range 100 to 200 so that the
difference between each element is 10. Here is an example of
what it looks like:
Exercise
Question 2
The following provides a numPy array.
np.array([[11 ,22, 33], [44, 55, 66], [77, 88, 99]])
Returns an array of items in the second column of all existing
rows. Here is the expected display:
Exercise
Question 3
The following provides a numPy array.
np.array([[3 ,6, 9, 12], [15 ,18, 21, 24],[27 ,30, 33, 36],
[39 ,42, 45, 48], [51 ,54, 57, 60]])
Returns the given array of odd rows and even columns. Here is
the expected display:
Exercise
Question 4
Add the following two NumPy arrays
arrayOne = np.array([[5, 6, 9], [21 ,18, 27]])
arrayTwo = np.array([[15 ,33, 24], [4 ,7, 1]])
And modify the resulting array by calculating the square root of each
element. Here is the expected display: