【问题标题】:Segmenting a dataset分割数据集
【发布时间】:2019-04-18 12:54:33
【问题描述】:

给定一个包含日期和值的 CSV 数据集,我想尝试创建一个新的 CSV 数据集,其中的输出由图形发生变化的点组成:增加、减少或根本没有变化。下面是一个示例从数据和所需的输出。 (CSV 下降到 1999 年)

Date        Value
07/04/2014  137209.0
04/04/2014  137639.0
03/04/2014  137876.0
02/04/2014  137795.0
01/04/2014  137623.0
31/03/2014  137589.0
28/03/2014  137826.0
27/03/2014  138114.0
26/03/2014  138129.0
25/03/2014  137945.0

输出应该是:

StartDate   EndDate   StartValue   EndValue
03/04/2014  07/04/2014  137876      137209
31/03/2014  03/04/2014  137589      137876
27/03/2014  31/03/2014  138114      137589
26/03/2014  27/03/2014  138129      138114
25/03/2014  26/03/2014  137945      138129

【问题讨论】:

  • 您为编写解决方案做了什么?你分组的依据是什么?纯粹的增长、纯粹的下降或纯粹的停滞?您的 csv 数据是否已排序?为什么你有重复的数据点(例如 3/4/ 作为纯减少/纯增加拉伸的起点和终点出现两次)?#
  • 我进行了清理,删除了 NULL 行,并使用所需的 4 列创建了空的新 CSV。是的,我正在对纯粹的增加和减少进行分组。 CSV 输入按日期列排序,如上面的示例所示,并且一直到 1999 年。
  • 有趣的谜题 - pandas 或 numpy 中可能有一些东西可以在 4 行中处理它,不幸的是你没有使用这些标签,所以 ppl 好的人不会看到这个。如果我是你,我会删除 data-* 标签,它们对你没有好处,最好添加 numpy/pandas。
  • 是的,谢谢你告诉我,我的主要用途是 Pandas 和 Numpy。

标签: python pandas numpy data-mining


【解决方案1】:

我尝试解决这个问题,涉及一个自写的Stretch 类,该类在添加数据时管理数据的拆分:

from enum import Enum

class Direction(Enum):
    NA = None 
    Up = 1 
    Stagnant = 0 
    Down = -1

    @staticmethod
    def getDir(a,b):
        """Gets two numbers and returns a Direction result by comparing them."""
        if a < b:   return Direction.Up
        elif a > b: return Direction.Down
        else:       return Direction.Stagnant

class Stretch:
    """Accepts tuples of (insignificant, float). Adds tuples to internal data struct
    while they have the same trend (down, up, stagnant). See add() for details."""

    def __init__(self,dp=None):
        self.data = []
        if dp:
            self.data.append(dp)
        self.dir = Direction.NA  


    def add(self,dp):
        """Adds dp to self if it follows a given trend (or it holds less then 2 datapts).
        Returns (True,None) if the datapoint was added to this Stretch instance,
        returns (False, new_stretch) if it broke the trend. The new_stretch
        contains the new last value of the self.data as well as the new dp."""
        if not self.data:
            self.data.append(dp)
            return True, None
        if len(self.data) == 1:
            self.dir = Direction.getDir(self.data[-1][1],dp[1]) 
            self.data.append(dp)
            return True, None
        if Direction.getDir(self.data[-1][1],dp[1]) == self.dir:
            self.data.append(dp)
            return True, None
        else:
            k = Stretch(self.data[-1])
            k.add(dp)
            return False, k

演示文件:

with open("d.txt","w") as w:
    w.write( """Date        Value
07/04/2014  137209.0
04/04/2014  137639.0
03/04/2014  137876.0
02/04/2014  137795.0
01/04/2014  137623.0
31/03/2014  137589.0
28/03/2014  137826.0
27/03/2014  138114.0
26/03/2014  138129.0
25/03/2014  137945.0
""" )

用法:

data_stretches = []

with open("d.txt") as r:
    S = Stretch()
    for line in r:
        try:
            date,value = line.strip().split()
            value = float(value)
        except (IndexError, ValueError) as e:
            print("Illegal line: '{}'".format(line))
            continue

        b, newstretch = S.add( (date,value) )
        if not b:
            data_stretches.append(S)
            S = newstretch
data_stretches.append(S)

for s in data_stretches:
    data = s.data
    direc = s.dir


    print(data[0][0], data[-1][0], data[0][1],data[-1][-1], s.dir)

输出:

# EndDate  StartDate  EndV     StartV   (reversed b/c I inverted dates)  
07/04/2014 03/04/2014 137209.0 137876.0 Direction.Up
03/04/2014 31/03/2014 137876.0 137589.0 Direction.Down
31/03/2014 26/03/2014 137589.0 138129.0 Direction.Up
26/03/2014 25/03/2014 138129.0 137945.0 Direction.Down 

除了基于“从何时到何时”评估数据的混乱方向之外,我的输出与你的不同......因为你将一个统一的序列分成两部分而没有明显的原因:

27/03/2014  31/03/2014  138114      137589   # further down
26/03/2014  27/03/2014  138129      138114   # down

【讨论】:

  • 非常感谢!这是解决我的问题的非常有趣的方法,我学到了很多新东西。
【解决方案2】:

您可以使用numpy 中的sign 并将其应用于“值”列上的diff,以查看图表趋势的变化位置,然后使用@987654324 为每组趋势创建增量值@和cumsum

ser_sign = np.sign(df.Value.diff(-1).ffill())
ser_gr = ser_gr =(ser_sign.shift() != ser_sign).cumsum()

现在您知道了这些组,要获取每个组的开始和结束,您可以在ser_grjoinlast 上使用groupby(在shift 之后ser_gr 中的值因为每组的最后一个是下一个的第一个)和first

df_new = (df.groupby(ser_gr.shift().bfill(),as_index=False).last()
            .join(df.groupby(ser_gr,as_index=False).first(),lsuffix='_start',rsuffix='_end'))

print (df_new)
   Date_start  Value_start    Date_end  Value_end
0  03/04/2014     137876.0  07/04/2014   137209.0
1  31/03/2014     137589.0  03/04/2014   137876.0
2  26/03/2014     138129.0  31/03/2014   137589.0
3  25/03/2014     137945.0  26/03/2014   138129.0

现在,如果您需要重新排序列并重命名它们,您可以这样做:

df_new.columns = ['StartDate', 'StartValue', 'EndDate', 'EndValue']
df_new = df_new[['StartDate','EndDate','StartValue','EndValue']]

print (df_new)
    StartDate     EndDate  StartValue  EndValue
0  03/04/2014  07/04/2014    137876.0  137209.0
1  31/03/2014  03/04/2014    137589.0  137876.0
2  26/03/2014  31/03/2014    138129.0  137589.0
3  25/03/2014  26/03/2014    137945.0  138129.0

这两个操作可以同时完成,而不是您使用rename 创建df_new

【讨论】:

  • 就像我想的那样......不是 4 班轮而是 6 班......而且很整洁 - 至少你得到的结果和我一样;)关于分组
  • @PatrickArtner 确实,几行就足够了 :) 谢谢。其实我很高兴你之前回答过,因为我得到了相同的分组,我对这个结果更有信心。
  • 非常感谢!我想 Numpy 的符号选项是我所需要的,这就是我的过程中缺少的步骤。感谢@PatrickArtner 的丰富方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 2020-08-19
  • 2020-01-29
  • 1970-01-01
  • 2020-07-03
  • 2021-05-20
  • 1970-01-01
相关资源
最近更新 更多