CS231n学习笔记-1.Python&Numpy学习

NumpyPython下一个非常强大的库。在这篇笔记里我将会把CS231n课程用到的一些PythonNumpy的用法用通俗易懂的语言和例子记录下来,方便自己复习也方便他人学习。这里附上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]])

讲下新用法,给出一个m*n的矩阵,如果newshape给的参数是(x, -1),那么函数会自动判别newshape为(x, m*n/x),这里的x一定要能被m*n整除!


6.numpy.sum(a,axis = )

>>> x = [1, 2, 3]
>>> np.sum(x, axis = 0)
6
>>> np.sum(x, axis = 1)
ValueError: 'axis' entry is out of bounds

输入为一个列表或者一维矩阵,axis = 1会报错,而用axis = 0是不会错的

>>> x = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
>>> np.sum(x, axis = 1)
array([ 6, 15, 24])
>>> np.sum(x, axis = 0)
array([ 12, 15, 18])

所以可以看出当输入是多维的时候,axis = 1是按行相加,axis = 0是按列相加。

  • 分清楚输入的维度,当是一维的时候只能用axis = 0, 多维的时候axis = 1是按照行来相加, axis = 0是按照列来相加

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不常见的用法都会在这个笔记下面补充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值