cs231n-svm-note


import numpy as np

range 的用法

list(range(1,5))
[1, 2, 3, 4]
list(range(5))
[0, 1, 2, 3, 4]

np.random.choice 的用法·

mask = np.random.choice(100,10)
mask
array([78, 69, 24, 91, 24, 65, 16, 22, 80, 53])

rangrange 的用法

from random import randrange
randrange(5) # 随机取一个数字,不包含 endpoint
2
randrange(2,5)
2
[randrange(i) for i in (500,3073)]
[65, 39]

数组索引的一种方式

a = np.array([[1,2,3],[4,5,6],[7,8,9]])
a[(1,2)]
6
np.max((0,-1))
0
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
x = a[(1,1,2),(2,1,0)]
x
array([6, 5, 7])
np.sum(a,axis=0)
array([12, 15, 18])
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
a[(1,1,2),(2,1,0)] = a[(1,1,2),(2,1,0)] - np.array([0,1,2])
a
array([[1, 2, 3],
       [4, 4, 6],
       [5, 8, 9]])
a[1,:]
array([4, 4, 6])
import copy

copy.copy() 是浅拷贝,只拷贝父对象,不会拷贝父对象的子对象
copy.deepcopy() 深拷贝,拷贝父对象及其子对象

a = [1,2,3,4,['a','b']]
b = copy.copy(a)
c = copy.deepcopy(a)
d = a
a.append(5)
a[4].append('c')
print('a=', a)
print('b=', b)
print('c=', c)
print('d=', d)
a= [1, 2, 3, 4, ['a', 'b', 'c'], 5]
b= [1, 2, 3, 4, ['a', 'b', 'c']]
c= [1, 2, 3, 4, ['a', 'b']]
d= [1, 2, 3, 4, ['a', 'b', 'c'], 5]

文章作者: lovelyfrog
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 lovelyfrog !
  目录