本文簡要介紹 python 語言中 scipy.sparse.random
的用法。
用法:
scipy.sparse.random(m, n, density=0.01, format='coo', dtype=None, random_state=None, data_rvs=None)#
生成具有隨機分布值的給定形狀和密度的稀疏矩陣。
警告
從 numpy 1.17 開始,為
random_state
傳遞np.random.Generator
(例如np.random.default_rng
)將導致執行時間更快。為了向後兼容,默認使用慢得多的實現。
- m, n: int
矩陣的形狀
- density: 真實的,可選的
生成矩陣的密度:密度等於 1 表示滿矩陣,密度為 0 表示沒有非零項的矩陣。
- format: str,可選
稀疏矩陣格式。
- dtype: dtype,可選
返回矩陣值的類型。
- random_state: {無,整數,
numpy.random.Generator
, numpy.random.RandomState
}, optional如果種子是無(或np.random), 這
numpy.random.RandomState
使用單例。如果種子是一個 int,一個新的
RandomState
使用實例,播種種子.如果種子已經是一個
Generator
或者RandomState
實例然後使用該實例。
該隨機狀態將用於對稀疏結構進行采樣,但不一定用於對矩陣的結構非零條目的值進行采樣。
- data_rvs: 可調用的,可選的
對請求數量的隨機值進行采樣。這個函數應該接受一個參數,指定它將返回的 ndarray 的長度。稀疏隨機矩陣的結構非零條目將從該函數采樣的數組中獲取。默認情況下,統一的 [0, 1) 隨機值將使用與采樣稀疏結構相同的隨機狀態進行采樣。
- res: 稀疏矩陣
參數 ::
返回 ::
例子:
傳遞
np.random.Generator
實例以獲得更好的性能:>>> from scipy.sparse import random >>> from scipy import stats >>> from numpy.random import default_rng >>> rng = default_rng() >>> S = random(3, 4, density=0.25, random_state=rng)
證明值的采樣器:
>>> rvs = stats.poisson(25, loc=10).rvs >>> S = random(3, 4, density=0.25, random_state=rng, data_rvs=rvs) >>> S.A array([[ 36., 0., 33., 0.], # random [ 0., 0., 0., 0.], [ 0., 0., 36., 0.]])
使用自定義發行版:
>>> class CustomDistribution(stats.rv_continuous): ... def _rvs(self, size=None, random_state=None): ... return random_state.standard_normal(size) >>> X = CustomDistribution(seed=rng) >>> Y = X() # get a frozen version of the distribution >>> S = random(3, 4, density=0.25, random_state=rng, data_rvs=Y.rvs) >>> S.A array([[ 0. , 0. , 0. , 0. ], # random [ 0.13569738, 1.9467163 , -0.81205367, 0. ], [ 0. , 0. , 0. , 0. ]])
相關用法
- Python SciPy sparse.rand用法及代碼示例
- Python SciPy sparse.isspmatrix用法及代碼示例
- Python SciPy sparse.save_npz用法及代碼示例
- Python SciPy sparse.issparse用法及代碼示例
- Python SciPy sparse.coo_matrix用法及代碼示例
- Python SciPy sparse.isspmatrix_csc用法及代碼示例
- Python SciPy sparse.isspmatrix_csr用法及代碼示例
- Python SciPy sparse.tril用法及代碼示例
- Python SciPy sparse.coo_array用法及代碼示例
- Python SciPy sparse.dia_array用法及代碼示例
- Python SciPy sparse.bmat用法及代碼示例
- Python SciPy sparse.hstack用法及代碼示例
- Python SciPy sparse.dia_matrix用法及代碼示例
- Python SciPy sparse.find用法及代碼示例
- Python SciPy sparse.isspmatrix_dia用法及代碼示例
- Python SciPy sparse.isspmatrix_lil用法及代碼示例
- Python SciPy sparse.csc_matrix用法及代碼示例
- Python SciPy sparse.block_diag用法及代碼示例
- Python SciPy sparse.diags用法及代碼示例
- Python SciPy sparse.vstack用法及代碼示例
- Python SciPy sparse.dok_matrix用法及代碼示例
- Python SciPy sparse.kron用法及代碼示例
- Python SciPy sparse.identity用法及代碼示例
- Python SciPy sparse.spdiags用法及代碼示例
- Python SciPy sparse.isspmatrix_coo用法及代碼示例
注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.sparse.random。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。