sciPy stats.signaltonoise() function | Python

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.