CS 231 n学習ノート-Python&Numpy学習

4649 ワード

NumpyPythonの次の非常に強力なライブラリです.このノートでは、CS 231 nコースで使用されている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.arang(n)aがマトリクスである場合、その結果、マトリクスaからランダムにsize個数を選択してarrayを再生成することになる.arang(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 "", line 1, in 
    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 ])

この2歩を経て,はっきりした.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ではあまり使われない使い方があるので、このメモの下で補足します.