Lecture 3 Flashcards

1
Q

a = [‘hi’]

a += ‘hello’

A

a == [‘hi’, ‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

should do a += [‘hello’]

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

a.append()
a += []
a = a + []
Difference?

A

append and += are the same, modifying a. However, the 3rd one creates a new object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
a = [0,1,2,3,4,5,6,7,8,9]
a[1::2][::-1] = ?
A

[9,7,5,3,1] (successive filters)

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

A list passed in a function has “a.append(b)”. An integer passed in a function has “a+=1”. Which variable is modified?

A

Only mutable variables (if not redefined as a local variable inside the function i.e. a=[‘ok’]). Int won’t be modified!

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

Transpose of a matrix

A
np.transpose(A)
A.T
A.transpose()
A.swapaxes(0,1)
Be careful, A.reshape() won't give the transpose!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Broadcasting, when are 2 matrices compatibles?

A

Must have a dimension in common, it’ll be stretched along the other axis which must be of dimension 1.
ex: (7,5,9) and (1,5,1)
A (5,1) matrix would be interpreted as (1,5,1)

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

Get unique elements of an array

A

np.unique(x)

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

Get a list of elements in a matrix

A

x.flatten()

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

Modify numpy type

A

x.astype(‘int’)

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

Resize a matrix

A

Modify: x.resize(x,y)
Copy: x.reshape(x,y)

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

Generate a numpy sequence of numbers

A

arange()

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

Inner product

A

inner(a,b) = a1b1+a2b2+a3b3 (Same as dot product), A@B

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

Outer product

A

outer(u,v) = v u^T

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