LAB 02.02 - Numpy
Contents
LAB 02.02 - Numpy¶
!wget --no-cache -O init.py -q https://raw.githubusercontent.com/fagonzalezo/ai4eng-unal/main/content/init.py
import init; init.init(force_download=False); init.get_weblink()
from local.lib.rlxmoocapi import submit, session
session.LoginSequence(endpoint=init.endpoint, course_id=init.course_id, lab_id="L02.02", varname="student");
import numpy as np
Task 1: Cauchy Matrix¶
Given two vectors (1D numpy
arrays), \(x\) and \(y\), build the Cauchy Matrix:
See https://en.wikipedia.org/wiki/Cauchy_matrix, the elements of the matrix are the results of substracting the correpsonding positions in the \(x\) and \(y\) vectors where the rows correspond to \(x\) and the columns to \(y\).
NOTE: if there is any division by zero, you must raise a ValueError
exception.
HINT: create a matrix with zeros with the desired output shape, use broadcasting of \(x\) as row vector, and then broadcasting of \(y\) as column vector with reshape(-1,1)
CHALLENGE: build a function with maximum four lines of code, including checking for zeros and raising the exception.
Execution example
>> x = np.array([45, 31, 67, 75, 54])
>> y = np.array([17, 7, 15, 15, 18])
>> cauchy(c,y)
array([[0.03571429, 0.02631579, 0.03333333, 0.03333333, 0.03703704],
[0.07142857, 0.04166667, 0.0625 , 0.0625 , 0.07692308],
[0.02 , 0.01666667, 0.01923077, 0.01923077, 0.02040816],
[0.01724138, 0.01470588, 0.01666667, 0.01666667, 0.01754386],
[0.02702703, 0.0212766 , 0.02564103, 0.02564103, 0.02777778]])
def cauchy(x, y):
r = ... # your code here
return r
check your code manually
x = np.array([45, 31, 67, 75, 54])
y = np.array([17, 7, 15, 15, 18])
cauchy(x,y)
this execution should raise a ValueError
exception
x = np.array([45, 31, 67, 75, 54])
y = np.array([17, 7, 15, 75, 18])
cauchy(x,y)
submit your code
student.submit_task(globals(), task_id="task_01");
Task 2: Position of closest scalar¶
Given a 1D vector \(x\), find the position of the closest element to \(v\)
Execution example:
>> x=np.arange(25,55,3)
>> minimo(x,34)
3
HINT: use np.argmin
CHALLENGE: solve it with one line of code
def minimo(x,v):
return ... # your code here
check manually your code
x = np.arange(25,55,3)
v = 34
minimo(x,v)
submit your code
student.submit_task(globals(), task_id="task_02");
Task 3: Substracting row mean¶
Given a matrix, your function must return a new one with the same dimensions in which each component is substracted the mean of its own row.
Execution example
>> X = np.array([[1, 2, 3], [4, 5, 6],[7,8,9]])
>> media(X)
array([[-1., 0., 1.],
[-1., 0., 1.],
[-1., 0., 1.]])
HINT: use broadcasting
CHALLENGE: solve it with one line of code
def media(X):
return ... # your code here
check your code manually
X = np.array([[1, 2, 3], [4, 5, 6],[7,8,9]])
media(X)
submit your code
student.submit_task(globals(), task_id="task_03");
Task 4: Double the diagonal¶
Complete the following function such that it returns the same matrix received in \(X\) but with its diagonal multiplied by 2. Assume \(X\) is a square matrix (with the same number of rows and columns).
Execution example
>> X = np.array([[79, 45, 67, 8, 37],
>> [47, 40, 5, 79, 86],
>> [72, 25, 44, 45, 22],
>> [12, 85, 8, 53, 28],
>> [ 4, 37, 36, 40, 16]])
>>
>> doublediag(X)
array([[158., 45., 67., 8., 37.],
[ 47., 80., 5., 79., 86.],
[ 72., 25., 88., 45., 22.],
[ 12., 85., 8., 106., 28.],
[ 4., 37., 36., 40., 32.]])
HINT: use np.eye
CHALLENGE: solve with one line of code
def doublediag(X):
return ... # your code here
check manually your code
X = np.array([[79, 45, 67, 8, 37],
[47, 40, 5, 79, 86],
[72, 25, 44, 45, 22],
[12, 85, 8, 53, 28],
[ 4, 37, 36, 40, 16]])
doublediag(X)
submit your code
student.submit_task(globals(), task_id="task_04");