Numpy 的 zeros(~)
方法創建一個全零作為其條目的 Numpy 數組。
參數
1.shape
| int
或 array-like
Numpy 數組的所需形狀。提供 int
將返回一維展平數組。
2. dtype
| string
或 type
| optional
Numpy 數組所需的數據類型。默認情況下,dtype=numpy.float64
。如果您確定新的 Numpy 數組僅包含整數,則應該指定 dtype=int
。
返回值
一個由零組成的 Numpy 數組,其形狀和類型由參數指定。
例子
創建一維 Numpy 數組
要創建包含 3 個零的扁平 Numpy 數組:
np.zeros(3)
array([0., 0., 0.])
創建 int 類型的 Numpy 數組
要創建具有 3 個零的 int
類型的扁平 Numpy 數組:
np.zeros(3, int)
array([0, 0, 0])
創建二維 Numpy 數組
使用元組
要使用元組創建一個用零填充的 2 x 3(即 2 行和 3 列)矩陣:
np.zeros((2,3))
array([[0., 0., 0.],
[0., 0., 0.]])
使用數組
要使用數組創建一個用零填充的 2 x 3(即 2 行和 3 列)矩陣:
np.zeros([2,3])
array([[0., 0., 0.],
[0., 0., 0.]])
請注意,您也可以使用 Numpy 數組。
相關用法
- Python NumPy zeros_like方法用法及代碼示例
- Python numpy matrix zeros()用法及代碼示例
- Python zip()用法及代碼示例
- Python zipfile.ZipFile.open用法及代碼示例
- Python zip用法及代碼示例
- Python zipfile.PyZipFile.writepy用法及代碼示例
- Python zlib.compress(s)用法及代碼示例
- Python zlib.decompress(s)用法及代碼示例
- Python zlib.adler32()用法及代碼示例
- Python zlib.crc32()用法及代碼示例
- Python zip方法用法及代碼示例
- Python string zfill()用法及代碼示例
- Python zipfile.Path.joinpath用法及代碼示例
- Python zoneinfo.ZoneInfo用法及代碼示例
- Python cudf.core.column.string.StringMethods.is_vowel用法及代碼示例
- Python NumPy fliplr方法用法及代碼示例
- Python torch.distributed.rpc.rpc_async用法及代碼示例
- Python torch.nn.InstanceNorm3d用法及代碼示例
- Python sklearn.cluster.MiniBatchKMeans用法及代碼示例
- Python pandas.arrays.IntervalArray.is_empty用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python numpy.less()用法及代碼示例
- Python Matplotlib.figure.Figure.add_gridspec()用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python Django File.save用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | zeros method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。