2017-06-19 86 views
0

什麼這兩行是什麼意思?Python語法numpy的隨機函數

1. np.random.seed(1) 
2. syn0 = 2*np.random.random((3,4))-1 

我知道,第一行是隨機生成的數字的起點,會做出什麼他們產生即使它們具有相同的出發點相同的序列?

第二行是權重的3×4矩陣的生成。 「1」是否與種子中的那個相關?我真的不明白,爲什麼有2 *,爲什麼他們是random.random

我期望的權重之和等於1種,因此在目的可能?

回答

1

作爲np.random是PRNG,它可以接種和它的種子被手動設置爲可再現的結果。現在,它正在接種1和種子然後np.random.random()構建你重置爲1

In [4]: np.random.seed? 
Docstring: 
seed(seed=None) 

Seed the generator. 

This method is called when `RandomState` is initialized. It can be 
called again to re-seed the generator. For details, see `RandomState`. 

Parameters 
---------- 
seed : int or array_like, optional 
    Seed for `RandomState`. 
    Must be convertible to 32 bit unsigned integers. 

隨機值矩陣後,你會總是得到相同的結果:

In [5]: np.random.random? 
Docstring: 
random_sample(size=None) 

Return random floats in the half-open interval [0.0, 1.0). 

其餘的是通常的numpy算術。 A - 1用於矩陣A意味着減去1逐元素從A。而2 * A是正常的標量操作。