Numpy
是Python
下一个非常强大的库。在这篇笔记里我将会把CS231n课程用到的一些Python
和Numpy
的用法用通俗易懂的语言和例子记录下来,方便自己复习也方便他人学习。这里附上Numpy
的官方链接。
1.enumerate
:不是单纯的打印内容,枚举的时候还会加上index
>>> classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
>>> for y, cls in enumerate(classes):
... print y, cls
0 plane
1 car
2 bird
3 cat
4 deer
5 dog
6 frog
7 horse
8 ship
9 truck
2.np.flatnonzero()
:打印非零元素的下标,具体如下
>>> x = np.arange(-2, 3)
>>> x
array([-2, -1, 0, 1, 2])
>>> np.flatnonzero(x)
array([0, 1, 3, 4])
3.numpy.random.randint(low, high=None, size=None, dtype='l')
:打印[low,high)之间的整数;如果high没有定义,那么就从[0,low)
>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
>>> np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
#If high is None (the default), then results are from [0, low).
#如果high没有定义,那么就默认从[0,low)
>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
4.numpy.random.choice(a, size=None, replace=True, p=None)
参数说明
a : 1-D array-like or int
If an ndarray, a random sample is generated from its elements.
If an int, the random sample is generated as if a was np.arange(n)
如果a是矩阵,那么结果就是从矩阵a中随机挑size个数出来重新生成array
如果a是一个数,那就从np.arange(a)中随机挑size个数出来重新生成array
size : int or tuple of ints, optional
replace : boolean, optional
Whether the sample is with or without replacement
p : 1-D array-like, optional
The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.
>>> np.random.choice(5, 3)
array([0, 3, 4])
#a : If an ndarray, a random sample is generated from its elements.
#If an int, the random sample is generated as if a was np.arange(n)
#This is equivalent to np.random.randint(0,5,3)
#从arange(5)里面挑选3个出来
这个参数里面有个replacement看不明白,差了半天,终于在StackOverflow上面找到了答案。A&Q如下
Q:What does replacement mean in numpy.random.choice?
A:It controls whether the sample is returned to the sample pool. If you want only unique samples then this should be false.
大致意思就是如果想要生成的是不重复的,请设置replace = False
5.numpy.reshape(a, newshape, order='C')
:当newshape里面出现了-1
>>> a = np.array([[1,2,3], [4,5,6]])
>>> np.reshape(a, (3,-1)) # the unspecified value is inferred to be 2
array([[1, 2],
[3, 4],
[5, 6]])
讲下新用法,给出一个mn的矩阵,如果newshape给的参数是(x, -1),那么函数会自动判别newshape为(x, mn/x),这里的x一定要能被mn整除!*
6.numpy.sum(a,axis = )
平日里一直以为axis = 1 是按照列相加的,以前一直记错了,其实是按照行相加的,然后重新生成一个数组。
7.numpy.argsort()
:常见用法,遇到很多numpy输出的都是下标,这个也不例外!!!
>>> a = numpy.array([1,2,0,5,3])
>>> numpy.argsort(a)
array([2, 0, 1, 4, 3], dtype=int64)
>>> a[numpy.argsort(a)]
array([0, 1, 2, 3, 5])
np.argsort(a)
的结果仅仅是下标!, a[np.argsort(a)]
的结果才是最终排好序的结果。
8.U1 = np.random.rand(*H1.shape) < p
:
乖乖,我孤陋寡闻了,以前都没见到过加*号的
>>> np.random.rand(a.shape)
Traceback (most recent call last):
File "<ipython-input-10-596e2a7492cd>", line 1, in <module>
np.random.rand(a.shape)
File "mtrand.pyx", line 1623, in mtrand.RandomState.rand (numpy\random\mtrand\mtrand.c:17636)
File "mtrand.pyx", line 1143, in mtrand.RandomState.random_sample (numpy\random\mtrand\mtrand.c:13908)
File "mtrand.pyx", line 163, in mtrand.cont0_array (numpy\random\mtrand\mtrand.c:2055)
TypeError: an integer is required
>>> np.random.rand(*a.shape)
array([ 0.10049452, 0.49159476, 0.3668072 ])
经过这两步,就清清楚楚了。np.random.rand()括号里加的是个int型的数,而a.shape结果并不是一个int型的数,这时候就要在a.shape前面加个*号了。
9.numpy.binicount(x, weight = None, minlength = None)
>>> x = np.array([0, 1, 1, 3, 2, 1, 7])
>>> np.bincount(x)
array([1, 3, 1, 1, 0, 0, 0, 1])
我们可以看到x中最大的数为7,因此结果的长度是7+1个
索引0(数0)出现了1次,索引1(数1)出现了3次......索引5(数5)出现了0次......
暂时写到这里,以后有用到其他numpy不常见的用法都会在这个笔记下面补充。
网友评论