Numpy 的 trunc(~)
方法刪除輸入數組中每個值的小數點。請參閱下麵的示例以進行說明。
參數
1. a
| array-like
輸入數組。
2. out
| Numpy array
| optional
您可以將計算結果放入 out
指定的數組中,而不是創建新數組。
3. where
| boolean
的array
| optional
標記為 False 的值將被忽略,即它們的原始值將未被初始化。如果指定了 out 參數,行為會略有不同 - 原始值將保持不變。由於這讓許多人感到困惑,請查看下麵的示例。
返回值
如果 a
是標量,則返回標量。否則,返回一個 Numpy 數組。
例子
基本用法
np.trunc([-3.2, -1.7, 1.5, 5.7])
array([-3., -1., 1., 5.])
指定輸出數組
a = np.zeros(3)
np.trunc([-3.2, -1.7, 1.5], out=a)
a
array([-3., -1., 1.])
指定布爾掩碼
np.trunc([-3.2, -1.7, 1.5], where=[False, True, False])
array([3454., -1., 1867.])
這裏,僅使用第二個數字進行計算,因為它在掩碼中具有相應的布爾值True
。您應該注意到 False
的值如何產生奇怪的結果 - 事實上,您應該忽略它們,因為它們是沒有實際用途的未初始化數字。
現在,如果您指定了 out
參數,而不是未初始化的值,則原始值將保持不變:
a = np.zeros(3)
np.trunc([-3.2, -1.7, 1.5], out=a, where=[False, True, False])
a
array([ 0., -1., 0.])
相關用法
- Python trunc()用法及代碼示例
- Python NumPy true_divide方法用法及代碼示例
- Python NumPy tri方法用法及代碼示例
- Python NumPy trim_zeros方法用法及代碼示例
- Python tracemalloc.Traceback.format用法及代碼示例
- Python NumPy transpose方法用法及代碼示例
- Python torch.distributed.rpc.rpc_async用法及代碼示例
- Python torch.nn.InstanceNorm3d用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python torchaudio.transforms.Fade用法及代碼示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代碼示例
- Python tf.summary.scalar用法及代碼示例
- Python tf.linalg.LinearOperatorFullMatrix.matvec用法及代碼示例
- Python tf.linalg.LinearOperatorToeplitz.solve用法及代碼示例
- Python tf.raw_ops.TPUReplicatedInput用法及代碼示例
- Python tf.raw_ops.Bitcast用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代碼示例
- Python Pandas tseries.offsets.CustomBusinessHour.onOffset用法及代碼示例
- Python torch.special.gammaincc用法及代碼示例
- Python typing.get_type_hints用法及代碼示例
- Python tensorflow.math.xlog1py()用法及代碼示例
- Python tf.compat.v1.Variable.eval用法及代碼示例
- Python tf.compat.v1.train.FtrlOptimizer.compute_gradients用法及代碼示例
- Python tf.distribute.OneDeviceStrategy.experimental_distribute_values_from_function用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | trunc method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。