【问题标题】:Standardize Pixel Input Data with many Zeros标准化具有许多零的像素输入数据
【发布时间】:2020-03-16 14:50:15
【问题描述】:

我想为神经网络标准化我的输入数据。

数据如下所示:

data= np.array([[0,0,0,0,233,2,0,0,0],[0,0,0,23,50,2,0,0,0],[0,0,0,0,3,20,3,0,0]])

这是我使用的功能。由于零,它不起作用。

def standardize(data): #dataframe
    _,c = data.shape
    data_standardized = data.copy(deep=True)
    for j in range(c):
        x = data_standardized.iloc[:, j]
        avg = x.mean()
        std = x.std()
        x_standardized = (x - avg)/ std
        data_standardized.iloc[:, j] = x_standardized

    return data_standardized

【问题讨论】:

  • 你是什么意思它因为 zeors 而不起作用。你遇到了什么错误?
  • @Poojan 如果所有样本的第一个特征为零,那么我得到一个 sdt = 0 所以我 div。零
  • 看看这个答案会有所帮助。 stackoverflow.com/questions/26248654/…。当除以零时,您可以将其设为 0。

标签: python python-3.x neural-network standardized


【解决方案1】:

使用布尔索引避免被零除:

In [90]: data= np.array([[0,0,0,0,233,2,0,0,0],[0,0,0,23,50,2,0,0,0],[0,0,0,0,3,20,3,0,0]])

In [91]: new = np.zeros(data.shape)

In [92]: m = data.mean(0)

In [93]: std = data.std(0)

In [94]: r = data-m

In [95]: new[:,std.nonzero()] = r[:,std.nonzero()]/std[std.nonzero()]

In [96]: new
Out[96]: 
array([[ 0.        ,  0.        ,  0.        , -0.70710678,  1.3875163 ,
        -0.70710678, -0.70710678,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  1.41421356, -0.45690609,
        -0.70710678, -0.70710678,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        , -0.70710678, -0.9306102 ,
         1.41421356,  1.41421356,  0.        ,  0.        ]])

或使用sklearn.preprocessing.StandardScaler


你的函数重构了:

def standardize(data): #dataframe
    data = data.values
    new = np.zeros(data.shape)
    m = data.mean(0)
    std = data.std(0)
    new[:,std.nonzero()] = r[:,std.nonzero()]/std[std.nonzero()]
    return pd.DataFrame(new)

【讨论】:

    猜你喜欢
    • 2018-01-04
    • 1970-01-01
    • 2017-10-28
    • 2019-03-19
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多