熊猫df.rolling()。corr()计算图案/模板与系列之间的滚动相关性

时间:2018-09-05 11:31:52

标签: python pandas correlation rolling-computation

我想使用pandas.DataFrame.corr()计算一个较长的N维序列(signal.csv)和较短的N维模式/模板序列(template.csv之间的运行/滚动相关性)。最终目的是查看图案/模板在系列中的何处。但是,在我的最小示例中,无论配置如何,最终都会得到NaN个结果或无法理解的结果。信号或模式/模板都不包含NaN本身。我想我只是在这里错过了有关滚动corr函数用法的琐碎小事,很乐意输入。

在我的最小示例中使用的数据:signal.csv的内容:

x_representative_signal,y_representative_signal
0.609480207401519,-0.2909085452578639
0.302817763805446,-0.36155724694545444
0.34503974911391266,-0.2500937121341063
0.5781442858502237,-0.04750282850314899
0.8743878364418098,0.1734356871382718
1.1362852032202269,0.3581472888511942
1.2966095284211565,0.47026260156684047
1.3183922941842035,0.49161742108654155
1.1779076615790691,0.38223885349457803
0.9098895260488038,0.2325360234551126
0.6307428264220079,0.03700830367822261
0.41266147736644176,-0.02199437426230552
0.2508476939728124,0.005973294162524012
0.2303711464499389,0.08756345550274755
0.16230132317598756,0.265671493732513
0.6540108563722036,-0.1345119063083255
0.9134397119333417,-0.2410536837434457
1.2368534045291402,2.067882799380657
0.3466897699577941,2.232634802226529
-1.1564176656952239,-0.251905954805923
-3.716500051975905,-3.9991497213034775
-6.597536447906082,-7.85822477586875
-10.016979637801647,-10.592127203920967
-13.055709987173474,-11.34828631449193
-12.461886416871147,-10.689295682246858
-9.868056158806336,-8.204463088197974
-6.169014186211656,-4.064346418572875
-1.437403764969658,0.612584387998341
3.832723695541548,4.8200164465189586
8.278702818579971,6.027809736356843
10.972642642563248,5.040563384216345
12.473919724055094,6.063499192325027
11.83301016316359,6.393299147027622
10.262933410963434,5.820116534884727
8.306758398889293,4.986024372987842
6.81041869604838,3.8337116060492633
4.794612932054495,2.9733021958560144
2.1574195251986255,2.8972826399479494
1.4423322534284986,2.675936788212711
1.2866906459586538,2.390105465828135
0.8925513070333135,2.237526041597313
0.3155909224265206,1.8333828035924982
0.40771110592286774,1.6535516382586695
0.36962256090226325,1.4952505277695212
0.349247710838416,1.291968862865662
0.4717398761764077,1.153842211141065
0.39840111616292806,0.9384465598841711
0.30791887193472467,0.6350476316092546
0.30393746879324945,0.5032857177358698
0.32560227737396874,0.3559097470444165
0.3928488748643394,0.33437543727209795
0.4721302641478139,0.39150697189378275
0.3540209433186279,0.43063695003700697
-0.15079464194245445,0.2097908475594854
-0.29714177402819103,0.13942803552445734
-0.5127556789114313,0.09459286331110497
0.7009211044178634,-0.6312632521384782
0.32985046784249455,0.6705984820723767
-1.2999538848885608,1.6918366988646698
-3.3282028727963464,0.7988024002941428
-6.368562409628439,-2.454984960571087
-9.154355281609313,-6.004843655143341
-12.474559077440798,-9.284772655386515
-14.741106426937222,-10.533891631647304
-15.091368927061017,-10.307432151688646
-11.811746913094945,-8.598397994877654
-6.366800693813725,-5.442737225734682
-0.3316001877491117,-1.578040099358798
5.507794446136636,2.207398421802787
9.8247423421304,4.460153856473163
14.489092664367822,4.04171064500281
16.25585276133676,4.338999604531073
15.441707672042913,5.13968157150827
13.140634401312234,5.712518124380516
10.43223026076592,5.42584546682322
8.636801480814325,4.886329713561838
6.785119952953761,3.6000210509399206
5.056218106841115,3.0173179243982897
3.1186147796084613,2.575385179739084
2.5338979949066953,2.1721492468642607
2.053330244519829,1.914804896564228
1.6862835086870396,1.697757935090109
1.1707791502406553,1.4096925677756746
0.46821577031853245,1.0801738893597126
0.3916048022544733,0.994105814301338
0.3327955545164804,0.8403770187364532
0.3537110341910711,0.8275768521932149
0.2033521980255612,0.7088148518620847
0.06079760150537267,0.6957160577258984
-0.016554189293947114,0.6646522614886176
0.003009866410683404,0.6740151365034572
-0.04714613303675863,0.5843269374879849
-0.09707927595224153,0.6005955160600307
0.030788638361286563,0.5322473243310885
-0.01469048831067099,0.48054337141440495
-0.15245224431640136,0.33379456057454054
-0.42356790816665707,0.18559584014444852
-0.47684821976367453,0.06999278418670085
-0.12300367494085954,-0.6382401318296695
-0.2604820169466121,0.770231992310322
-1.1462492726792486,1.997381604211323
-3.069591674580705,1.0039480119221686
-5.307824640467911,-2.1834300085531604
-8.019977828236895,-6.363653755915276
-10.844412174800665,-9.552065763245654
-12.83448325897207,-11.00818653345058
-14.128151818126902,-10.042537053944386
-11.448706543437554,-7.632923867308518
-6.83251171886155,-3.9834170945558833
-2.2834294959262205,-0.3985041907293443
2.16778078395113,2.603397231923591
6.250322136447228,4.404961280096129
10.01936529112389,3.1364294818545364
12.323741677358443,2.85943907585602
12.920540286898337,3.663239741640862
11.743376642063799,4.620614907638858
10.068789771397228,5.08503948300746
8.404041062516043,4.688957755122163
6.628073800180236,3.8935717770565854
4.90761108880551,3.058742952282081
2.041021467020121,2.676499440804001
1.223663255842969,2.464462314322911
1.0462515855564527,2.46878969365162
0.9235820855507872,2.245158666275051
0.762117133517484,1.6567273610462507
0.5901513914232891,1.6189277326996787
0.4951392843186713,1.3563313807545452
0.4875909987973537,1.1910778811822031
0.5244158766085443,1.1308317086629556
0.657756933648197,1.059703461410749
0.524382410823789,0.9059243701612687
0.5836652858462706,0.7990779193148393
0.42689622354274015,0.6360126523591695
0.23811536867569363,0.5454169870860123
0.10107929527584006,0.3824214582292416
0.13642252802323326,0.3884556378352375
0.06460995625165392,0.29629503939052254
0.2840850103668212,0.36377160685179377
0.4911751569761441,0.47420684893431475
0.5611919200822557,0.4956268314662894
0.7845804376590664,-0.11682508952735204
0.6809101668344166,0.15783047593841243
0.7501640733508352,2.38760787994614
-0.2237925575563368,2.3167978251563395
-2.7278491844135937,-0.3162732431953251
-5.485219207595251,-4.614351908863471
-8.385595766815193,-9.081438714371568
-11.037236584051639,-12.088574205436398
-13.056198618101519,-13.18487178050556
-13.761940954460773,-11.37215926487794
-12.790964045034626,-7.915616837920634
-9.708146656989394,-3.6786270234803773
-5.099326827510866,0.6909418600582118
-0.9590021162638527,3.4118329911432435
2.460584990094146,3.015056738965611
5.410503437997009,1.4562078592108023
8.292761358422382,2.437844146155856
10.609841316472899,3.75490125327825
11.493869832645261,5.340770424079979
11.23806979005732,6.161175111106099
10.618127668198666,6.349324154027694
9.349221466628498,5.874943936453402
7.3921155702417245,4.61286864354523
5.147089882926364,3.8194110942882844
2.858171605667442,3.265398954212102
1.2243271243731004,2.6780496211844107
1.1021193872046204,2.3318432935523843
1.3122872835591703,2.236007744357868
1.1672963524055784,1.9969451649412702
1.3201264205599208,1.6371767264433008
0.5406308644239682,1.0645893099044867
-0.18094541394719493,0.4807328757113829
-0.7232642827751996,0.09682469419821749
-1.3034626896853805,-0.2856963812257212
-1.1532904158587238,-0.21711184237310482
-0.841474888880027,-0.0408644512699311
-0.5506134099485216,0.18589415310435314
-0.22525246206110702,0.3766345478445518
0.25851254866217344,0.7006979129845148
0.31755129473667526,0.7166385656622094
0.3350920738006441,0.5280604849505762
0.15786206128888608,0.44511268223080597
0.08822239623689351,0.3641946860067021
0.41170042053599454,0.4586752455967459
0.4878255803360477,0.5037009907711643
0.3373061297308249,0.46273425494069237
0.20059422564137364,0.4056091493321618
0.2080051659769872,0.3566862280029906
0.28130644396999094,0.3833836684811328
0.1586566417232786,0.3651128728750709
0.013610355118435502,0.3563625029092197
0.08002570075473292,0.5163852569049668
0.1591460565602572,0.6322381618005625
0.2993021672626103,0.6526874387854678
0.27145014045864535,0.5226740596160401
0.0878425069288393,0.40428206310549736
-0.07455656885293399,0.3040772659590257
0.007308393204794372,0.22307140045673007
0.06956742209429884,0.14189924354570088
0.11473482795856113,0.06609735969205371
0.10129637111847076,0.036175037741453836
0.3002040481401824,0.10015067016160692
0.36790124974080907,0.1629380910310818
0.16893710449661908,0.16419016882506504
-0.11546833671627266,0.0889882675192719
-0.09282711478152356,0.13316178098800063
-0.2429287478091637,0.08470376459811242
-0.15025879187764962,0.0849335324087018
-0.04138777074943714,0.054634831750158055
0.10419527102167371,0.11066199425191224
0.13674568009570148,0.1425886905621997
0.13756372821849833,0.1824321255789101
0.013988674453204605,0.10683874105578756
-0.08597953742402421,0.029600024004392073
-0.20282865296948688,-0.06644703820754352
-0.3045957095118802,-0.15376720698421298
-0.3492018152243004,-0.19395578659326806
-0.28445214912424943,-0.13773862416580068
-0.04803596107363216,0.0750278903036605
0.43247342822124873,0.515356823957142

template.csv的内容:

x_representative_signal_mean,y_representative_signal_mean
-0.04714613303675863,0.5843269374879849
0.23152055173571298,0.5864521239869704
0.35499502897724566,0.5679132238279873
0.4787840222879458,0.5132479416875171
0.2795501940206251,0.3328467597069289
0.30722923379624134,0.17417795165852826
0.4050063455834164,-0.14224441102854463
0.7568521284053183,0.2717446804611785
0.34556732961575687,1.6517222995017722
-1.2886869053064478,1.4609628922208406
-3.727588781863141,-1.0784038718173503
-6.637136688087547,-5.088542715723133
-9.567919682740882,-8.975823242179692
-12.351977543784527,-11.552691578632757
-14.371843925052067,-11.984070265769502
-13.868713799151706,-10.623076935745758
-10.47681714395754,-7.6218043221986225
-5.7725454684596915,-3.451028672398407
-1.072342717676299,0.6222351445866666
3.5530674148997887,3.584153424739665
7.279834590563261,4.325777496869846
10.447608514114188,4.069172046348629
12.314483190700445,4.8768744131236295
12.494632569000048,5.881731076688441
11.48426159006346,6.384868999730065
10.088197619316544,6.226677960895336
8.630713686606471,5.48236525724747
6.780543223003896,4.454431584089067
4.229933169024703,3.7413954877465505
2.2865355543215906,3.136583367965859
1.5786971270838757,2.700040831604413
1.1867598073037136,2.3678702071727393
0.9580244363579149,1.9616129971190515
0.8858112670367156,1.6064823675327227
0.749115593955554,1.3688058868894553
0.5350462873610969,1.0861581738321862
0.3278637863737719,0.8462990093510462
0.11054521809510173,0.6505946131283524
-0.3874117581260483,0.13279790400521388
-1.1532904158587238,-0.21711184237310482
-0.841474888880027,-0.0408644512699311

加载和快速可视化:

import pandas as pd

signal = pd.read_csv('signal.csv')
template = pd.read_csv('template.csv')
signal.plot()
template.plot()

The longer, 2-dimensional signal. The shorter, 2-dimensional template/pattern.

在此数据上使用滚动corr函数时,我似乎会以NaN或结果无法理解,这几乎与实际用法无关,与两个维度或其中一个维度无关用于模式/模板和信号:

res1 = signal.rolling(window=template.shape[0]).corr(template, pairwise=True)
res2 = signal.rolling(window=template.shape[0]).corr(template, pairwise=False)
res3 = signal['x_representative_signal'].rolling(window=template.shape[0]).corr(template['x_representative_signal_mean'], pairwise=True)
res4 = signal['x_representative_signal'].rolling(window=template.shape[0]).corr(template['x_representative_signal_mean'], pairwise=False)
res5 = template.rolling(window=template.shape[0]).corr(signal, pairwise=True)
res6 = template.rolling(window=template.shape[0]).corr(signal, pairwise=False)
res7 = template['x_representative_signal_mean'].rolling(window=template.shape[0]).corr(signal['x_representative_signal'], pairwise=True)
res8 = template['x_representative_signal_mean'].rolling(window=template.shape[0]).corr(signal['x_representative_signal'], pairwise=False)

res2和res6产生纯NaN,res1和res5显示两个成对的相关点,而所有其他点都给出一个数值结果。

res1:

res1

res5:

res5

res3,res4,res7,res8(完全相同的结果): res3, res4, res7, res8

我现在的问题是:如何使用滚动corr函数,以便输出改为在图案/模板和信号之间提供“连续”滚动相关性?我在这里想念什么?

0 个答案:

没有答案