Numpy
是Python
下一個(gè)非常強(qiáng)大的庫(kù)赖歌。在這篇筆記里我將會(huì)把CS231n課程用到的一些Python
和Numpy
的用法用通俗易懂的語(yǔ)言和例子記錄下來(lái),方便自己復(fù)習(xí)也方便他人學(xué)習(xí)。這里附上Numpy
的官方鏈接违孝。
1.enumerate
:不是單純的打印內(nèi)容嗤攻,枚舉的時(shí)候還會(huì)加上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()
:打印非零元素的下標(biāo)腺怯,具體如下
>>> 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)之間的整數(shù);如果high沒(méi)有定義川无,那么就從[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沒(méi)有定義呛占,那么就默認(rèn)從[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)
參數(shù)說(shuō)明
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是矩陣,那么結(jié)果就是從矩陣a中隨機(jī)挑size個(gè)數(shù)出來(lái)重新生成array
如果a是一個(gè)數(shù)懦趋,那就從np.arange(a)中隨機(jī)挑size個(gè)數(shù)出來(lái)重新生成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個(gè)出來(lái)
這個(gè)參數(shù)里面有個(gè)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.
大致意思就是如果想要生成的是不重復(fù)的走贪,請(qǐng)?jiān)O(shè)置replace = False
5.numpy.reshape(a, newshape, order='C')
:當(dāng)newshape里面出現(xiàn)了-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]])
講下新用法,給出一個(gè)mn的矩陣惑芭,如果newshape給的參數(shù)是(x, -1),那么函數(shù)會(huì)自動(dòng)判別newshape為(x, mn/x),這里的x一定要能被mn整除坠狡!*
6.numpy.sum(a,axis = )
平日里一直以為axis = 1 是按照列相加的,以前一直記錯(cuò)了遂跟,其實(shí)是按照行相加的逃沿,然后重新生成一個(gè)數(shù)組。
7.numpy.argsort()
:常見用法,遇到很多numpy輸出的都是下標(biāo)幻锁,這個(gè)也不例外?痢!哄尔!
>>> 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)
的結(jié)果僅僅是下標(biāo)假消!, a[np.argsort(a)]
的結(jié)果才是最終排好序的結(jié)果岭接。
8.U1 = np.random.rand(*H1.shape) < p
:
乖乖富拗,我孤陋寡聞了,以前都沒(méi)見到過(guò)加*號(hào)的
>>> 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 ])
經(jīng)過(guò)這兩步鸣戴,就清清楚楚了啃沪。np.random.rand()括號(hào)里加的是個(gè)int型的數(shù),而a.shape結(jié)果并不是一個(gè)int型的數(shù)窄锅,這時(shí)候就要在a.shape前面加個(gè)*號(hào)了创千。
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中最大的數(shù)為7,因此結(jié)果的長(zhǎng)度是7+1個(gè)
索引0(數(shù)0)出現(xiàn)了1次入偷,索引1(數(shù)1)出現(xiàn)了3次......索引5(數(shù)5)出現(xiàn)了0次......
暫時(shí)寫到這里追驴,以后有用到其他numpy不常見的用法都會(huì)在這個(gè)筆記下面補(bǔ)充。