📜  sciPy stats.signaltonoise()函数| Python

📅  最后修改于: 2022-05-13 01:54:29.930000             🧑  作者: Mango

sciPy stats.signaltonoise()函数| Python

scipy.stats.signaltonoise(arr, axis=0, ddof=0)函数计算输入数据的信噪比。

其公式:

代码 #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)) 

输出 :


代码#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))

输出 :