pythonscipystatz-score

Z-Score on Scipy


I need to find out the Zscore pertaining to 1 specific point, that is, for 1 value of X using Scipy.

Below is the manual code:

data = [25, 37, 15, 36, 92, 28, 33, 40]
mean = sum(data)/len(data)
summation = 0
for i in range(0, len(data)):
  summation += (data[i]-mean)**2
std = ((1/len(data))*summation))**(1/2)
Z = (x-mean)/std

Next, when I try to do the same with Scipy:

Z = stats.zscore(data)

I get the output:

[-0.61219538 -0.05775428 -1.07422964 -0.10395771  2.48343411 -0.47358511
 -0.24256798  0.08085599]

Maybe because I am passing only one parameter and that is the data itself. How to get the Z-Score for only one value of X?


Solution

  • This solution might be suitable for you. This solution might be better than others.

    from scipy import stats
    import numpy as np
    
    data = [25, 37, 15, 36, 92, 28, 33, 40]
    x = 40 # Please assign a specific value
    
    mean = np.mean(data)
    std = np.std(data, ddof=0)
    Z = (x - mean) / std
    print(Z)
    

    You might like this method also (scipy.stats.zscore):

    from scipy import stats
    data = [25, 37, 15, 36, 92, 28, 33, 40]
    x = 40
    all_zscores = stats.zscore(data, ddof=0)
    x_index = data.index(x)
    Z = all_zscores[x_index]
    print(Z)
    

    Ouput:

    0.08085599417810461