【问题标题】:Python Pandas Dataframe: Normalize data between 0.01 and 0.99?Python Pandas Dataframe:规范化 0.01 到 0.99 之间的数据?
【发布时间】:2016-07-06 06:26:23
【问题描述】:

我正在尝试将数据框中的每个值绑定在 0.01 到 0.99 之间

我已经成功地将 0 和 1 之间的数据归一化,使用:.apply(lambda x: (x - x.min()) / (x.max() - x.min())) 如下:

df = pd.DataFrame({'one' : ['AAL', 'AAL', 'AAPL', 'AAPL'], 'two' : [1, 1, 5, 5], 'three' : [4,4,2,2]})

df[['two', 'three']].apply(lambda x: (x - x.min()) / (x.max() - x.min()))

df

现在我想绑定 0.01 到 0.99 之间的所有值

这是我尝试过的:

def bound_x(x):
    if x == 1:
        return x - 0.01
    elif x < 0.99:
        return x + 0.01

df[['two', 'three']].apply(bound_x)

​ df

但我收到以下错误:

ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred at index two')

【问题讨论】:

    标签: python pandas dataframe normalization


    【解决方案1】:

    有一个应用程序,错误clip method,为此:

    import pandas as pd
    df = pd.DataFrame({'one' : ['AAL', 'AAL', 'AAPL', 'AAPL'], 'two' : [1, 1, 5, 5], 'three' : [4,4,2,2]})    
    df = df[['two', 'three']].apply(lambda x: (x - x.min()) / (x.max() - x.min()))
    df = df.clip(lower=0.01, upper=0.99)
    

    产量

        two  three
    0  0.01   0.99
    1  0.01   0.99
    2  0.99   0.01
    3  0.99   0.01
    

    问题

    df[['two', 'three']].apply(bound_x)
    

    bound_x 是否通过了类似df['two'] 的系列,然后if x == 1 要求x == 1 在布尔上下文中评估x == 1 是一个布尔系列,如

    In [44]: df['two'] == 1
    Out[44]: 
    0    False
    1    False
    2     True
    3     True
    Name: two, dtype: bool
    

    Python 尝试将此 Series 简化为单个布尔值 TrueFalse。 Pandas 遵循 raising an error when you try to convert a Series (or array) to a bool 的 NumPy 约定。

    【讨论】:

      【解决方案2】:

      所以我有一个类似的问题,我想要自定义标准化,因为我常规的基准百分位数或 z 分数是不够的。有时我知道总体的可行最大值和最小值是多少,因此想要定义它而不是我的样本,或者不同的中点,或其他任何东西!所以我构建了一个自定义函数(在此处的代码中使用了额外的步骤以使其尽可能可读):

      def NormData(s,low='min',center='mid',hi='max',insideout=False,shrinkfactor=0.):    
          if low=='min':
              low=min(s)
          elif low=='abs':
              low=max(abs(min(s)),abs(max(s)))*-1.#sign(min(s))
          if hi=='max':
              hi=max(s)
          elif hi=='abs':
              hi=max(abs(min(s)),abs(max(s)))*1.#sign(max(s))
      
          if center=='mid':
              center=(max(s)+min(s))/2
          elif center=='avg':
              center=mean(s)
          elif center=='median':
              center=median(s)
      
          s2=[x-center for x in s]
          hi=hi-center
          low=low-center
          center=0.
      
          r=[]
      
          for x in s2:
              if x<low:
                  r.append(0.)
              elif x>hi:
                  r.append(1.)
              else:
                  if x>=center:
                      r.append((x-center)/(hi-center)*0.5+0.5)
                  else:
                      r.append((x-low)/(center-low)*0.5+0.)
      
          if insideout==True:
              ir=[(1.-abs(z-0.5)*2.) for z in r]
              r=ir
      
          rr =[x-(x-0.5)*shrinkfactor for x in r]    
          return rr
      

      这将采用 pandas 系列,甚至只是一个列表,并将其标准化为您指定的低点、中心点和高点。还有缩水的因素!允许您将数据从 0 和 1 缩小(在 matplotlib 中组合颜色图时我必须这样做:Single pcolormesh with more than one colormap using Matplotlib)所以您可能会看到代码是如何工作的,但基本上说您有值 [-5,1 ,10] 在样本中,但希望基于 -7 到 7 的范围进行归一化(因此任何高于 7 的值,我们的“10”都被有效地视为 7),中点为 2,但将其缩小以适合 256 RGB 颜色图:

      #In[1]
      NormData([-5,2,10],low=-7,center=1,hi=7,shrinkfactor=2./256)
      #Out[1]
      [0.1279296875, 0.5826822916666667, 0.99609375]
      

      它还可以将您的数据翻过来……这可能看起来很奇怪,但我发现它对热图很有用。假设您想要更接近 0 而不是高/低的值的颜色更深。您可以根据 insideout=True 的规范化数据进行热图:

      #In[2]
      NormData([-5,2,10],low=-7,center=1,hi=7,insideout=True,shrinkfactor=2./256)
      #Out[2]
      [0.251953125, 0.8307291666666666, 0.00390625]
      

      所以现在距离中心最近的“2”,定义为“1”是最大值。

      无论如何,我认为我的问题与您的问题非常相似,并且此功能可能对您有用。

      【讨论】:

        猜你喜欢
        • 2012-08-21
        • 1970-01-01
        • 2013-09-06
        • 1970-01-01
        • 2019-03-15
        • 2012-09-13
        • 2020-05-31
        • 2019-06-13
        • 2020-06-21
        相关资源
        最近更新 更多