【问题标题】:Normalizing data to certain range of values将数据标准化为特定范围的值
【发布时间】:2018-06-15 00:11:11
【问题描述】:

我是 Python 新手,有什么函数可以规范化数据吗?

例如,我在0 - 1 范围内有一组列表,例如:[0.92323, 0.7232322, 0,93832, 0.4344433]

我想将所有值标准化为0.25 - 0.50 范围内

谢谢你,

【问题讨论】:

    标签: python normalize


    【解决方案1】:

    你可以按照以下方式做某事:

    >>> l = [0.92323, 0.7232322, 0.93832, 0.4344433]
    >>> lower, upper = 0.25, 0.5
    >>> l_norm = [lower + (upper - lower) * x for x in l]
    >>> l_norm
    [0.4808075, 0.43080805, 0.48458, 0.35861082499999997]
    

    【讨论】:

    • 很酷,我可以在数据框中做吗?我尝试过,但在应用时使用它时出错。
    • 范围 0,1 怎么样?
    【解决方案2】:

    您可以将sklearn.preprocessing 用于包括规范化在内的多种预处理任务。

    【讨论】:

      【解决方案3】:

      以下函数考虑通用情况:

      def normalize(values, bounds):
          return [bounds['desired']['lower'] + (x - bounds['actual']['lower']) * (bounds['desired']['upper'] - bounds['desired']['lower']) / (bounds['actual']['upper'] - bounds['actual']['lower']) for x in values]
      

      用途:

      normalize(
          [0.92323, 0.7232322, 0.93832, 0.4344433],
          {'actual': {'lower': 0, 'upper': 1}, 'desired': {'lower': 0.25, 'upper': 0.5}}
      ) # [0.4808075, 0.43080805, 0.48458, 0.35861082499999997]
      
      normalize(
          [5, 7.5, 10, 12.5, 15],
          {'actual':{'lower':5,'upper':15},'desired':{'lower':1,'upper':2}}
      ) # [1.0, 1.25, 1.5, 1.75, 2.0]
      

      我选择了一个两级字典作为参数,但你可以通过多种方式给出它,例如在两个单独的元组中,一个用于实际边界,另一个用于期望,第一个元素是下限,而第二个上部:

      def normalize(values, actual_bounds, desired_bounds):
          return [desired_bounds[0] + (x - actual_bounds[0]) * (desired_bounds[1] - desired_bounds[0]) / (actual_bounds[1] - actual_bounds[0]) for x in values]
      

      用途:

         normalize(
          [0.92323, 0.7232322, 0.93832, 0.4344433],
          (0,1),
          (0.25,0.5)
      ) # [0.4808075, 0.43080805, 0.48458, 0.35861082499999997]
      
      normalize(
          [5, 7.5, 10, 12.5, 15],
          (5,15),
          (1,2)
      ) # [1.0, 1.25, 1.5, 1.75, 2.0]
      

      【讨论】:

        猜你喜欢
        • 2016-08-19
        • 1970-01-01
        • 2016-01-07
        • 2016-10-31
        • 2013-05-07
        • 1970-01-01
        • 2019-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多