numpy.log10()
方法用于计算数组中元素的以 10 为底的对数。
示例
import numpy as np
# create an array
array1 = np.array([1, 10, 100, 1000])
# calculate base-10 logarithm of array1 elements
result = np.log10(array1)
print(result)
# Output: [0. 1. 2. 3.]
log10() 语法
numpy.log10()
方法的语法是:
numpy.log10(x, out = None, where = True, casting = 'same_kind', dtype = None)
log10() 参数
numpy.log10()
方法接受以下参数:
x
- 输入数组out
(可选) - 用于存储结果的输出数组where
(可选) - 一个布尔数组,用于指示计算对数的位置casting
(可选) - 转换数据类型时的转换行为dtype
(可选) - 返回输出的数据类型
log10() 返回值
numpy.log10()
方法返回一个包含相应以 10 为底的对数值的数组。
示例 1:在 log10() 中使用 out 和 where
import numpy as np
array1 = np.array([1, -10, 10, 100, -1000, 10000])
# create an output array with the same shape as array1
result = np.zeros_like(array1, dtype=float)
# compute log10 of elements in array1
# only where the element is greater than 1
np.log10(array1, where = array1 > 1, out = result)
print(result)
输出
[0. 0. 1. 2. 0. 4.]
在此,numpy.log10()
方法用于计算 array1 中元素的以 10 为底的对数。我们向此方法提供了以下附加参数:
where
用于指定条件array1 > 1
,表示仅为大于 1 的元素计算对数。out
设置为 result,将计算出的对数值存储在 result 数组中。
注意: 负值的对数不会被计算,并且 result 数组中对应的元素将被赋值为零。
示例 2:在 log10() 中使用可选的 casting 参数
casting
参数指定了在转换数据类型时的转换行为。
casting 可以是:
'no'
- 完全不允许数据类型转换'equiv'
- 只允许字节顺序的更改'safe'
- 只允许保留数值精度的转换'same_kind'
- 只允许安全转换或相同种类内的转换'unsafe
' - 允许任何数据转换
让我们看一个例子。
import numpy as np
# array of floating-point numbers
array1 = np.array([1, -2, 0, 4, -5], dtype = np.float32)
# no casting is allowed, same data type as array1 is maintained
array2 = np.sign(array1, casting = 'no')
# casting is allowed to equivalent data types (floating-point numbers)
array3 = np.sign(array1, casting = 'equiv')
# casting is allowed to safe data types
# preserving precision (floating-point numbers)
array4 = np.sign(array1, casting = 'safe')
# casting is allowed to data types of
# the same kind (floating-point numbers)
array5 = np.sign(array1, casting = 'same_kind')
# casting is allowed to any data type
# without checks (signed integers)
array6 = np.sign(array1, casting = 'unsafe')
print("Array with 'no' casting:", array2)
print("Array with 'equiv' casting:", array3)
print("Array with 'safe' casting:", array4)
print("Array with 'same_kind' casting:", array5)
print("Array with 'unsafe' casting:", array6)
输出
Array with 'no' casting: [ 1. -1. 0. 1. -1.] Array with 'equiv' casting: [ 1. -1. 0. 1. -1.] Array with 'safe' casting: [ 1. -1. 0. 1. -1.] Array with 'same_kind' casting: [ 1. -1. 0. 1. -1.] Array with 'unsafe' casting: [ 1. -1. 0. 1. -1.]
注意:numpy.log10()
方法不涉及类型转换,因此 casting
参数不会影响结果。
示例 3:使用 dtype 参数计算 log10()
import numpy as np
array1 = np.array([1, 10, 100, 1000, 10000])
# compute the log10 with dtype = float64
result1 = np.log10(array1, dtype = np.float64)
print("Result with dtype float64:", result1)
# convert the logarithmic values to integers
result2 = np.round(result1).astype(int)
print("Result with dtype int:", result2)
输出
Result with dtype float64: [0. 1. 2. 3. 4.] Result with dtype int: [0 1 2 3 4]
在此示例中,我们使用 np.log10()
和 dtype = np.float64
计算了对数值。
然后,我们使用 np.round()
将浮点数值四舍五入到最接近的整数。最后,我们使用 astype(int)
将四舍五入后的值转换为整数。
现在,result2 数组包含四舍五入到最接近整数的对数值。