Chapter2-Numpy Flashcards

1
Q

Dot product
v transpose dot w

A

np.dot(v.T, w)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

v.T.shape

A

get the shape (number of lines, number of columns) of v transpose

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how to create a 2x2 Matrix A as 2D Numpy Array having the elements 1234?

A

A = np.array([[1,2],[3,4]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

create a vector vdont with shape(3,). Is it a mathematical vector?

A

vdont = np.array([1,1,1])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

if x is a numpy array([[9]]) what is the output of print(x.item())

A

when that array contains exactly one item results on this element: 9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how to get the norm of a vector v?

A

np.linalg.norm(v)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

matrix A with shape (3,2) and vector u with shape (2,1). Is it possible in Python to calculate A dot u? If yes, what is the shape of the result?

A

Yes because the number of columns of the matrix is equal the number of elements of the vector. The resulting vector will have shape (3,1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the definition of the identity matrix? How to create one identity matrix I3 of grad 3?

A

definition: Matrix with all elements in the main diagonal set to 1. I3= np.identity(3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is the result np.dot(I3, w3) for all vectors w3 of shape (3,*)?

A

the vector w3 again

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How is the inverse of a Matrix defined and when does the inverse of a matrix exist?

A

Mathematically, the inverse of a matrix A is denoted as A⁻¹, and it satisfies the equation:

A * A⁻¹ = A⁻¹ * A = I

follow criteria must be met:

  1. Square Matrix: The matrix must be a square matrix, which means it has the same number of rows and columns
  2. Full Rank: The matrix must be of full rank. A matrix is said to be of full rank if its rows and columns are linearly independent.
  3. Non-Singular (or Non-Degenerate): A matrix is invertible if and only if it is non-singular (or non-degenerate). A non-singular matrix is one that does not have a determinant of zero.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Given a system of equations in the form Ax = b, how to solve this system for x mathematicaly and in python?

A

math: x = A⁻¹ * b
python: x = np.dot(np.linalg.inv(A), b)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is the diference between np.dot(A, B) and A * B?

A

A * B ist the element-wise multiplication

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to obtain the type of data stored in one array A?

A

A.dtype

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how to create one column vector np array containing 10 equally spaced real number from 1 to 5?

A

np.linspace(1,5,10).reshape(10,1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what do the function calls np.hstack([v,w]) and np.vstack([v,w]) do?

A

np.hstack([v,w]): attach the vector w as a column to the vector v creting a matrix
np.vstack([v,w]): attach the vector w as a ‘line’ to the vector v creting a matrix

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what to get
array([[1, 2, 1, 2, 1, 2],
[3, 4, 3, 4, 3, 4]])
from
A = array([[1, 2],
[3, 4]])

A

np.tile(A, 3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

how to create one vector containing the natural numbers from 0 to 29?

A

np.arange(30)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

how to get the first column of a matrix?

A

A[:, 0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

which elements o the lines and columns of A define the new matrix extracted by view1 = A[0:3, 0:3]

A

line: elements 0, 1 and 2
column: elements 0, 1 and 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

considering isBo to be array([ True, False, False, True, False, True, True]) what does data[isBo, :] will retrieve?

A

True for every one that I want and false for every row I don’t want to extract

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

if data was defined as np.arrange(0, 28), how to get all the even numbers 0, 2, … , 26 ?

A

data[data%2==0]

22
Q

how to directly solve the system of linear equation A.x-> = b-> for x?

A

x will be np.linalg.solve(A, b)

23
Q

What is the result of the operation np.array([[3],[2]]) with shape (2,1) and np.array([[3, 2]]) with shape (1,2) ? why?

A

the result is np.array([[4,6],[3,5]] because o broadcasting

24
Q

create one object of random number generator on the variable rgn. Use rgn to generate one matrix of standard normal distributed numbers of shape (4,2) and store in col_means the vector of column means.
How to transform the matrix onto mean = 4, variance=5?

A
  • rgn = np.random.default_rgn()
  • X = rgn.standard_normal(size=(4,3))
  • col_means = X.mean(0)
  • X * np.sqrt(5) + 4
25
Q

what is the difference between X.mean(axis=0) and X.mean(axis=1)?

A
  • X.mean(axis=0) => jump along the axis of rows, calculating the mean for each column
  • X.mean(axis=1) => jump along the axis of columns, calculating the mean for each row
26
Q

Replace all odd numbers in arr x with -1 without changing x and store in y.

A

y = np.where(x%2 == 1, -1, x)

27
Q

how to get from (nxm) matrix X to non math array with shape (n*m, )?

A

X.flatten()

28
Q

how to apply one function in whole np array x?

A
  • 1 - define the function
  • 2 - vectorize de function: vectorizedfunction = np.vectorize(function)
  • 3 - call the vectorized function on the np array x : vectorizedfunction(x)
29
Q

how to sum all the elements of np array x?

A

np.add.reduce(x)

30
Q

how to multiply over all elements of np array x?

A

np.multiply.reduce(x)

31
Q

what are usually axis 0 and 1 in np?

A

axis 0 = lines, axis 1 = columns

32
Q

explain how numpy compaires shapes of elements to possible broadcasting them? Does both elements need to have same number of dimensions? What happens with missing dimensions like broadcasting (8,2) and (8,)?

A
  • element-wise starting from the rightmost working its way left. Two dim are compatible when they are equal or one of them are 1.
  • The resulting array will have the same number of dimensions as the input array with the greatest number of dimensions, where the size of each dimension is the largest size of the corresponding dimension among the input arrays.
  • broadcasting (8,2) and (8,) will not work because (8,) will be threated like (1,8)
33
Q

how to get the number of dimension of a matrix m?

A

m.ndim

34
Q

what is the requirement to apply reshape?

A

the new shape must contain the same number of entries ( e.g. rows* columns)

35
Q

how to swipe the second and third columns of a matrix u ? Explain the synthax.

A

u[:,[1,2]] = u[:,[2,1]]
creates a view where all rows and second, third columns is assigned to all rows and third, second columns

36
Q

if a vector v has shape (3,) what is the shape of v.transpose() ?

A

(3,)

37
Q

what does the method .all() do?

A

returns true if all elements of iterable are true

38
Q

what is the output of follow code and why?
v3 = np.array([‘a’, 1, 5.0])
print(v3[1] + v3[2])

A

15.0 => because of the ‘a’ v3 stores strings and the operator + turns to concatenation

39
Q

Is np.array([2,3,4,5,6,7]).reshape(3,2).T = np.array([2,3,4,5,6,7]).reshape(2,3) ?

A

NO!

40
Q

O que significa ter uma Metriz M que é tranformacao linear que leva um ponto P ate um ponto R? Como

A

M . P = R

41
Q

como mapear resolver A . P = b, conhecendo P e b:
- se b for invertable
- se b nao for invertable

A
  • A = np.dot(b, np.linalg.inv(P))
  • A = np.dot(b, np.linalg.pinv(P))
42
Q

How to determine the rank of a matrix ?

A

np.linalg.matrix_rank

43
Q

como calcular sin e cos?

A

sin_values = np.sin(angle)
cos_values = np.cos(angle)

44
Q

what does apply rotation r to a cube mean in terms of dot product?

A

cube . r

45
Q

how to use np.save and np.load ?

A

-np save: np.save(‘array.npy’, M)
-np load: array_load = np.load(‘array.npy’)

46
Q

hot to find the index of max value in an array?

A

np.argmax(array)

47
Q

which numbers are contained in the array np.arange(-1000, 1000)

A

intergers [-1000, -999, …, 998, 999]

48
Q

a é uma matriz 4x5 e s é uma view das colunas 1 e 2 de a. Apos s = s.reshape(2, 4), a ta,be, sera modificado?

A

Nao. s = s.reshape(2, 4) cria uma copia e nao uma view

49
Q

o que é uma fatias simples? o que é uma fatia avancada? Qual é a diferenca em termos de copia/view?

A

fatia simples: array[start:stop:step], cria view
fatia avancada: Utiliza índices avançados, como listas de índices, fatias não contínuas ou máscaras booleanas., cria copia

50
Q

df.loc[row_start:row_end, col_name] row_end é incluido ou nao?

A

sim. Para “loc” a selecao é inclusiva para o inicio e o fim.

51
Q
A