sciPy stats.signaltonoise() function | Python

• Last Updated : 18 Feb, 2019

scipy.stats.signaltonoise(arr, axis=0, ddof=0) function computes the signal-to-noise ratio of the input data.

Its formula : 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.

Code #1: Working

 # stats.signaltonoise() method import numpy as npfrom 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))

Output :

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

Code #2 : How to implement

 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))

Output :

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

My Personal Notes arrow_drop_up