Lecture 3 Flashcards
a = [‘hi’]
a += ‘hello’
a == [‘hi’, ‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
should do a += [‘hello’]
a.append()
a += []
a = a + []
Difference?
append and += are the same, modifying a. However, the 3rd one creates a new object.
a = [0,1,2,3,4,5,6,7,8,9] a[1::2][::-1] = ?
[9,7,5,3,1] (successive filters)
A list passed in a function has “a.append(b)”. An integer passed in a function has “a+=1”. Which variable is modified?
Only mutable variables (if not redefined as a local variable inside the function i.e. a=[‘ok’]). Int won’t be modified!
Transpose of a matrix
np.transpose(A) A.T A.transpose() A.swapaxes(0,1) Be careful, A.reshape() won't give the transpose!
Broadcasting, when are 2 matrices compatibles?
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)
Get unique elements of an array
np.unique(x)
Get a list of elements in a matrix
x.flatten()
Modify numpy type
x.astype(‘int’)
Resize a matrix
Modify: x.resize(x,y)
Copy: x.reshape(x,y)
Generate a numpy sequence of numbers
arange()
Inner product
inner(a,b) = a1b1+a2b2+a3b3 (Same as dot product), A@B
Outer product
outer(u,v) = v u^T