NumPy Basics#
Before solving these basic NumPy exercises you should have read NumPy Arrays, Array Operations, Advanced Indexing, and Vectorization. Only use NumPy features discussed there.
import numpy as np
Maximum#
Given two arrays print the largest value of all elements from both arrays. Use np.max or np.maximum or both.
Test your code with
Solution:
# your solution
Largest Difference#
Print the largest elementwise absolute difference between two equally shaped arrays.
Test your code with
Solution:
# your solution
Comparisons 1#
Given an array of integers, test whether there is a one in the array. Print a message showing the result.
Test your code with
Solution:
# your solution
Comparisons 2#
Test whether all elements of an array are positive. Print a message showing the result.
Test your code with
Solution:
# your solution
Indexing 1#
Set all elements between -1 and 1 to zero. Remember: Avoid loops wherever possible.
Test you code with
Solution:
# your solution
Indexing 2#
Write a function which takes an integer \(n\) and returns an \(n\times n\) matrix with ones at the borders and zeros inside. Data type of the matrix should be int8.
Example for \(n=5\):
Solution:
# your solution
Indexing 3#
Write a function which takes an arbitrarily sized matrix and returns a matrix having the same elements as the original matrix, but being bordered by additional zeros. The returned matrix should have the same data type as the original matrix.
Example:
Solution:
# your solution
Broadcasting#
Take the first row of a matrix and add it to all other rows of the matrix. Print the resulting matrix.
Test your code with
Solution:
# your solution
Not as Easy as it Seems#
Given a square matrix with odd number of rows/columns replace all but the boundary elements by zeros and write the mean of all boundary elements to the center element. Print the modified matrix.
Example:
Solution:
# your solution