如何在numpy数组上进行数据标准化,不使用MinMaxScaler包。

huangapple go评论93阅读模式
英文:

How to normalization data on numpy array, without MinMaxScaler package

问题

如何在没有MinMaxScaler包的情况下进行数据归一化。在这里,我尝试根据MinMax缩放公式进行操作,但出现了以下错误 IndexError: invalid index to scalar variable.

代码:

scale = []

for i in range(0,6):
    minFP = FCData[:,i].min()
    maxFP = FCData[:,i].max()
    yscale = (FCData[:,i] - minFP) / (maxFP - minFP)
    scale.append(yscale)

scale = np.array(scale)

我的数据:
数据形状:(15000,6)

array([[     4.46733  ,      4.39629  ,    -34.2351   ,  -4077.23     ,
         -6206.81     ,   -874.539    ],
       [     7.65166  ,      2.61174  ,    -49.7356   ,  -4846.76     ,
         -9060.05     ,  -1291.39     ],
       [    11.285    ,     -2.91447  ,    -87.9661   ,  -5412.32     ,
        -16345.2      ,   -213.72     ],
       [    12.7313   ,     -6.48048  ,   -123.094    ,  -5939.48     ,
        -23005.6      ,    443.115    ],
       [    11.6425   ,      0.0259204,   -131.717    ,  -6972.53     ,
        -24651.9      ,  -1112.73     ],
       [    12.3602   ,     10.1988   ,   -139.597    ,  -8544.17     ,
        -26118.8      ,  -3260.79     ],
       [    16.0733   ,     12.1455   ,   -165.01     , -10371.5      ,
        -30873.5      ,  -3643.65     ],
       [    21.1933   ,      8.86926  ,   -210.599    , -12673.2      ,
        -39447.9      ,  -2785.69     ],
       [    24.3619   ,      7.59683  ,   -267.449    , -16170.6      ,
        -50300.9      ,  -2823.35     ]])

如何在numpy数组上进行数据标准化,不使用MinMaxScaler包。

英文:

how to normalize data without minmaxscaler package. here I have tried it according to the minmax scale formula. but i get an error like this IndexError: invalid index to scalar variable.

the code:

scale = []

for i in range(0,6):
    minFP = FCData[:,i].min()
    maxFP = FCData[:,i].max()
    yscale = (FCData[:,i] - minFP[i]) / ( maxFP[i] - minFP[i])
    scale.append(yscale[i])

scale = np.array(scale)

my data:
Data shape : (15000,6)

array([[     4.46733  ,      4.39629  ,    -34.2351   ,  -4077.23     ,
         -6206.81     ,   -874.539    ],
       [     7.65166  ,      2.61174  ,    -49.7356   ,  -4846.76     ,
         -9060.05     ,  -1291.39     ],
       [    11.285    ,     -2.91447  ,    -87.9661   ,  -5412.32     ,
        -16345.2      ,   -213.72     ],
       [    12.7313   ,     -6.48048  ,   -123.094    ,  -5939.48     ,
        -23005.6      ,    443.115    ],
       [    11.6425   ,      0.0259204,   -131.717    ,  -6972.53     ,
        -24651.9      ,  -1112.73     ],
       [    12.3602   ,     10.1988   ,   -139.597    ,  -8544.17     ,
        -26118.8      ,  -3260.79     ],
       [    16.0733   ,     12.1455   ,   -165.01     , -10371.5      ,
        -30873.5      ,  -3643.65     ],
       [    21.1933   ,      8.86926  ,   -210.599    , -12673.2      ,
        -39447.9      ,  -2785.69     ],
       [    24.3619   ,      7.59683  ,   -267.449    , -16170.6      ,
        -50300.9      ,  -2823.35     ]])

如何在numpy数组上进行数据标准化,不使用MinMaxScaler包。

答案1

得分: 1

你可以使用以下代码在一行内完成这个操作:

scaleddata = (FCData - FCData.min(axis=0)) / (FCData.max(axis=0) - FCData.min(axis=0))

或者(感谢评论中的 @cards 提供的方法),这也可以更加简洁地写成:

scaleddata = (FCData - FCData.min(axis=0)) / np.ptp(FCData, axis=0)
英文:

You should be able to do this on 1 line with:

scaleddata = (FCData - FCData.min(axis=0)) / (FCData.max(axis=0) - FCData.min(axis=0))

or (thanks to @cards in the comments), this could be even shorter with:

scaleddata = (FCData - FCData.min(axis=0)) / np.ptp(FCData, axis=0)

答案2

得分: 0

maxFP和minFP只是值,而不是列表/数组,所以不需要i索引,例如,将yscale = (FCData[:,i] - minFP[i]) / (maxFP[i] - minFP[i])更改为yscale = (FCData[:,i] - minFP) / (maxFP - minFP)。来源:https://stackoverflow.com/questions/75055006/how-to-normalization-data-on-numpy-array-without-minmaxscaler-package/75094153#comment132449311_75055006

英文:

maxFP and minFP are just values rather than lists/arrays, so do not need the i index, e.g., change yscale = (FCData[:,i] - minFP[i]) / ( maxFP[i] - minFP[i]) to yscale = (FCData[:,i] - minFP) / (maxFP - minFP) https://stackoverflow.com/questions/75055006/how-to-normalization-data-on-numpy-array-without-minmaxscaler-package/75094153#comment132449311_75055006

huangapple
  • 本文由 发表于 2023年1月9日 17:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定