Practices 04: Numpy

Overview

In practice 04, you will create a single Python file main.py. In this file you will write (or complete) the following functions, each of which solves a particular problem:

  • task01(matrix)
  • task02(matrix, dim)
  • task03(v1, v2)
  • task04(rain_matrix)
  • task05(n, m)
  • task06(matrix)

Both filename and names of the functions have to be exactly the same as described.

When submitting your solution:

  1. Zip the file main.py into .zip archive with arbitrary name.
  2. Upload the .zip file to the BRUTE evaluation system.

Grading

You can obtain maximum of 2 points.

Testing Your Code Localy

In starter code which is provided bellow each function contains docstring with short desription. Docstrings also contain examples of usage. Those examples can be used for testing your functions using doctest module. To run those tests you can simply run the main module as main script.

Starter Code

Below is a basic code structure you can use. Copy-paste the following code and complete each function (task01, task02, task03, …) according to description in the function docstring. Then submit your final main.py.

import numpy as np
from numpy import linalg
 
def task01(matrix):
	'''Returns copy of original matrix with even rows and columns removed.
	Notice that even rows (or columns) have odd indices in original matrix
	for example in Example bellow, second row is [6,2,9,6,7,3,5]
 
	:param matrix: 2D np.array (matrix) M x N
	:return: 2D np.array (matrix) M x N, contains only odd rows and columns of original matrix
 
	Examples:
 
	>>> m = np.array([
	...	  [2,6,9,5,2,4,5],
	...	  [6,2,9,6,7,3,5],
	...	  [3,9,1,6,7,8,2],
	...	  [5,6,9,3,7,9,1],
	...	  [6,8,2,9,1,4,7]
	... ])
	>>> result = np.array([
	...		[2,9,2,5],
	...		[3,1,7,2],
	...		[6,2,1,7]
	... ])
	>>> np.array_equal(task01(m), result)
	True
	'''
	pass
 
def task02(matrix, dim):
	'''Takes matrix 'matrix' and dimension 'dim' of submatrix and creates square output matrix 2 x 2.
	Each item in the output matrix equals to sum of all items in square submatrix of size dim x dim
	in corresponding corner of the original matrix.
 
	:param matrix: 2D np.array (matrix) M x N
	:param dim: integer, dimension of submatrix which is used to count one value in output matrix
	:return: 2D np.array 2 x 2 (square matrix)
 
	Examples:
 
	>>> m = np.array([
	...		[2,6,9,5,2,4,5],
	... 	[6,2,9,6,7,3,5],
	... 	[3,9,1,6,7,8,2],
	... 	[5,6,9,3,7,9,1],
	... 	[6,8,2,9,1,4,7]
	...	])
	>>> result = np.array([
	...		[16,17],
	...		[25,21]
	...	])
	>>> np.array_equal(task02(m, 2), result)
	True
	'''
	pass
 
def task03(v1, v2):
	'''Returns angle in degrees between two 3D vectors
 
	:param v1: np.array with 3 items defining 3D vector
	:param v2: np.array with 3 items defining 3D vector
	:return: float, angle between vectors in degrees, rounded to 1 decimal place
 
	Examples:
 
	>>> vector1 = np.array([0,1,0])
	>>> vector2 = np.array([1,0,0])
	>>> task03(vector1, vector2)
	90.0
 
	>>> vector = np.array([1,8,5])
	>>> task03(vector, vector)
	0.0
 
	>>> vector1 = np.array([1,8,5])
	>>> vector2 = np.array([1,8,6])
	>>> task03(vector1, vector2)
	4.9
	'''
	pass
 
def task04(rain_matrix):
	'''Processes precipitation data collected during multi-day experiment
	and returns 2 x 2 matrix related to max. precipitation.
 
	There is always 24 columns in "rain_martix" data 
	as each column represents one hour period.
	Each row corresponds to one day. 
	Days are numbered starting from 0 and going up to N - 1 (for N-day long experiment).
	That means that row index in 'rain_matrix' corresponds directly to day number since the beginning of the experiment.
	Each value in the 'rain_matrix' corresponds to precipitation measured on particlar day during particular 1 hour period. 
 
	Function returns square matrix 2 x 2 containging following data:
 
	rp: rainfall peak (maximum precipitation in mm/h found during whole experiment)
	rp_day: rainfall peak day (day number on which rainfall_peak occured)
	mdp: maximum daily precipitation (maximum precipitation over 24-hour period)
	mdp_day: most daily precipitation day (day number on which max_daily_precipitation occured or the most rainy day)
 
	Structure of output matrix is following:
	[[mdp_day, rp_day],
	 [mdp, rp]] 		
 
	For example in 'rain_data' matrix example bellow precipitation on day 3 and hour 0 was 8.
 
	Notice that precipitation data sometimes contain negative values. 
	Negative values were caused by faulty rain gauge and are ignored (replaced by value 0).
 
	:param rain_matrix: 2D np.array (matrix) M x 24 containing integer precipitation data in mm/h 
	:return: 2D np.array (matrix) 2 x 2 containing integer values 
 
	Example:
 
	>>> rain_data = np.array([
	...		[-8, 0, 0, 0,-3, 0,-8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 3, 0, 9, 5],
	...		[ 0,-4, 0,-2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	...		[ 0,-4, 0, 0, 0, 10, 0, 0, 0,-6, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
	...		[ 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 1, 0, 0, 3]
	... ])
	>>> result = np.array([
	...		[ 0, 2],
	...		[23,10]
	... ])
	>>> np.array_equal(task04(rain_data), result)
	True
	'''
	pass
 
 
def task05(n, m):
	'''
	Creates an nxm NumPy array with a checkerboard pattern of 0 and 1.
	The pattern starts with 0 in the top-left corner.
 
	:param n: int, number of rows
	:param m: int, number of columns
	:return: 2D np.array (matrix) nxm
 
	Examples:
 
	>>> result = np.array([[0, 1, 0, 1],
	...                    [1, 0, 1, 0],
	...                    [0, 1, 0, 1],
	...                    [1, 0, 1, 0]])
	>>> np.array_equal(task05(4, 4), result)
	True
 
	>>> result = np.array([[0, 1, 0, 1, 0],
	...                    [1, 0, 1, 0, 1],
	...                    [0, 1, 0, 1, 0]])
	>>> np.array_equal(task05(3, 5), result)
	True
	'''
	pass
 
def task06(matrix):
	'''Finds the maximum value in the input matrix that is smaller or equal to the average of the matrix.
 
	:param matrix: 2D np.array of numbers
	:return: The maximum value among the elements that is <= the average of the matrix
 
	Examples:
 
	>>> matrix = np.array([[5, 2, 7], [9, 1, 6]])
	>>> task06(matrix)
	5
 
	>>> matrix = np.array([[4, 5], [6, 7]])
	>>> task06(matrix)
	5
	'''
	pass
 
if __name__ == '__main__':
	import doctest
 
	doctest.testmod(verbose=True)

courses/be5b33pge/practices/04.txt · Last modified: 2025/03/12 17:32 by parildav