sciPy stats.signaltonoise()函数| Python
scipy.stats.signaltonoise(arr, axis=0, ddof=0)函数计算输入数据的信噪比。
其公式:
Parameters :
arr : [array_like]Input array or object having the elements to calculate the signal-to-noise ratio
axis : Axis along which the mean is to be computed. By default axis = 0.
ddof : Degree of freedom correction for Standard Deviation.
Results : mean to standard deviation ratio i.e. signal-to-noise ratio.
代码 #1:工作
# stats.signaltonoise() method
import numpy as np
from scipy import stats
arr1 = [[20, 2, 7, 1, 34],
[50, 12, 12, 34, 4]]
arr2 = [50, 12, 12, 34, 4]
print ("\narr1 : ", arr1)
print ("\narr2 : ", arr2)
print ("\nsignaltonoise ratio for arr1 : ",
stats.signaltonoise(arr1, axis = 0, ddof = 0))
print ("\nsignaltonoise ratio for arr1 : ",
stats.signaltonoise(arr1, axis = 1, ddof = 0))
print ("\nsignaltonoise ratio for arr1 : ",
stats.signaltonoise(arr2, axis = 0, ddof = 0))
输出 :
arr1 : [[20, 2, 7, 1, 34], [50, 12, 12, 34, 4]]
arr2 : [50, 12, 12, 34, 4]
signaltonoise ratio for arr1 : [2.33333333 1.4 3.8 1.06060606 1.26666667]
signaltonoise ratio for arr1 : [1.01779811 1.31482934]
signaltonoise ratio for arr2 : 1.3148293369202024
代码#2:如何实现
def signaltonoise(a, axis, ddof):
a = np.asanyarray(a)
m = a.mean(axis)
sd = a.std(axis = axis, ddof = ddof)
return np.where(sd == 0, 0, m / sd)
print ("\nsignaltonoise ratio for arr1 : ",
signaltonoise(arr1, axis = 0, ddof = 0))
print ("\nsignaltonoise ratio for arr1 : ",
signaltonoise(arr1, axis = 1, ddof = 0))
print ("\nsignaltonoise ratio for arr2 : ",
signaltonoise(arr2, axis = 0, ddof = 0))
输出 :
signaltonoise ratio for arr1 : [2.33333333 1.4 3.8 1.06060606 1.26666667]
signaltonoise ratio for arr1 : [1.01779811 1.31482934]
signaltonoise ratio for arr2 : 1.3148293369202024