Python中Tsmoothie的基本使用是怎样
Admin 2022-08-10 群英技术资讯 899 次浏览
在这篇文章中,我们来学习一下“Python中Tsmoothie的基本使用是怎样”的相关知识,下文有详细的讲解,易于大家学习和理解,有需要的朋友可以借鉴参考,下面就请大家跟着小编的思路一起来学习一下吧。
有些时候,这样的散点数据是不利于我们进行数据的聚类和预测的。因此我们需要把它们平滑化,如下图所示:

如果我们将散点及其范围区间都去除,平滑后的效果如下:

这样的时序数据是不是看起来舒服多了?此外,使用平滑后的时序数据去做聚类或预测或许有令人惊艳的效果,因为它去除了一些偏差值并细化了数据的分布范围。
如果我们自己开发一个这样的平滑工具,会耗费不少的时间。因为平滑的技术有很多种,你需要一个个地去研究,找到最合适的技术并编写代码,这是一个非常耗时的过程。平滑技术包括但不限于:
所幸,有大佬已经为我们实现好了时间序列的这些平滑技术,并在GitHub上开源了这份模块的代码——它就是 Tsmoothie 模块。
开始之前,你要确保Python和pip已经成功安装在电脑上。
(可选1) 如果你用Python的目的是数据分析,可以直接安装Anaconda,它内置了Python和pip.
(可选2) 此外,推荐大家用VSCode编辑器,它有许多的优点。
请选择以下任一种方式输入命令安装依赖:
pip install tsmoothie
(PS) Tsmoothie 仅支持Python 3.6 及以上的版本。
为了尝试Tsmoothie的效果,我们需要生成随机数据:
import numpy as np
import matplotlib.pyplot as plt
from tsmoothie.utils_func import sim_randomwalk
from tsmoothie.smoother import LowessSmoother
# 生成 3 个长度为200的随机数据组
np.random.seed(123)
data = sim_randomwalk(n_series=3, timesteps=200,
process_noise=10, measure_noise=30)
然后使用Tsmoothie执行平滑化:
# 平滑 smoother = LowessSmoother(smooth_fraction=0.1, iterations=1) smoother.smooth(data)
通过 smoother.smooth_data 你就可以获取平滑后的数据:
print(smoother.smooth_data) # [[ 5.21462928 3.07898076 0.93933646 -1.19847767 -3.32294934 # -5.40678762 -7.42425709 -9.36150892 -11.23591897 -13.05271523 # ....... ....... ....... ....... ....... ]]
绘制效果图:

事实上,基于smoother生成的范围区域,我们可以进行异常值的检测:

可以看到,在蓝色范围以外的点,都属于异常值。我们可以轻易地将这些异常值标红或记录,以便后续的处理。
_low, _up = smoother.get_intervals('sigma_interval', n_sigma=2)
series['low'] = np.hstack([series['low'], _low[:,[-1]]])
series['up'] = np.hstack([series['up'], _up[:,[-1]]])
is_anomaly = np.logical_or(
series['original'][:,-1] > series['up'][:,-1],
series['original'][:,-1] < series['low'][:,-1]
).reshape(-1,1)
假设蓝色范围interval的最大值为up、最小值为low,如果存在 data > up 或 data < low 则表明此数据是异常点。
使用以下代码通过滚动数据点进行平滑化和异常检测,就能保存得到上方的GIF动图。
上滑查看更多代码
# Origin: https://github.com/cerlymarco/MEDIUM_NoteBook/blob/master/Anomaly_Detection_RealTime/Anomaly_Detection_RealTime.ipynb
import numpy as np
import matplotlib.pyplot as plt
from celluloid import Camera
from collections import defaultdict
from functools import partial
from tqdm import tqdm
from tsmoothie.utils_func import sim_randomwalk, sim_seasonal_data
from tsmoothie.smoother import *
def plot_history(ax, i, is_anomaly, window_len, color='blue', **pltargs):
posrange = np.arange(0,i)
ax.fill_between(posrange[window_len:],
pltargs['low'][1:], pltargs['up'][1:],
color=color, alpha=0.2)
if is_anomaly:
ax.scatter(i-1, pltargs['original'][-1], c='red')
else:
ax.scatter(i-1, pltargs['original'][-1], c='black')
ax.scatter(i-1, pltargs['smooth'][-1], c=color)
ax.plot(posrange, pltargs['original'][1:], '.k')
ax.plot(posrange[window_len:],
pltargs['smooth'][1:], color=color, linewidth=3)
if 'ano_id' in pltargs.keys():
if pltargs['ano_id'].sum()>0:
not_zeros = pltargs['ano_id'][pltargs['ano_id']!=0] -1
ax.scatter(not_zeros, pltargs['original'][1:][not_zeros],
c='red', alpha=1.)
np.random.seed(42)
n_series, timesteps = 3, 200
data = sim_randomwalk(n_series=n_series, timesteps=timesteps,
process_noise=10, measure_noise=30)
window_len = 20
fig = plt.figure(figsize=(18,10))
camera = Camera(fig)
axes = [plt.subplot(n_series,1,ax+1) for ax in range(n_series)]
series = defaultdict(partial(np.ndarray, shape=(n_series,1), dtype='float32'))
for i in tqdm(range(timesteps+1), total=(timesteps+1)):
if i>window_len:
smoother = ConvolutionSmoother(window_len=window_len, window_type='ones')
smoother.smooth(series['original'][:,-window_len:])
series['smooth'] = np.hstack([series['smooth'], smoother.smooth_data[:,[-1]]])
_low, _up = smoother.get_intervals('sigma_interval', n_sigma=2)
series['low'] = np.hstack([series['low'], _low[:,[-1]]])
series['up'] = np.hstack([series['up'], _up[:,[-1]]])
is_anomaly = np.logical_or(
series['original'][:,-1] > series['up'][:,-1],
series['original'][:,-1] < series['low'][:,-1]
).reshape(-1,1)
if is_anomaly.any():
series['ano_id'] = np.hstack([series['ano_id'], is_anomaly*i]).astype(int)
for s in range(n_series):
pltargs = {k:v[s,:] for k,v in series.items()}
plot_history(axes[s], i, is_anomaly[s], window_len,
**pltargs)
camera.snap()
if i>=timesteps:
continue
series['original'] = np.hstack([series['original'], data[:,[i]]])
print('CREATING GIF...') # it may take a few seconds
camera._photos = [camera._photos[-1]] + camera._photos
animation = camera.animate()
animation.save('animation1.gif', codec="gif", writer='imagemagick')
plt.close(fig)
print('DONE')
注意,异常点并非都是负面作用,在不同的应用场景下,它们可能代表了不同的意义。

比如在股票中,它或许可以代表着震荡行情中某种趋势反转的信号。
或者在家庭用电量分析中,它可能代表着某个时刻的用电峰值,根据这个峰值我们可以此时此刻开启了什么样的电器。
所以异常点的作用需要根据不同应用场景进行不同的分析,才能找到它真正的价值。

总而言之,Tsmoothie 不仅可以使用多种平滑技术平滑化我们的时序数据,让我们的模型训练更加有效,还可以根据平滑结果找出数据中的离群点,是我们做数据分析和研究的一个好帮手,非常有价值。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
opencv矩形轮廓矫正怎样实现,及相关问题怎样解决,一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
这篇文章主要介绍基于python实现自动下载文件sftp的内容,实现功能和需求、以及具体的实现代码如下,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章能有所收获,接下来小编带着大家一起了解看看。
这篇文章主要介绍了Python基础学习函数+模块+类,这是基础学习的第三篇内容,小编已把前两篇链接放在下面,需要学习的同学可以参考一下
自己写 Python 也有四五年了,一直是用自己的“强迫症”在维持自己代码的质量。都有去看 Google 的 Python 代码规范,对这几年的工作经验,做个简单的笔记,如果你也在学 Python,准备要学习 Python,希望这篇文章对你有用。
这篇文章主要介绍了Python登录系统界面实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008