Python 分析趋势并发现异常行为

Python 分析趋势并发现异常行为,python,mysql,database,analytics,Python,Mysql,Database,Analytics,我正在创建一个记录传感器数据的系统。(只是一系列数字) 我希望能够让系统进入“学习”模式几天,这样它就可以看到它的“正常”操作值是什么,一旦它脱离这个状态,任何偏离这个行为超过某一点的情况都可以被标记。数据都存储在MySQL数据库中 欢迎就如何开展这项工作提出任何建议,也欢迎就这一主题提供进一步阅读的地点 我更愿意使用python来完成这项任务 在白天访问和使用的温度控制区域内,数据温度和湿度值每5分钟显示一次。这意味着它在使用时会有波动,并且会发生一些温度变化。但任何与此不同的情况,如冷却或加

我正在创建一个记录传感器数据的系统。(只是一系列数字)

我希望能够让系统进入“学习”模式几天,这样它就可以看到它的“正常”操作值是什么,一旦它脱离这个状态,任何偏离这个行为超过某一点的情况都可以被标记。数据都存储在MySQL数据库中

欢迎就如何开展这项工作提出任何建议,也欢迎就这一主题提供进一步阅读的地点

我更愿意使用python来完成这项任务


在白天访问和使用的温度控制区域内,数据温度和湿度值每5分钟显示一次。这意味着它在使用时会有波动,并且会发生一些温度变化。但任何与此不同的情况,如冷却或加热系统故障,都需要检测到

看起来您正在尝试执行,但您对数据的描述含糊不清。您应该首先尝试定义/约束数据“正常”的一般含义

  • 每个传感器是否有不同的“正常”值
  • 传感器测量是否以某种方式依赖于其先前的测量
  • “正常”会在一天中发生变化吗
  • 传感器的“正常”测量值是否可以用统计模型表征(例如,数据是高斯还是对数正常)

  • 一旦回答了这些类型的问题,就可以使用数据库中的一批数据来训练分类器或异常检测器,并使用结果来评估未来的日志输出。如果机器学习算法适用于你的数据,你可以考虑使用。对于统计模型,可以使用的
    stats
    子包。当然,对于python中的任何类型的数字数据操作,都是您的朋友。

    本质上,您应该关注的是:确定一些变量的行为模型的任务,以便您可以查找与它的偏差

    下面是一些非常简单的示例代码。我假设温度和湿度在其未转换的尺度上具有独立的正态分布:

    import numpy as np
    from matplotlib.mlab import normpdf
    from itertools import izip
    
    class TempAndHumidityModel(object):
        def __init__(self):
            self.tempMu=0
            self.tempSigma=1
            self.humidityMu=0
            self.humiditySigma=1
    
        def setParams(self, tempMeasurements, humidityMeasurements, quantile):
            self.tempMu=np.mean(tempMeasurements)
            self.tempSigma=np.std(tempMeasurements)
            self.humidityMu=np.mean(humidityMeasurements)
            self.humiditySigma=np.std(humidityMeasurements)
    
            if not 0 < quantile <= 1:
                raise ValueError("Quantile for threshold must be between 0 and 1")
    
            self._thresholdDensity(quantile, tempMeasurements, humidityMeasurements)
    
        def _thresholdDensity(self, quantile, tempMeasurements, humidityMeasurements):
            tempDensities = np.apply_along_axis(
                lambda x: normpdf(x, self.tempMu, self.tempSigma),0,tempMeasurements)
            humidityDensities = np.apply_along_axis(
                lambda x: normpdf(x, self.humidityMu, self.humiditySigma),0,humidityMeasurements)
    
            densities = sorted(tempDensities * humidityDensities, reverse=True)
            #Here comes the massive oversimplification: just choose the
            #density value at the quantile*length position, and use this as the threshold
            self.threshold = densities[int(np.round(quantile*len(densities)))]
    
        def probOfObservation(self, temp, humidity):
            return normpdf(temp, self.tempMu, self.tempSigma) * \
                   normpdf(humidity, self.humidityMu, self.humiditySigma)
    
        def isNormalMeasurement(self, temp, humidity):
            return self.probOfObservation(temp, humidity) > self.threshold
    
    if __name__ == '__main__':
        #Create some simulated data
        temps = np.random.randn(100)*10 + 50
        humidities = np.random.randn(100)*2 + 10
    
        thm = TempAndHumidityModel()
        #going to hard code in the 95% threshold
        thm.setParams(temps, humidities, 0.95) 
    
        #Create some new data from same dist and see how many false positives
        newTemps = np.random.randn(100)*10 + 50
        newHumidities = np.random.randn(100)*2 + 10
    
        numFalseAlarms = sum(~thm.isNormalMeasurement(t,h) for t,h in izip(newTemps,newHumidities))
        print '{} false alarms!'.format(numFalseAlarms)
    
        #Now create some abnormal data: mean temp drops to 20
        lowTemps = np.random.randn(100)*10 + 20
        normalHumidities = np.random.randn(100)*2 + 10
    
        numDetections = sum(~thm.isNormalMeasurement(t,h) for t,h in izip(lowTemps,normalHumidities))
        print '{} abnormal measurements flagged'.format(numDetections)
    
    现在,我不知道正态性假设是否适用于您的数据(您可能希望将数据转换为不同的比例,使其符合要求);假设温度和湿度之间是独立的,这可能是非常不准确的;我用来找到密度值的技巧,对应于要求的分布分位数,应该用分布的逆CDF来代替。然而,这应该会让你知道该怎么做


    另外请注意,有许多很好的非参数密度估计器:立即浮现在脑海中。如果您的数据看起来不像任何标准分布,这些可能更合适。

    您能提供一些关于您将记录什么的详细信息吗?连续值?一个值还是一组值?对行为进行测量的频率有多高,以及您将测试哪种类型的装置的异常情况?在白天访问和使用的温度控制区域内,每5分钟测量一次数据温度和湿度值。这意味着它在使用时会有波动,并且会发生一些温度变化。但任何与此不同的事情,比如冷却或加热系统故障,都需要检测。我必须拿出我的统计课本。但是谢谢你。
    >> 3 false alarms!
    >> 77 abnormal measurements flagged