我写了一个代码来查找 zscore 但它的输出大于 1 ,而且它的标准偏差不是 1,这里的错误是什么

原文标题I have written a code to find zscore but it’s output is greater than 1 , also it’s standard deviation is not 1, what is the error here

我已经编写了一个代码来从头开始标准化数据,但问题是,它的输出有时大于 1,我不知道是什么问题。

如果我做了一些愚蠢的事情,请指出我的错误,

这是代码

import numpy as np
import matplotlib.pyplot as plt
def std(x):
    x = x.copy()
    mean = np.mean(x,axis=1, keepdims=True)
    x = x-mean
    x/=np.std(x)
    
    return x
x = np.array([[1,2,3,3.6,7,85,23]])
print(std(x))

输出 :

[[-0.59333009 -0.55801282 -0.52269556 -0.5015052  -0.38142649  2.37332037
   0.18364979]]

原文链接:https://stackoverflow.com//questions/71448497/i-have-written-a-code-to-find-zscore-but-its-output-is-greater-than-1-also-it

回复

我来回复
  • AJH的头像
    AJH 评论

    请注意:如果“标准化数据”是指 0 和 stdev 1,则不能保证所有值都在 -1 和 1 之间;正态分布中只有 68% 的值介于 -σ 和 σ 之间。

    在你的x数组中,85 是一个非常明显的异常值,这就是为什么对应的值大于 1。还要注意,有这么大的异常值会严重扭曲你的 stdev 计算。

    请注意,在函数内部复制x对您没有任何帮助。因为您调用x作为函数的参数,所以对x所做的任何更改仅适用于函数内部,全局x不会更改,除非您使用覆盖全局x(例如x = my_func(x))的函数。您的函数的本地范围也意味着函数内的x副本在函数使用后消失。

    如果您尝试获取 0 到 1 之间的所有值,则可以这样做:

    def standardize_x(x):
        x -= np.mean(x)
    
        # The following 2 lines make the distance between min(x) and max(x) 1.
        # In the example, this makes the new range between -0.2 and 0.8.
    
        x /= (np.amax(x) - np.amin(x))
        x -= np.mean(x)
    
        # Move x up to start at 0, multiply by 2 so that range is from 0 to 2,
        # then subtract 1 so new minimum is -1 and new maximum is 1.
    
        x = 2*(x - np.amin(x)) - 1
        
        return x
    
    # This will not overwrite the original x array.
    standardized_x = standardize_x(x)
    

    让我知道是否有任何需要澄清的地方。

    2年前 0条评论