Python用KShape对时间序列进行聚类和肘方法确定最优聚类数k可视化|附代码数据

全文链接:http://tecdat.cn/?p=27078

最近我们被客户要求撰写关于KShape的研究报告,包括一些图形和统计输出。

时序数据的聚类方法,该算法按照以下流程执行。

  1. 使用基于互相关测量的距离标度(基于形状的距离:SBD)
  2. 根据 1 计算时间序列聚类的质心。(一种新的基于质心的聚类算法,可保留时间序列的形状)
  3. 划分成每个簇的方法和一般的kmeans一样,但是在计算距离尺度和重心的时候使用上面的1和2。
import pandas as pd

图片

    # 读取数据帧,将其转化为时间序列数组,并将其存储在一个列表中    tata = []    for i, df in enmee(dfs):

        

        # 检查每个时间序列数据的最大长度。        for ts in tsda:

            if len(s) > ln_a:

                lenmx = len(ts)

        

        # 给出最后一个数据,以调整时间序列数据的长度        for i, ts in enumerate(tsdata):

            dta[i] = ts + [ts[-1]] * n_dd

    





    # 转换为矢量    stack_list = []    for j in range(len(timeseries_dataset)):

       

        stack_list.append(data)

    

    # 转换为一维数组    trasfome_daa = np.stack(ack_ist, axis=0)

    return trafoed_data

数据集准备

# 文件列表flnes= soted(go.ob('mpldat/smeda*.csv'))
# 从文件中加载数据帧并将其存储在一个列表中。for ienme in fiemes:

    df = pd.read_csv(filnme, indx_cl=one,hadr=0)    flt.append(df)

聚类结果的可视化

# 为了计算交叉关系,需要对它们进行归一化处理。# TimeSeriesScalerMeanVariance将是对数据进行规范化的类。sac_da = TimeeiesalerMVarne(mu=0.0, std=1.0).fit_trnform(tranfome_data)# KShape类的实例化。ks = KShpe(_clusrs=2, n_nit=10, vrboe=True, rano_stte=sed)

yprd = ks.ft_reitsak_ata)# 聚类和可视化plt.tight_layout()

plt.show()

Python用KShape对时间序列进行聚类和肘方法确定最优聚类数k可视化|附代码数据_第1张图片


点击标题查阅往期内容

Python用KShape对时间序列进行聚类和肘方法确定最优聚类数k可视化|附代码数据_第2张图片

R语言k-Shape时间序列聚类方法对股票价格时间序列聚类

图片

左右滑动查看更多

图片

01

Python用KShape对时间序列进行聚类和肘方法确定最优聚类数k可视化|附代码数据_第3张图片

02

Python用KShape对时间序列进行聚类和肘方法确定最优聚类数k可视化|附代码数据_第4张图片

03

Python用KShape对时间序列进行聚类和肘方法确定最优聚类数k可视化|附代码数据_第5张图片

04

Python用KShape对时间序列进行聚类和肘方法确定最优聚类数k可视化|附代码数据_第6张图片

用肘法计算簇数

  • 什么是肘法...
  • 计算从每个点到簇中心的距离的平方和,指定为簇内误差平方和 (SSE)。
  • 它是一种更改簇数,绘制每个 SSE 值,并将像“肘”一样弯曲的点设置为最佳簇数的方法。

    #计算到1~10个群组 for i  in range(1,11):

    #进行聚类计算。

    ks.fit(sacdta)

    #KS.fit给出KS.inrta_    disorons.append(ks.netia_)

plt.plot(range(1,11), disorins, marker='o')


![图片](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6a1ccbea12254f3d97fa81a99e98bfab~tplv-k3u1fbpfcp-zoom-1.image)

![图片](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/298a79a5376b426eac52746d0e36085a~tplv-k3u1fbpfcp-zoom-1.image)

* * *

 

* * *

  


![图片](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/aa8d6ccca81a4219a91ca89c6a2bebbb~tplv-k3u1fbpfcp-zoom-1.image)

点击文末 **“阅读原文”**

获取全文完整资料。

  


本文选自《**Python用KShape对时间序列进行聚类和肘方法确定最优聚类数k可视化**》。

  


**点击标题查阅往期内容**

[R语言中的SOM(自组织映射神经网络)对NBA球员聚类分析](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247516548&idx=2&sn=5b75c1e34d6b40864244e1aa5a1dbe94&chksm=fd92bf8fcae5369996bef14478ddee6c2a8592019daec30320c018c1e3a3e00fdd95ce1d0f0d&scene=21#wechat_redirect)  
[K-means和层次聚类分析癌细胞系微阵列数据和树状图可视化比较](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247510121&idx=1&sn=9c2b39769533229d1fd5bb2cdf185be7&chksm=fd929662cae51f74e0d487f8141a76612f721f329431ff3ec806ed6be9634da98f11f02031c7&scene=21#wechat_redirect)[KMEANS均值聚类和层次聚类:亚洲国家地区生活幸福质量异同可视化分析和选择最佳聚类数](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247508401&idx=2&sn=03b8a812234cc82a7f4cccd365fb1f97&chksm=fd929fbacae516ac2b6ea9a1bc73e30d40fff4f9cdab45a14213c08062f45c2085c5a19f5c43&scene=21#wechat_redirect)  
[PYTHON实现谱聚类算法和改变聚类簇数结果可视化比较](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247508051&idx=1&sn=640bd7a0dbd844d50a1a6f4d89e68c45&chksm=fd929e58cae5174e523d422a2253efebdd254f507d3b80d92519a4de21cc3a1ea784ddacb617&scene=21#wechat_redirect)  
[有限混合模型聚类FMM、广义线性回归模型GLM混合应用分析威士忌市场和研究专利申请数据](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247507609&idx=1&sn=2526c1a15e5c2b64c4eeb218767f2a71&chksm=fd92e092cae5698418bf5bf79c98837000b8c711e81ceabb30afd7fd074c12e7729f61f390c9&scene=21#wechat_redirect)  
[R语言多维数据层次聚类散点图矩阵、配对图、平行坐标图、树状图可视化城市宏观经济指标数据](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247506893&idx=1&sn=3722d123322132225f076f8ccd97e2d7&chksm=fd92e5c6cae56cd0775bc5b88e7a9406613e5f04ecba1fb073190228b0649606bff742b86976&scene=21#wechat_redirect)  
[r语言有限正态混合模型EM算法的分层聚类、分类和密度估计及可视化](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247502736&idx=1&sn=d1b8691595a347f58e489fc0ce6edaf0&chksm=fd92f59bcae57c8de701ec891d3c8ec3bed5bc2a798d9d5937ba977372c8f07c8090caa013f5&scene=21#wechat_redirect)  
[Python Monte Carlo K-Means聚类实战研究](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247501328&idx=2&sn=cb254a796edf83b34bf66ef43c651ec0&chksm=fd92f81bcae5710daaf84437bd99e7e7fa35a1634c6081bedf2db4e9b21a8ea72da44e8fc23a&scene=21#wechat_redirect)  
[R语言k-Shape时间序列聚类方法对股票价格时间序列聚类](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247500705&idx=2&sn=1e9d8fbef30b7e62aa5bd16c304f6088&chksm=fd92fdaacae574bc2ea18b9f9a3791b555e904a44d68dc3be9e3c675461cb41902baaddc5286&scene=21#wechat_redirect)  
[R语言对用电负荷时间序列数据进行K-medoids聚类建模和GAM回归](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247500286&idx=2&sn=a81aebc0b60318d226968e07b0020db1&chksm=fd92fff5cae576e356df36372b9d935f0b914f13ad1ba80c91b1185acd92f2efaf2b745bcebd&scene=21#wechat_redirect)  
[R语言谱聚类、K-MEANS聚类分析非线性环状数据比较](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247500104&idx=1&sn=1eb955cf5c730ed5cd07bdbe07472bb9&chksm=fd92ff43cae576552fd2a14d14f0e2af3d59f6f410221eb24b51ecc5ee0d0113f14c05c22767&scene=21#wechat_redirect)  
[R语言实现k-means聚类优化的分层抽样(Stratified Sampling)分析各市镇的人口](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247499299&idx=1&sn=694818311fdfafcfd3b1bd2553200085&chksm=fd92c028cae5493e1168a2a59bd8301f001c2efb9dd1620b8e258c4b8dde1a1e7534b80caea7&scene=21#wechat_redirect)  
[R语言聚类有效性:确定最优聚类数分析IRIS鸢尾花数据和可视化](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247498712&idx=1&sn=7683c54364c529923c843df31882a2ce&chksm=fd92c5d3cae54cc52dd7be623292ec059a5d924ee36c0029e8930474db6c65d0267ff0818ca4&scene=21#wechat_redirect)[Python、R对小说进行文本挖掘和层次聚类可视化分析案例](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247498465&idx=2&sn=91b7297690d8ddc5194e987a4e8a3357&chksm=fd92c4eacae54dfcc0bfab86f878b028f42302a83efb3a59e5de519a979b298f4fa8af8d68a4&scene=21#wechat_redirect)  
[R语言k-means聚类、层次聚类、主成分(PCA)降维及可视化分析鸢尾花iris数据集](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247498465&idx=1&sn=dd4c1f65a1c2d8cd0dfd1236c827492a&chksm=fd92c4eacae54dfc1cda2f924c5c5c60cf497eb2079cd5459a39cc9daefca3aec7fd76cb9c32&scene=21#wechat_redirect)  
[R语言有限混合模型(FMM,finite mixture model)EM算法聚类分析间歇泉喷发时间](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247497673&idx=1&sn=b31e2b3c1f703d201f09ca203caddce3&chksm=fd92c9c2cae540d4c13dbefbed1d034c62386ec2744ce372eae8099d8fdb936f857219b3fb74&scene=21#wechat_redirect)  
[R语言用温度对城市层次聚类、kmean聚类、主成分分析和Voronoi图可视化](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247495156&idx=1&sn=82e9cd3cbfe47006cbb694eb24a0d74e&chksm=fd92d3ffcae55ae94850149f0d8f56de96129429754ab6b15a3e84f8c06dae05ab803431ac2a&scene=21#wechat_redirect)  
[R语言k-Shape时间序列聚类方法对股票价格时间序列聚类](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247493463&idx=2&sn=18232f6b637b9272ac503924392fe8d3&chksm=fd92d95ccae5504a43f777aa15ef10d6f74e9701c4318c41b636cbf61a3649ee2b5b2bd1d233&scene=21#wechat_redirect)  
[R语言中的SOM(自组织映射神经网络)对NBA球员聚类分析](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247493017&idx=1&sn=7fd07acebc2e5d7216236f287a333914&chksm=fd92db92cae55284671aaaeba424b5d6a892a293e181e6831c8585a0432b394b0f5645649e67&scene=21#wechat_redirect)  
[R语言复杂网络分析:聚类(社区检测)和可视化](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247492713&idx=2&sn=3b1341efc989f5f395929c798ac9da60&chksm=fd92da62cae55374bec3b89f9e382a5e143171bebce4171f214a4af4d43dc6fe70c2d23b01d5&scene=21#wechat_redirect)  
[R语言中的划分聚类模型](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247492472&idx=3&sn=43056832fc7b4b5dd6e1cbd0f035f9de&chksm=fd92dd73cae55465b419f4572d0ba0497a20bae6047c891bc6399e12d91a0caf7d83c578e5b4&scene=21#wechat_redirect)  
[基于模型的聚类和R语言中的高斯混合模型](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247492432&idx=3&sn=2960efe44c2a0bbab0e2c804755adf70&chksm=fd92dd5bcae5544d6328947ce22bcb4fe9a574f440f7d4b016a4642e3ea0a74ef69aa1c88408&scene=21#wechat_redirect)  
[r语言聚类分析:k-means和层次聚类](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247491996&idx=3&sn=76fdce2eeb0f9eb0bcf8681e258c0a99&chksm=fd92df97cae55681e9c371f5fb7d25912d3bae75093e32f476d6bc73403f46af15c1859a5694&scene=21#wechat_redirect)  
[SAS用K-Means 聚类最优k值的选取和分析](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247491396&idx=1&sn=4ca13d1b6bda580d7a60605f8e1ed2de&chksm=fd91214fcae6a859b5e670be257cf3ea29892c63b66927e48514e190e703547ff1fb8f93bd1b&scene=21#wechat_redirect)  
[用R语言进行网站评论文本挖掘聚类](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247489693&idx=3&sn=ed0a65fc1019f2e62e90734e25b2e6cc&chksm=fd912696cae6af8050cdcb5c516ffd4ea98278ef438712c07a01c6f11f0a17f5a3744ff24d84&scene=21#wechat_redirect)  
[基于LDA主题模型聚类的商品评论文本挖掘](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247489519&idx=3&sn=bd8179e866ebd67286a6f5e8afda3de1&chksm=fd9129e4cae6a0f29dc622fb174dc0e321f4cbf93afb0f5dee5051cd0cbb0ee9677012e6507b&scene=21#wechat_redirect)  
[R语言鸢尾花iris数据集的层次聚类分析](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247488780&idx=1&sn=8426dcbc64a4485383d333e3e440c81c&chksm=fd912b07cae6a21107a3aa6e9c8aefe8b2e96aea6864b8c5f561618aeeee4c3abf67e3332be3&scene=21#wechat_redirect)  
[R语言对用电负荷时间序列数据进行K-medoids聚类建模和GAM回归](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247488780&idx=2&sn=c56669c116190eb04e2639194cb912f8&chksm=fd912b07cae6a211fdb7c8e8dabd6045330657c14735f07b8356d90434e622a7a7e5c1f779f9&scene=21#wechat_redirect)  
[R语言聚类算法的应用实例](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247487708&idx=3&sn=a7b207b86934c101a22a223c40b4741c&chksm=fd912ed7cae6a7c1d179c0b921c199beae98bc67f49939d3551adca08f0360f7b1feac861b01&scene=21#wechat_redirect)[对用电负荷时间序列数据进行K-medoids聚类建模和GAM回归](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247516271&idx=3&sn=781a28e7bc8e5db5fb0e368d7a76566b&chksm=fd92be64cae53772b909ea0dfaa4376c8852e6f171c11c60fbe0f5fde61c095349fc08dc1eaf&scene=21#wechat_redirect)  
[分位数回归、GAM样条曲线、指数平滑和SARIMA对电力负荷时间序列预测](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247509153&idx=3&sn=4aff9db04827809fecf70d91fdc482df&chksm=fd929aaacae513bccaf6b595ed861a87113abe5f140bf6982075d98fd31867c9a436698b5312&scene=21#wechat_redirect)  
[【视频】R语言广义相加模型(GAM)在电力负荷预测中的应用](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247500246&idx=1&sn=f41d4fcb12796ed52b7c4cdd2fd04099&chksm=fd92ffddcae576cbb10dc1f819079c3212c8edfd0e1870927cb9067739a023b034375e04538c&scene=21#wechat_redirect)  
[R语言里的非线性模型:多项式回归、局部样条、平滑样条、 广义相加模型GAM分析](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247496258&idx=2&sn=d651743315f28e19a94c818fc3fdad9b&chksm=fd92cc49cae5455fd193e776dca6aed628f5d8fbafef2a3420f3567eb1d7a5d9caf696719286&scene=21#wechat_redirect)  
[R语言用标准最小二乘OLS,广义相加模型GAM ,样条函数进行逻辑回归LOGISTIC分类](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247495252&idx=1&sn=28310cd9e77eeb87dbe8d8b4870be72d&chksm=fd92d05fcae5594948645b998c141b8a352d7cd4df043a0ced68e8032b7185af6523bebdd790&scene=21#wechat_redirect)  
[R语言ISLR工资数据进行多项式回归和样条回归分析](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247494779&idx=2&sn=470849162dfc3270c69e18999e6c025a&chksm=fd92d270cae55b66adf34e437f19698959dd211bb7d54bb9c8fd84df4d550879eb3a8c9d83b5&scene=21#wechat_redirect)  
[R语言中的多项式回归、局部回归、核平滑和平滑样条回归模型](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247494720&idx=1&sn=def27845fe8c39827fb580baa90bd0b0&chksm=fd92d24bcae55b5d477d0fa66632940e1c675f8efa585c65218f88d2ed58d69e8bd4c688971d&scene=21#wechat_redirect)  
[R语言用泊松Poisson回归、GAM样条曲线模型预测骑自行车者的数量](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247492508&idx=1&sn=ea5713493413c251cdd5c2104c80f4cb&chksm=fd92dd97cae554810882ab453f5d45dd61e6699a67c41202ea6fc50cab3c5fc1398f8efa736f&scene=21#wechat_redirect)  
[R语言中的多项式回归、B样条曲线(B-spline Curves)回归](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247491963&idx=1&sn=dcf6f3c7dadf5dd449060c9001bb4466&chksm=fd92df70cae55666baace37b76c44e3a439cc53cdf50aa9e1a5c73d06470cd29baf7a583f37f&scene=21#wechat_redirect)  
[R语言广义相加模型 (GAMs)分析预测CO2时间序列数据](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247495111&idx=1&sn=6a918ec70f6055e1e680160dbb4db655&chksm=fd92d3cccae55ada6264dcb7a9fd3996688d5616f3e04b0634a83ae5266f2e508f7e9d67bbe7&scene=21#wechat_redirect)  
[R语言中实现广义相加模型GAM和普通最小二乘(OLS)回归](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247495047&idx=1&sn=0afeb7e0614f936d554b2ecddb42e8dd&chksm=fd92d38ccae55a9a2ed44cc090a403b45782203ac1040ba4eaa33bf3165734517eb628415e4e&scene=21#wechat_redirect)  
[在r语言中使用GAM(广义相加模型)进行电力负荷时间序列分析](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247493193&idx=2&sn=e12e1946b1b650dde707444fb4b3b202&chksm=fd92d842cae55154975aab8696680d4b51eff20b9ce765ea13ea6211e378b0bcf0dd422a7f5d&scene=21#wechat_redirect)  
[R语言用泊松Poisson回归、GAM样条曲线模型预测骑自行车者的数量](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247492508&idx=1&sn=ea5713493413c251cdd5c2104c80f4cb&chksm=fd92dd97cae554810882ab453f5d45dd61e6699a67c41202ea6fc50cab3c5fc1398f8efa736f&scene=21#wechat_redirect)  
[Python用广义加性模型GAM进行时间序列分析](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247492268&idx=2&sn=375ecb35c5d83bd11a597302cb3f03a0&chksm=fd92dca7cae555b10e66e082ae7d10a3420c5c8f1498426ea443e7e3429f68ab9d07fb7ba50f&scene=21#wechat_redirect)  
[R语言广义线性模型GLM、多项式回归和广义可加模型GAM预测泰坦尼克号幸存者](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247492140&idx=1&sn=843b0c171b2b8a6574a6585fda0263dd&chksm=fd92dc27cae55531a04913c9f9332bdbf1b5adb0da209f3aa4c256d5456b6ea64b10a40b4b71&scene=21#wechat_redirect)  
[R语言中的广义线性模型(GLM)和广义相加模型(GAM):多元(平滑)回归分析保险资金投资组合信用风险敞口](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247489964&idx=3&sn=8ce0c4a13c1e03422b69c8d4398e8414&chksm=fd9127a7cae6aeb11709a2fe0cf8c16b875aa3c5037420ac0f56545a7ec7f9020cd296068f2e&scene=21#wechat_redirect)  
[R语言对用电负荷时间序列数据进行K-medoids聚类建模和GAM回归](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247488780&idx=2&sn=c56669c116190eb04e2639194cb912f8&chksm=fd912b07cae6a211fdb7c8e8dabd6045330657c14735f07b8356d90434e622a7a7e5c1f779f9&scene=21#wechat_redirect)  
[对用电负荷时间序列数据进行K-medoids聚类建模和GAM回归](http://mp.weixin.qq.com/s?__biz=MzU4NTA1MDk4MA==&mid=2247483934&idx=3&sn=aba0bcc39c07bf44580f7dd2fb0a0e62&chksm=fd913c15cae6b503f629ed6105f97c115a69682185901603d3342eadb531f6ba65881404e055&scene=21#wechat_redirect)

你可能感兴趣的:(数据挖掘深度学习机器学习算法)