_function_base_impl.py 189 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760
  1. import builtins
  2. import collections.abc
  3. import functools
  4. import re
  5. import warnings
  6. import numpy as np
  7. import numpy._core.numeric as _nx
  8. from numpy._core import overrides, transpose
  9. from numpy._core._multiarray_umath import _array_converter
  10. from numpy._core.fromnumeric import any, mean, nonzero, partition, ravel, sum
  11. from numpy._core.multiarray import (
  12. _monotonicity,
  13. _place,
  14. bincount,
  15. interp as compiled_interp,
  16. interp_complex as compiled_interp_complex,
  17. normalize_axis_index,
  18. )
  19. from numpy._core.numeric import (
  20. absolute,
  21. arange,
  22. array,
  23. asanyarray,
  24. asarray,
  25. concatenate,
  26. dot,
  27. empty,
  28. integer,
  29. intp,
  30. isscalar,
  31. ndarray,
  32. ones,
  33. take,
  34. where,
  35. zeros_like,
  36. )
  37. from numpy._core.numerictypes import typecodes
  38. from numpy._core.umath import (
  39. add,
  40. arctan2,
  41. cos,
  42. exp,
  43. floor,
  44. frompyfunc,
  45. less_equal,
  46. minimum,
  47. mod,
  48. not_equal,
  49. pi,
  50. sin,
  51. sqrt,
  52. subtract,
  53. )
  54. from numpy._utils import set_module
  55. # needed in this module for compatibility
  56. from numpy.lib._histograms_impl import histogram, histogramdd # noqa: F401
  57. from numpy.lib._twodim_base_impl import diag
  58. array_function_dispatch = functools.partial(
  59. overrides.array_function_dispatch, module='numpy')
  60. __all__ = [
  61. 'select', 'piecewise', 'trim_zeros', 'copy', 'iterable', 'percentile',
  62. 'diff', 'gradient', 'angle', 'unwrap', 'sort_complex', 'flip',
  63. 'rot90', 'extract', 'place', 'vectorize', 'asarray_chkfinite', 'average',
  64. 'bincount', 'digitize', 'cov', 'corrcoef',
  65. 'median', 'sinc', 'hamming', 'hanning', 'bartlett',
  66. 'blackman', 'kaiser', 'trapezoid', 'i0',
  67. 'meshgrid', 'delete', 'insert', 'append', 'interp',
  68. 'quantile'
  69. ]
  70. # _QuantileMethods is a dictionary listing all the supported methods to
  71. # compute quantile/percentile.
  72. #
  73. # Below virtual_index refers to the index of the element where the percentile
  74. # would be found in the sorted sample.
  75. # When the sample contains exactly the percentile wanted, the virtual_index is
  76. # an integer to the index of this element.
  77. # When the percentile wanted is in between two elements, the virtual_index
  78. # is made of a integer part (a.k.a 'i' or 'left') and a fractional part
  79. # (a.k.a 'g' or 'gamma')
  80. #
  81. # Each method in _QuantileMethods has two properties
  82. # get_virtual_index : Callable
  83. # The function used to compute the virtual_index.
  84. # fix_gamma : Callable
  85. # A function used for discrete methods to force the index to a specific value.
  86. _QuantileMethods = {
  87. # --- HYNDMAN and FAN METHODS
  88. # Discrete methods
  89. 'inverted_cdf': {
  90. 'get_virtual_index': lambda n, quantiles: _inverted_cdf(n, quantiles), # noqa: PLW0108
  91. 'fix_gamma': None, # should never be called
  92. },
  93. 'averaged_inverted_cdf': {
  94. 'get_virtual_index': lambda n, quantiles: (n * quantiles) - 1,
  95. 'fix_gamma': lambda gamma, _: _get_gamma_mask(
  96. shape=gamma.shape,
  97. default_value=1.,
  98. conditioned_value=0.5,
  99. where=gamma == 0),
  100. },
  101. 'closest_observation': {
  102. 'get_virtual_index': lambda n, quantiles: _closest_observation(n, quantiles), # noqa: PLW0108
  103. 'fix_gamma': None, # should never be called
  104. },
  105. # Continuous methods
  106. 'interpolated_inverted_cdf': {
  107. 'get_virtual_index': lambda n, quantiles:
  108. _compute_virtual_index(n, quantiles, 0, 1),
  109. 'fix_gamma': lambda gamma, _: gamma,
  110. },
  111. 'hazen': {
  112. 'get_virtual_index': lambda n, quantiles:
  113. _compute_virtual_index(n, quantiles, 0.5, 0.5),
  114. 'fix_gamma': lambda gamma, _: gamma,
  115. },
  116. 'weibull': {
  117. 'get_virtual_index': lambda n, quantiles:
  118. _compute_virtual_index(n, quantiles, 0, 0),
  119. 'fix_gamma': lambda gamma, _: gamma,
  120. },
  121. # Default method.
  122. # To avoid some rounding issues, `(n-1) * quantiles` is preferred to
  123. # `_compute_virtual_index(n, quantiles, 1, 1)`.
  124. # They are mathematically equivalent.
  125. 'linear': {
  126. 'get_virtual_index': lambda n, quantiles: (n - 1) * quantiles,
  127. 'fix_gamma': lambda gamma, _: gamma,
  128. },
  129. 'median_unbiased': {
  130. 'get_virtual_index': lambda n, quantiles:
  131. _compute_virtual_index(n, quantiles, 1 / 3.0, 1 / 3.0),
  132. 'fix_gamma': lambda gamma, _: gamma,
  133. },
  134. 'normal_unbiased': {
  135. 'get_virtual_index': lambda n, quantiles:
  136. _compute_virtual_index(n, quantiles, 3 / 8.0, 3 / 8.0),
  137. 'fix_gamma': lambda gamma, _: gamma,
  138. },
  139. # --- OTHER METHODS
  140. 'lower': {
  141. 'get_virtual_index': lambda n, quantiles: np.floor(
  142. (n - 1) * quantiles).astype(np.intp),
  143. 'fix_gamma': None, # should never be called, index dtype is int
  144. },
  145. 'higher': {
  146. 'get_virtual_index': lambda n, quantiles: np.ceil(
  147. (n - 1) * quantiles).astype(np.intp),
  148. 'fix_gamma': None, # should never be called, index dtype is int
  149. },
  150. 'midpoint': {
  151. 'get_virtual_index': lambda n, quantiles: 0.5 * (
  152. np.floor((n - 1) * quantiles)
  153. + np.ceil((n - 1) * quantiles)),
  154. 'fix_gamma': lambda gamma, index: _get_gamma_mask(
  155. shape=gamma.shape,
  156. default_value=0.5,
  157. conditioned_value=0.,
  158. where=index % 1 == 0),
  159. },
  160. 'nearest': {
  161. 'get_virtual_index': lambda n, quantiles: np.around(
  162. (n - 1) * quantiles).astype(np.intp),
  163. 'fix_gamma': None,
  164. # should never be called, index dtype is int
  165. }}
  166. def _rot90_dispatcher(m, k=None, axes=None):
  167. return (m,)
  168. @array_function_dispatch(_rot90_dispatcher)
  169. def rot90(m, k=1, axes=(0, 1)):
  170. """
  171. Rotate an array by 90 degrees in the plane specified by axes.
  172. Rotation direction is from the first towards the second axis.
  173. This means for a 2D array with the default `k` and `axes`, the
  174. rotation will be counterclockwise.
  175. Parameters
  176. ----------
  177. m : array_like
  178. Array of two or more dimensions.
  179. k : integer
  180. Number of times the array is rotated by 90 degrees.
  181. axes : (2,) array_like
  182. The array is rotated in the plane defined by the axes.
  183. Axes must be different.
  184. Returns
  185. -------
  186. y : ndarray
  187. A rotated view of `m`.
  188. See Also
  189. --------
  190. flip : Reverse the order of elements in an array along the given axis.
  191. fliplr : Flip an array horizontally.
  192. flipud : Flip an array vertically.
  193. Notes
  194. -----
  195. ``rot90(m, k=1, axes=(1,0))`` is the reverse of
  196. ``rot90(m, k=1, axes=(0,1))``
  197. ``rot90(m, k=1, axes=(1,0))`` is equivalent to
  198. ``rot90(m, k=-1, axes=(0,1))``
  199. Examples
  200. --------
  201. >>> import numpy as np
  202. >>> m = np.array([[1,2],[3,4]], int)
  203. >>> m
  204. array([[1, 2],
  205. [3, 4]])
  206. >>> np.rot90(m)
  207. array([[2, 4],
  208. [1, 3]])
  209. >>> np.rot90(m, 2)
  210. array([[4, 3],
  211. [2, 1]])
  212. >>> m = np.arange(8).reshape((2,2,2))
  213. >>> np.rot90(m, 1, (1,2))
  214. array([[[1, 3],
  215. [0, 2]],
  216. [[5, 7],
  217. [4, 6]]])
  218. """
  219. axes = tuple(axes)
  220. if len(axes) != 2:
  221. raise ValueError("len(axes) must be 2.")
  222. m = asanyarray(m)
  223. if axes[0] == axes[1] or absolute(axes[0] - axes[1]) == m.ndim:
  224. raise ValueError("Axes must be different.")
  225. if (axes[0] >= m.ndim or axes[0] < -m.ndim
  226. or axes[1] >= m.ndim or axes[1] < -m.ndim):
  227. raise ValueError(f"Axes={axes} out of range for array of ndim={m.ndim}.")
  228. k %= 4
  229. if k == 0:
  230. return m[:]
  231. if k == 2:
  232. return flip(flip(m, axes[0]), axes[1])
  233. axes_list = arange(0, m.ndim)
  234. (axes_list[axes[0]], axes_list[axes[1]]) = (axes_list[axes[1]],
  235. axes_list[axes[0]])
  236. if k == 1:
  237. return transpose(flip(m, axes[1]), axes_list)
  238. else:
  239. # k == 3
  240. return flip(transpose(m, axes_list), axes[1])
  241. def _flip_dispatcher(m, axis=None):
  242. return (m,)
  243. @array_function_dispatch(_flip_dispatcher)
  244. def flip(m, axis=None):
  245. """
  246. Reverse the order of elements in an array along the given axis.
  247. The shape of the array is preserved, but the elements are reordered.
  248. Parameters
  249. ----------
  250. m : array_like
  251. Input array.
  252. axis : None or int or tuple of ints, optional
  253. Axis or axes along which to flip over. The default,
  254. axis=None, will flip over all of the axes of the input array.
  255. If axis is negative it counts from the last to the first axis.
  256. If axis is a tuple of ints, flipping is performed on all of the axes
  257. specified in the tuple.
  258. Returns
  259. -------
  260. out : array_like
  261. A view of `m` with the entries of axis reversed. Since a view is
  262. returned, this operation is done in constant time.
  263. See Also
  264. --------
  265. flipud : Flip an array vertically (axis=0).
  266. fliplr : Flip an array horizontally (axis=1).
  267. Notes
  268. -----
  269. flip(m, 0) is equivalent to flipud(m).
  270. flip(m, 1) is equivalent to fliplr(m).
  271. flip(m, n) corresponds to ``m[...,::-1,...]`` with ``::-1`` at position n.
  272. flip(m) corresponds to ``m[::-1,::-1,...,::-1]`` with ``::-1`` at all
  273. positions.
  274. flip(m, (0, 1)) corresponds to ``m[::-1,::-1,...]`` with ``::-1`` at
  275. position 0 and position 1.
  276. Examples
  277. --------
  278. >>> import numpy as np
  279. >>> A = np.arange(8).reshape((2,2,2))
  280. >>> A
  281. array([[[0, 1],
  282. [2, 3]],
  283. [[4, 5],
  284. [6, 7]]])
  285. >>> np.flip(A, 0)
  286. array([[[4, 5],
  287. [6, 7]],
  288. [[0, 1],
  289. [2, 3]]])
  290. >>> np.flip(A, 1)
  291. array([[[2, 3],
  292. [0, 1]],
  293. [[6, 7],
  294. [4, 5]]])
  295. >>> np.flip(A)
  296. array([[[7, 6],
  297. [5, 4]],
  298. [[3, 2],
  299. [1, 0]]])
  300. >>> np.flip(A, (0, 2))
  301. array([[[5, 4],
  302. [7, 6]],
  303. [[1, 0],
  304. [3, 2]]])
  305. >>> rng = np.random.default_rng()
  306. >>> A = rng.normal(size=(3,4,5))
  307. >>> np.all(np.flip(A,2) == A[:,:,::-1,...])
  308. True
  309. """
  310. if not hasattr(m, 'ndim'):
  311. m = asarray(m)
  312. if axis is None:
  313. indexer = (np.s_[::-1],) * m.ndim
  314. else:
  315. axis = _nx.normalize_axis_tuple(axis, m.ndim)
  316. indexer = [np.s_[:]] * m.ndim
  317. for ax in axis:
  318. indexer[ax] = np.s_[::-1]
  319. indexer = tuple(indexer)
  320. return m[indexer]
  321. @set_module('numpy')
  322. def iterable(y):
  323. """
  324. Check whether or not an object can be iterated over.
  325. Parameters
  326. ----------
  327. y : object
  328. Input object.
  329. Returns
  330. -------
  331. b : bool
  332. Return ``True`` if the object has an iterator method or is a
  333. sequence and ``False`` otherwise.
  334. Examples
  335. --------
  336. >>> import numpy as np
  337. >>> np.iterable([1, 2, 3])
  338. True
  339. >>> np.iterable(2)
  340. False
  341. Notes
  342. -----
  343. In most cases, the results of ``np.iterable(obj)`` are consistent with
  344. ``isinstance(obj, collections.abc.Iterable)``. One notable exception is
  345. the treatment of 0-dimensional arrays::
  346. >>> from collections.abc import Iterable
  347. >>> a = np.array(1.0) # 0-dimensional numpy array
  348. >>> isinstance(a, Iterable)
  349. True
  350. >>> np.iterable(a)
  351. False
  352. """
  353. try:
  354. iter(y)
  355. except TypeError:
  356. return False
  357. return True
  358. def _weights_are_valid(weights, a, axis):
  359. """Validate weights array.
  360. We assume, weights is not None.
  361. """
  362. wgt = np.asanyarray(weights)
  363. # Sanity checks
  364. if a.shape != wgt.shape:
  365. if axis is None:
  366. raise TypeError(
  367. "Axis must be specified when shapes of a and weights "
  368. "differ.")
  369. if wgt.shape != tuple(a.shape[ax] for ax in axis):
  370. raise ValueError(
  371. "Shape of weights must be consistent with "
  372. "shape of a along specified axis.")
  373. # setup wgt to broadcast along axis
  374. wgt = wgt.transpose(np.argsort(axis))
  375. wgt = wgt.reshape(tuple((s if ax in axis else 1)
  376. for ax, s in enumerate(a.shape)))
  377. return wgt
  378. def _average_dispatcher(a, axis=None, weights=None, returned=None, *,
  379. keepdims=None):
  380. return (a, weights)
  381. @array_function_dispatch(_average_dispatcher)
  382. def average(a, axis=None, weights=None, returned=False, *,
  383. keepdims=np._NoValue):
  384. """
  385. Compute the weighted average along the specified axis.
  386. Parameters
  387. ----------
  388. a : array_like
  389. Array containing data to be averaged. If `a` is not an array, a
  390. conversion is attempted.
  391. axis : None or int or tuple of ints, optional
  392. Axis or axes along which to average `a`. The default,
  393. `axis=None`, will average over all of the elements of the input array.
  394. If axis is negative it counts from the last to the first axis.
  395. If axis is a tuple of ints, averaging is performed on all of the axes
  396. specified in the tuple instead of a single axis or all the axes as
  397. before.
  398. weights : array_like, optional
  399. An array of weights associated with the values in `a`. Each value in
  400. `a` contributes to the average according to its associated weight.
  401. The array of weights must be the same shape as `a` if no axis is
  402. specified, otherwise the weights must have dimensions and shape
  403. consistent with `a` along the specified axis.
  404. If `weights=None`, then all data in `a` are assumed to have a
  405. weight equal to one.
  406. The calculation is::
  407. avg = sum(a * weights) / sum(weights)
  408. where the sum is over all included elements.
  409. The only constraint on the values of `weights` is that `sum(weights)`
  410. must not be 0.
  411. returned : bool, optional
  412. Default is `False`. If `True`, the tuple (`average`, `sum_of_weights`)
  413. is returned, otherwise only the average is returned.
  414. If `weights=None`, `sum_of_weights` is equivalent to the number of
  415. elements over which the average is taken.
  416. keepdims : bool, optional
  417. If this is set to True, the axes which are reduced are left
  418. in the result as dimensions with size one. With this option,
  419. the result will broadcast correctly against the original `a`.
  420. *Note:* `keepdims` will not work with instances of `numpy.matrix`
  421. or other classes whose methods do not support `keepdims`.
  422. .. versionadded:: 1.23.0
  423. Returns
  424. -------
  425. retval, [sum_of_weights] : array_type or double
  426. Return the average along the specified axis. When `returned` is `True`,
  427. return a tuple with the average as the first element and the sum
  428. of the weights as the second element. `sum_of_weights` is of the
  429. same type as `retval`. The result dtype follows a general pattern.
  430. If `weights` is None, the result dtype will be that of `a` , or ``float64``
  431. if `a` is integral. Otherwise, if `weights` is not None and `a` is non-
  432. integral, the result type will be the type of lowest precision capable of
  433. representing values of both `a` and `weights`. If `a` happens to be
  434. integral, the previous rules still applies but the result dtype will
  435. at least be ``float64``.
  436. Raises
  437. ------
  438. ZeroDivisionError
  439. When all weights along axis are zero. See `numpy.ma.average` for a
  440. version robust to this type of error.
  441. TypeError
  442. When `weights` does not have the same shape as `a`, and `axis=None`.
  443. ValueError
  444. When `weights` does not have dimensions and shape consistent with `a`
  445. along specified `axis`.
  446. See Also
  447. --------
  448. mean
  449. ma.average : average for masked arrays -- useful if your data contains
  450. "missing" values
  451. numpy.result_type : Returns the type that results from applying the
  452. numpy type promotion rules to the arguments.
  453. Examples
  454. --------
  455. >>> import numpy as np
  456. >>> data = np.arange(1, 5)
  457. >>> data
  458. array([1, 2, 3, 4])
  459. >>> np.average(data)
  460. 2.5
  461. >>> np.average(np.arange(1, 11), weights=np.arange(10, 0, -1))
  462. 4.0
  463. >>> data = np.arange(6).reshape((3, 2))
  464. >>> data
  465. array([[0, 1],
  466. [2, 3],
  467. [4, 5]])
  468. >>> np.average(data, axis=1, weights=[1./4, 3./4])
  469. array([0.75, 2.75, 4.75])
  470. >>> np.average(data, weights=[1./4, 3./4])
  471. Traceback (most recent call last):
  472. ...
  473. TypeError: Axis must be specified when shapes of a and weights differ.
  474. With ``keepdims=True``, the following result has shape (3, 1).
  475. >>> np.average(data, axis=1, keepdims=True)
  476. array([[0.5],
  477. [2.5],
  478. [4.5]])
  479. >>> data = np.arange(8).reshape((2, 2, 2))
  480. >>> data
  481. array([[[0, 1],
  482. [2, 3]],
  483. [[4, 5],
  484. [6, 7]]])
  485. >>> np.average(data, axis=(0, 1), weights=[[1./4, 3./4], [1., 1./2]])
  486. array([3.4, 4.4])
  487. >>> np.average(data, axis=0, weights=[[1./4, 3./4], [1., 1./2]])
  488. Traceback (most recent call last):
  489. ...
  490. ValueError: Shape of weights must be consistent
  491. with shape of a along specified axis.
  492. """
  493. a = np.asanyarray(a)
  494. if axis is not None:
  495. axis = _nx.normalize_axis_tuple(axis, a.ndim, argname="axis")
  496. if keepdims is np._NoValue:
  497. # Don't pass on the keepdims argument if one wasn't given.
  498. keepdims_kw = {}
  499. else:
  500. keepdims_kw = {'keepdims': keepdims}
  501. if weights is None:
  502. avg = a.mean(axis, **keepdims_kw)
  503. avg_as_array = np.asanyarray(avg)
  504. scl = avg_as_array.dtype.type(a.size / avg_as_array.size)
  505. else:
  506. wgt = _weights_are_valid(weights=weights, a=a, axis=axis)
  507. if issubclass(a.dtype.type, (np.integer, np.bool)):
  508. result_dtype = np.result_type(a.dtype, wgt.dtype, 'f8')
  509. else:
  510. result_dtype = np.result_type(a.dtype, wgt.dtype)
  511. scl = wgt.sum(axis=axis, dtype=result_dtype, **keepdims_kw)
  512. if np.any(scl == 0.0):
  513. raise ZeroDivisionError(
  514. "Weights sum to zero, can't be normalized")
  515. avg = avg_as_array = np.multiply(a, wgt,
  516. dtype=result_dtype).sum(axis, **keepdims_kw) / scl
  517. if returned:
  518. if scl.shape != avg_as_array.shape:
  519. scl = np.broadcast_to(scl, avg_as_array.shape, subok=True).copy()
  520. return avg, scl
  521. else:
  522. return avg
  523. @set_module('numpy')
  524. def asarray_chkfinite(a, dtype=None, order=None):
  525. """Convert the input to an array, checking for NaNs or Infs.
  526. Parameters
  527. ----------
  528. a : array_like
  529. Input data, in any form that can be converted to an array. This
  530. includes lists, lists of tuples, tuples, tuples of tuples, tuples
  531. of lists and ndarrays. Success requires no NaNs or Infs.
  532. dtype : data-type, optional
  533. By default, the data-type is inferred from the input data.
  534. order : {'C', 'F', 'A', 'K'}, optional
  535. The memory layout of the output.
  536. 'C' gives a row-major layout (C-style),
  537. 'F' gives a column-major layout (Fortran-style).
  538. 'C' and 'F' will copy if needed to ensure the output format.
  539. 'A' (any) is equivalent to 'F' if input a is non-contiguous or
  540. Fortran-contiguous, otherwise, it is equivalent to 'C'.
  541. Unlike 'C' or 'F', 'A' does not ensure that the result is contiguous.
  542. 'K' (keep) preserves the input order for the output.
  543. 'C' is the default.
  544. Returns
  545. -------
  546. out : ndarray
  547. Array interpretation of `a`. No copy is performed if the input
  548. is already an ndarray. If `a` is a subclass of ndarray, a base
  549. class ndarray is returned.
  550. Raises
  551. ------
  552. ValueError
  553. Raises ValueError if `a` contains NaN (Not a Number) or Inf (Infinity).
  554. See Also
  555. --------
  556. asarray : Create and array.
  557. asanyarray : Similar function which passes through subclasses.
  558. ascontiguousarray : Convert input to a contiguous array.
  559. asfortranarray : Convert input to an ndarray with column-major
  560. memory order.
  561. fromiter : Create an array from an iterator.
  562. fromfunction : Construct an array by executing a function on grid
  563. positions.
  564. Examples
  565. --------
  566. >>> import numpy as np
  567. Convert a list into an array. If all elements are finite, then
  568. ``asarray_chkfinite`` is identical to ``asarray``.
  569. >>> a = [1, 2]
  570. >>> np.asarray_chkfinite(a, dtype=float)
  571. array([1., 2.])
  572. Raises ValueError if array_like contains Nans or Infs.
  573. >>> a = [1, 2, np.inf]
  574. >>> try:
  575. ... np.asarray_chkfinite(a)
  576. ... except ValueError:
  577. ... print('ValueError')
  578. ...
  579. ValueError
  580. """
  581. a = asarray(a, dtype=dtype, order=order)
  582. if a.dtype.char in typecodes['AllFloat'] and not np.isfinite(a).all():
  583. raise ValueError(
  584. "array must not contain infs or NaNs")
  585. return a
  586. def _piecewise_dispatcher(x, condlist, funclist, *args, **kw):
  587. yield x
  588. # support the undocumented behavior of allowing scalars
  589. if np.iterable(condlist):
  590. yield from condlist
  591. @array_function_dispatch(_piecewise_dispatcher)
  592. def piecewise(x, condlist, funclist, *args, **kw):
  593. """
  594. Evaluate a piecewise-defined function.
  595. Given a set of conditions and corresponding functions, evaluate each
  596. function on the input data wherever its condition is true.
  597. Parameters
  598. ----------
  599. x : ndarray or scalar
  600. The input domain.
  601. condlist : list of bool arrays or bool scalars
  602. Each boolean array corresponds to a function in `funclist`. Wherever
  603. `condlist[i]` is True, `funclist[i](x)` is used as the output value.
  604. Each boolean array in `condlist` selects a piece of `x`,
  605. and should therefore be of the same shape as `x`.
  606. The length of `condlist` must correspond to that of `funclist`.
  607. If one extra function is given, i.e. if
  608. ``len(funclist) == len(condlist) + 1``, then that extra function
  609. is the default value, used wherever all conditions are false.
  610. funclist : list of callables, f(x,*args,**kw), or scalars
  611. Each function is evaluated over `x` wherever its corresponding
  612. condition is True. It should take a 1d array as input and give a 1d
  613. array or a scalar value as output. If, instead of a callable,
  614. a scalar is provided then a constant function (``lambda x: scalar``) is
  615. assumed.
  616. args : tuple, optional
  617. Any further arguments given to `piecewise` are passed to the functions
  618. upon execution, i.e., if called ``piecewise(..., ..., 1, 'a')``, then
  619. each function is called as ``f(x, 1, 'a')``.
  620. kw : dict, optional
  621. Keyword arguments used in calling `piecewise` are passed to the
  622. functions upon execution, i.e., if called
  623. ``piecewise(..., ..., alpha=1)``, then each function is called as
  624. ``f(x, alpha=1)``.
  625. Returns
  626. -------
  627. out : ndarray
  628. The output is the same shape and type as x and is found by
  629. calling the functions in `funclist` on the appropriate portions of `x`,
  630. as defined by the boolean arrays in `condlist`. Portions not covered
  631. by any condition have a default value of 0.
  632. See Also
  633. --------
  634. choose, select, where
  635. Notes
  636. -----
  637. This is similar to choose or select, except that functions are
  638. evaluated on elements of `x` that satisfy the corresponding condition from
  639. `condlist`.
  640. The result is::
  641. |--
  642. |funclist[0](x[condlist[0]])
  643. out = |funclist[1](x[condlist[1]])
  644. |...
  645. |funclist[n2](x[condlist[n2]])
  646. |--
  647. Examples
  648. --------
  649. >>> import numpy as np
  650. Define the signum function, which is -1 for ``x < 0`` and +1 for ``x >= 0``.
  651. >>> x = np.linspace(-2.5, 2.5, 6)
  652. >>> np.piecewise(x, [x < 0, x >= 0], [-1, 1])
  653. array([-1., -1., -1., 1., 1., 1.])
  654. Define the absolute value, which is ``-x`` for ``x <0`` and ``x`` for
  655. ``x >= 0``.
  656. >>> np.piecewise(x, [x < 0, x >= 0], [lambda x: -x, lambda x: x])
  657. array([2.5, 1.5, 0.5, 0.5, 1.5, 2.5])
  658. Apply the same function to a scalar value.
  659. >>> y = -2
  660. >>> np.piecewise(y, [y < 0, y >= 0], [lambda x: -x, lambda x: x])
  661. array(2)
  662. """
  663. x = asanyarray(x)
  664. n2 = len(funclist)
  665. # undocumented: single condition is promoted to a list of one condition
  666. if isscalar(condlist) or (
  667. not isinstance(condlist[0], (list, ndarray)) and x.ndim != 0):
  668. condlist = [condlist]
  669. condlist = asarray(condlist, dtype=bool)
  670. n = len(condlist)
  671. if n == n2 - 1: # compute the "otherwise" condition.
  672. condelse = ~np.any(condlist, axis=0, keepdims=True)
  673. condlist = np.concatenate([condlist, condelse], axis=0)
  674. n += 1
  675. elif n != n2:
  676. raise ValueError(
  677. f"with {n} condition(s), either {n} or {n + 1} functions are expected"
  678. )
  679. y = zeros_like(x)
  680. for cond, func in zip(condlist, funclist):
  681. if not isinstance(func, collections.abc.Callable):
  682. y[cond] = func
  683. else:
  684. vals = x[cond]
  685. if vals.size > 0:
  686. y[cond] = func(vals, *args, **kw)
  687. return y
  688. def _select_dispatcher(condlist, choicelist, default=None):
  689. yield from condlist
  690. yield from choicelist
  691. @array_function_dispatch(_select_dispatcher)
  692. def select(condlist, choicelist, default=0):
  693. """
  694. Return an array drawn from elements in choicelist, depending on conditions.
  695. Parameters
  696. ----------
  697. condlist : list of bool ndarrays
  698. The list of conditions which determine from which array in `choicelist`
  699. the output elements are taken. When multiple conditions are satisfied,
  700. the first one encountered in `condlist` is used.
  701. choicelist : list of ndarrays
  702. The list of arrays from which the output elements are taken. It has
  703. to be of the same length as `condlist`.
  704. default : array_like, optional
  705. The element inserted in `output` when all conditions evaluate to False.
  706. Returns
  707. -------
  708. output : ndarray
  709. The output at position m is the m-th element of the array in
  710. `choicelist` where the m-th element of the corresponding array in
  711. `condlist` is True.
  712. See Also
  713. --------
  714. where : Return elements from one of two arrays depending on condition.
  715. take, choose, compress, diag, diagonal
  716. Examples
  717. --------
  718. >>> import numpy as np
  719. Beginning with an array of integers from 0 to 5 (inclusive),
  720. elements less than ``3`` are negated, elements greater than ``3``
  721. are squared, and elements not meeting either of these conditions
  722. (exactly ``3``) are replaced with a `default` value of ``42``.
  723. >>> x = np.arange(6)
  724. >>> condlist = [x<3, x>3]
  725. >>> choicelist = [-x, x**2]
  726. >>> np.select(condlist, choicelist, 42)
  727. array([ 0, -1, -2, 42, 16, 25])
  728. When multiple conditions are satisfied, the first one encountered in
  729. `condlist` is used.
  730. >>> condlist = [x<=4, x>3]
  731. >>> choicelist = [x, x**2]
  732. >>> np.select(condlist, choicelist, 55)
  733. array([ 0, 1, 2, 3, 4, 25])
  734. """
  735. # Check the size of condlist and choicelist are the same, or abort.
  736. if len(condlist) != len(choicelist):
  737. raise ValueError(
  738. 'list of cases must be same length as list of conditions')
  739. # Now that the dtype is known, handle the deprecated select([], []) case
  740. if len(condlist) == 0:
  741. raise ValueError("select with an empty condition list is not possible")
  742. # TODO: This preserves the Python int, float, complex manually to get the
  743. # right `result_type` with NEP 50. Most likely we will grow a better
  744. # way to spell this (and this can be replaced).
  745. choicelist = [
  746. choice if type(choice) in (int, float, complex) else np.asarray(choice)
  747. for choice in choicelist]
  748. choicelist.append(default if type(default) in (int, float, complex)
  749. else np.asarray(default))
  750. try:
  751. dtype = np.result_type(*choicelist)
  752. except TypeError as e:
  753. msg = f'Choicelist and default value do not have a common dtype: {e}'
  754. raise TypeError(msg) from None
  755. # Convert conditions to arrays and broadcast conditions and choices
  756. # as the shape is needed for the result. Doing it separately optimizes
  757. # for example when all choices are scalars.
  758. condlist = np.broadcast_arrays(*condlist)
  759. choicelist = np.broadcast_arrays(*choicelist)
  760. # If cond array is not an ndarray in boolean format or scalar bool, abort.
  761. for i, cond in enumerate(condlist):
  762. if cond.dtype.type is not np.bool:
  763. raise TypeError(
  764. f'invalid entry {i} in condlist: should be boolean ndarray')
  765. if choicelist[0].ndim == 0:
  766. # This may be common, so avoid the call.
  767. result_shape = condlist[0].shape
  768. else:
  769. result_shape = np.broadcast_arrays(condlist[0], choicelist[0])[0].shape
  770. result = np.full(result_shape, choicelist[-1], dtype)
  771. # Use np.copyto to burn each choicelist array onto result, using the
  772. # corresponding condlist as a boolean mask. This is done in reverse
  773. # order since the first choice should take precedence.
  774. choicelist = choicelist[-2::-1]
  775. condlist = condlist[::-1]
  776. for choice, cond in zip(choicelist, condlist):
  777. np.copyto(result, choice, where=cond)
  778. return result
  779. def _copy_dispatcher(a, order=None, subok=None):
  780. return (a,)
  781. @array_function_dispatch(_copy_dispatcher)
  782. def copy(a, order='K', subok=False):
  783. """
  784. Return an array copy of the given object.
  785. Parameters
  786. ----------
  787. a : array_like
  788. Input data.
  789. order : {'C', 'F', 'A', 'K'}, optional
  790. Controls the memory layout of the copy. 'C' means C-order,
  791. 'F' means F-order, 'A' means 'F' if `a` is Fortran contiguous,
  792. 'C' otherwise. 'K' means match the layout of `a` as closely
  793. as possible. (Note that this function and :meth:`ndarray.copy` are very
  794. similar, but have different default values for their order=
  795. arguments.)
  796. subok : bool, optional
  797. If True, then sub-classes will be passed-through, otherwise the
  798. returned array will be forced to be a base-class array (defaults to False).
  799. Returns
  800. -------
  801. arr : ndarray
  802. Array interpretation of `a`.
  803. See Also
  804. --------
  805. ndarray.copy : Preferred method for creating an array copy
  806. Notes
  807. -----
  808. This is equivalent to:
  809. >>> np.array(a, copy=True) #doctest: +SKIP
  810. The copy made of the data is shallow, i.e., for arrays with object dtype,
  811. the new array will point to the same objects.
  812. See Examples from `ndarray.copy`.
  813. Examples
  814. --------
  815. >>> import numpy as np
  816. Create an array x, with a reference y and a copy z:
  817. >>> x = np.array([1, 2, 3])
  818. >>> y = x
  819. >>> z = np.copy(x)
  820. Note that, when we modify x, y changes, but not z:
  821. >>> x[0] = 10
  822. >>> x[0] == y[0]
  823. True
  824. >>> x[0] == z[0]
  825. False
  826. Note that, np.copy clears previously set WRITEABLE=False flag.
  827. >>> a = np.array([1, 2, 3])
  828. >>> a.flags["WRITEABLE"] = False
  829. >>> b = np.copy(a)
  830. >>> b.flags["WRITEABLE"]
  831. True
  832. >>> b[0] = 3
  833. >>> b
  834. array([3, 2, 3])
  835. """
  836. return array(a, order=order, subok=subok, copy=True)
  837. # Basic operations
  838. def _gradient_dispatcher(f, *varargs, axis=None, edge_order=None):
  839. yield f
  840. yield from varargs
  841. @array_function_dispatch(_gradient_dispatcher)
  842. def gradient(f, *varargs, axis=None, edge_order=1):
  843. """
  844. Return the gradient of an N-dimensional array.
  845. The gradient is computed using second order accurate central differences
  846. in the interior points and either first or second order accurate one-sides
  847. (forward or backwards) differences at the boundaries.
  848. The returned gradient hence has the same shape as the input array.
  849. Parameters
  850. ----------
  851. f : array_like
  852. An N-dimensional array containing samples of a scalar function.
  853. varargs : list of scalar or array, optional
  854. Spacing between f values. Default unitary spacing for all dimensions.
  855. Spacing can be specified using:
  856. 1. single scalar to specify a sample distance for all dimensions.
  857. 2. N scalars to specify a constant sample distance for each dimension.
  858. i.e. `dx`, `dy`, `dz`, ...
  859. 3. N arrays to specify the coordinates of the values along each
  860. dimension of F. The length of the array must match the size of
  861. the corresponding dimension
  862. 4. Any combination of N scalars/arrays with the meaning of 2. and 3.
  863. If `axis` is given, the number of varargs must equal the number of axes
  864. specified in the axis parameter.
  865. Default: 1. (see Examples below).
  866. edge_order : {1, 2}, optional
  867. Gradient is calculated using N-th order accurate differences
  868. at the boundaries. Default: 1.
  869. axis : None or int or tuple of ints, optional
  870. Gradient is calculated only along the given axis or axes
  871. The default (axis = None) is to calculate the gradient for all the axes
  872. of the input array. axis may be negative, in which case it counts from
  873. the last to the first axis.
  874. Returns
  875. -------
  876. gradient : ndarray or tuple of ndarray
  877. A tuple of ndarrays (or a single ndarray if there is only one
  878. dimension) corresponding to the derivatives of f with respect
  879. to each dimension. Each derivative has the same shape as f.
  880. Examples
  881. --------
  882. >>> import numpy as np
  883. >>> f = np.array([1, 2, 4, 7, 11, 16])
  884. >>> np.gradient(f)
  885. array([1. , 1.5, 2.5, 3.5, 4.5, 5. ])
  886. >>> np.gradient(f, 2)
  887. array([0.5 , 0.75, 1.25, 1.75, 2.25, 2.5 ])
  888. Spacing can be also specified with an array that represents the coordinates
  889. of the values F along the dimensions.
  890. For instance a uniform spacing:
  891. >>> x = np.arange(f.size)
  892. >>> np.gradient(f, x)
  893. array([1. , 1.5, 2.5, 3.5, 4.5, 5. ])
  894. Or a non uniform one:
  895. >>> x = np.array([0., 1., 1.5, 3.5, 4., 6.])
  896. >>> np.gradient(f, x)
  897. array([1. , 3. , 3.5, 6.7, 6.9, 2.5])
  898. For two dimensional arrays, the return will be two arrays ordered by
  899. axis. In this example the first array stands for the gradient in
  900. rows and the second one in columns direction:
  901. >>> np.gradient(np.array([[1, 2, 6], [3, 4, 5]]))
  902. (array([[ 2., 2., -1.],
  903. [ 2., 2., -1.]]),
  904. array([[1. , 2.5, 4. ],
  905. [1. , 1. , 1. ]]))
  906. In this example the spacing is also specified:
  907. uniform for axis=0 and non uniform for axis=1
  908. >>> dx = 2.
  909. >>> y = [1., 1.5, 3.5]
  910. >>> np.gradient(np.array([[1, 2, 6], [3, 4, 5]]), dx, y)
  911. (array([[ 1. , 1. , -0.5],
  912. [ 1. , 1. , -0.5]]),
  913. array([[2. , 2. , 2. ],
  914. [2. , 1.7, 0.5]]))
  915. It is possible to specify how boundaries are treated using `edge_order`
  916. >>> x = np.array([0, 1, 2, 3, 4])
  917. >>> f = x**2
  918. >>> np.gradient(f, edge_order=1)
  919. array([1., 2., 4., 6., 7.])
  920. >>> np.gradient(f, edge_order=2)
  921. array([0., 2., 4., 6., 8.])
  922. The `axis` keyword can be used to specify a subset of axes of which the
  923. gradient is calculated
  924. >>> np.gradient(np.array([[1, 2, 6], [3, 4, 5]]), axis=0)
  925. array([[ 2., 2., -1.],
  926. [ 2., 2., -1.]])
  927. The `varargs` argument defines the spacing between sample points in the
  928. input array. It can take two forms:
  929. 1. An array, specifying coordinates, which may be unevenly spaced:
  930. >>> x = np.array([0., 2., 3., 6., 8.])
  931. >>> y = x ** 2
  932. >>> np.gradient(y, x, edge_order=2)
  933. array([ 0., 4., 6., 12., 16.])
  934. 2. A scalar, representing the fixed sample distance:
  935. >>> dx = 2
  936. >>> x = np.array([0., 2., 4., 6., 8.])
  937. >>> y = x ** 2
  938. >>> np.gradient(y, dx, edge_order=2)
  939. array([ 0., 4., 8., 12., 16.])
  940. It's possible to provide different data for spacing along each dimension.
  941. The number of arguments must match the number of dimensions in the input
  942. data.
  943. >>> dx = 2
  944. >>> dy = 3
  945. >>> x = np.arange(0, 6, dx)
  946. >>> y = np.arange(0, 9, dy)
  947. >>> xs, ys = np.meshgrid(x, y)
  948. >>> zs = xs + 2 * ys
  949. >>> np.gradient(zs, dy, dx) # Passing two scalars
  950. (array([[2., 2., 2.],
  951. [2., 2., 2.],
  952. [2., 2., 2.]]),
  953. array([[1., 1., 1.],
  954. [1., 1., 1.],
  955. [1., 1., 1.]]))
  956. Mixing scalars and arrays is also allowed:
  957. >>> np.gradient(zs, y, dx) # Passing one array and one scalar
  958. (array([[2., 2., 2.],
  959. [2., 2., 2.],
  960. [2., 2., 2.]]),
  961. array([[1., 1., 1.],
  962. [1., 1., 1.],
  963. [1., 1., 1.]]))
  964. Notes
  965. -----
  966. Assuming that :math:`f\\in C^{3}` (i.e., :math:`f` has at least 3 continuous
  967. derivatives) and let :math:`h_{*}` be a non-homogeneous stepsize, we
  968. minimize the "consistency error" :math:`\\eta_{i}` between the true gradient
  969. and its estimate from a linear combination of the neighboring grid-points:
  970. .. math::
  971. \\eta_{i} = f_{i}^{\\left(1\\right)} -
  972. \\left[ \\alpha f\\left(x_{i}\\right) +
  973. \\beta f\\left(x_{i} + h_{d}\\right) +
  974. \\gamma f\\left(x_{i}-h_{s}\\right)
  975. \\right]
  976. By substituting :math:`f(x_{i} + h_{d})` and :math:`f(x_{i} - h_{s})`
  977. with their Taylor series expansion, this translates into solving
  978. the following the linear system:
  979. .. math::
  980. \\left\\{
  981. \\begin{array}{r}
  982. \\alpha+\\beta+\\gamma=0 \\\\
  983. \\beta h_{d}-\\gamma h_{s}=1 \\\\
  984. \\beta h_{d}^{2}+\\gamma h_{s}^{2}=0
  985. \\end{array}
  986. \\right.
  987. The resulting approximation of :math:`f_{i}^{(1)}` is the following:
  988. .. math::
  989. \\hat f_{i}^{(1)} =
  990. \\frac{
  991. h_{s}^{2}f\\left(x_{i} + h_{d}\\right)
  992. + \\left(h_{d}^{2} - h_{s}^{2}\\right)f\\left(x_{i}\\right)
  993. - h_{d}^{2}f\\left(x_{i}-h_{s}\\right)}
  994. { h_{s}h_{d}\\left(h_{d} + h_{s}\\right)}
  995. + \\mathcal{O}\\left(\\frac{h_{d}h_{s}^{2}
  996. + h_{s}h_{d}^{2}}{h_{d}
  997. + h_{s}}\\right)
  998. It is worth noting that if :math:`h_{s}=h_{d}`
  999. (i.e., data are evenly spaced)
  1000. we find the standard second order approximation:
  1001. .. math::
  1002. \\hat f_{i}^{(1)}=
  1003. \\frac{f\\left(x_{i+1}\\right) - f\\left(x_{i-1}\\right)}{2h}
  1004. + \\mathcal{O}\\left(h^{2}\\right)
  1005. With a similar procedure the forward/backward approximations used for
  1006. boundaries can be derived.
  1007. References
  1008. ----------
  1009. .. [1] Quarteroni A., Sacco R., Saleri F. (2007) Numerical Mathematics
  1010. (Texts in Applied Mathematics). New York: Springer.
  1011. .. [2] Durran D. R. (1999) Numerical Methods for Wave Equations
  1012. in Geophysical Fluid Dynamics. New York: Springer.
  1013. .. [3] Fornberg B. (1988) Generation of Finite Difference Formulas on
  1014. Arbitrarily Spaced Grids,
  1015. Mathematics of Computation 51, no. 184 : 699-706.
  1016. `PDF <https://www.ams.org/journals/mcom/1988-51-184/
  1017. S0025-5718-1988-0935077-0/S0025-5718-1988-0935077-0.pdf>`_.
  1018. """
  1019. f = np.asanyarray(f)
  1020. N = f.ndim # number of dimensions
  1021. if axis is None:
  1022. axes = tuple(range(N))
  1023. else:
  1024. axes = _nx.normalize_axis_tuple(axis, N)
  1025. len_axes = len(axes)
  1026. n = len(varargs)
  1027. if n == 0:
  1028. # no spacing argument - use 1 in all axes
  1029. dx = [1.0] * len_axes
  1030. elif n == 1 and np.ndim(varargs[0]) == 0:
  1031. # single scalar for all axes
  1032. dx = varargs * len_axes
  1033. elif n == len_axes:
  1034. # scalar or 1d array for each axis
  1035. dx = list(varargs)
  1036. for i, distances in enumerate(dx):
  1037. distances = np.asanyarray(distances)
  1038. if distances.ndim == 0:
  1039. continue
  1040. elif distances.ndim != 1:
  1041. raise ValueError("distances must be either scalars or 1d")
  1042. if len(distances) != f.shape[axes[i]]:
  1043. raise ValueError("when 1d, distances must match "
  1044. "the length of the corresponding dimension")
  1045. if np.issubdtype(distances.dtype, np.integer):
  1046. # Convert numpy integer types to float64 to avoid modular
  1047. # arithmetic in np.diff(distances).
  1048. distances = distances.astype(np.float64)
  1049. diffx = np.diff(distances)
  1050. # if distances are constant reduce to the scalar case
  1051. # since it brings a consistent speedup
  1052. if (diffx == diffx[0]).all():
  1053. diffx = diffx[0]
  1054. dx[i] = diffx
  1055. else:
  1056. raise TypeError("invalid number of arguments")
  1057. if edge_order > 2:
  1058. raise ValueError("'edge_order' greater than 2 not supported")
  1059. # use central differences on interior and one-sided differences on the
  1060. # endpoints. This preserves second order-accuracy over the full domain.
  1061. outvals = []
  1062. # create slice objects --- initially all are [:, :, ..., :]
  1063. slice1 = [slice(None)] * N
  1064. slice2 = [slice(None)] * N
  1065. slice3 = [slice(None)] * N
  1066. slice4 = [slice(None)] * N
  1067. otype = f.dtype
  1068. if otype.type is np.datetime64:
  1069. # the timedelta dtype with the same unit information
  1070. otype = np.dtype(otype.name.replace('datetime', 'timedelta'))
  1071. # view as timedelta to allow addition
  1072. f = f.view(otype)
  1073. elif otype.type is np.timedelta64:
  1074. pass
  1075. elif np.issubdtype(otype, np.inexact):
  1076. pass
  1077. else:
  1078. # All other types convert to floating point.
  1079. # First check if f is a numpy integer type; if so, convert f to float64
  1080. # to avoid modular arithmetic when computing the changes in f.
  1081. if np.issubdtype(otype, np.integer):
  1082. f = f.astype(np.float64)
  1083. otype = np.float64
  1084. for axis, ax_dx in zip(axes, dx):
  1085. if f.shape[axis] < edge_order + 1:
  1086. raise ValueError(
  1087. "Shape of array too small to calculate a numerical gradient, "
  1088. "at least (edge_order + 1) elements are required.")
  1089. # result allocation
  1090. out = np.empty_like(f, dtype=otype)
  1091. # spacing for the current axis
  1092. uniform_spacing = np.ndim(ax_dx) == 0
  1093. # Numerical differentiation: 2nd order interior
  1094. slice1[axis] = slice(1, -1)
  1095. slice2[axis] = slice(None, -2)
  1096. slice3[axis] = slice(1, -1)
  1097. slice4[axis] = slice(2, None)
  1098. if uniform_spacing:
  1099. out[tuple(slice1)] = (f[tuple(slice4)] - f[tuple(slice2)]) / (2. * ax_dx)
  1100. else:
  1101. dx1 = ax_dx[0:-1]
  1102. dx2 = ax_dx[1:]
  1103. a = -(dx2) / (dx1 * (dx1 + dx2))
  1104. b = (dx2 - dx1) / (dx1 * dx2)
  1105. c = dx1 / (dx2 * (dx1 + dx2))
  1106. # fix the shape for broadcasting
  1107. shape = np.ones(N, dtype=int)
  1108. shape[axis] = -1
  1109. a = a.reshape(shape)
  1110. b = b.reshape(shape)
  1111. c = c.reshape(shape)
  1112. # 1D equivalent -- out[1:-1] = a * f[:-2] + b * f[1:-1] + c * f[2:]
  1113. out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] \
  1114. + c * f[tuple(slice4)]
  1115. # Numerical differentiation: 1st order edges
  1116. if edge_order == 1:
  1117. slice1[axis] = 0
  1118. slice2[axis] = 1
  1119. slice3[axis] = 0
  1120. dx_0 = ax_dx if uniform_spacing else ax_dx[0]
  1121. # 1D equivalent -- out[0] = (f[1] - f[0]) / (x[1] - x[0])
  1122. out[tuple(slice1)] = (f[tuple(slice2)] - f[tuple(slice3)]) / dx_0
  1123. slice1[axis] = -1
  1124. slice2[axis] = -1
  1125. slice3[axis] = -2
  1126. dx_n = ax_dx if uniform_spacing else ax_dx[-1]
  1127. # 1D equivalent -- out[-1] = (f[-1] - f[-2]) / (x[-1] - x[-2])
  1128. out[tuple(slice1)] = (f[tuple(slice2)] - f[tuple(slice3)]) / dx_n
  1129. # Numerical differentiation: 2nd order edges
  1130. else:
  1131. slice1[axis] = 0
  1132. slice2[axis] = 0
  1133. slice3[axis] = 1
  1134. slice4[axis] = 2
  1135. if uniform_spacing:
  1136. a = -1.5 / ax_dx
  1137. b = 2. / ax_dx
  1138. c = -0.5 / ax_dx
  1139. else:
  1140. dx1 = ax_dx[0]
  1141. dx2 = ax_dx[1]
  1142. a = -(2. * dx1 + dx2) / (dx1 * (dx1 + dx2))
  1143. b = (dx1 + dx2) / (dx1 * dx2)
  1144. c = - dx1 / (dx2 * (dx1 + dx2))
  1145. # 1D equivalent -- out[0] = a * f[0] + b * f[1] + c * f[2]
  1146. out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] \
  1147. + c * f[tuple(slice4)]
  1148. slice1[axis] = -1
  1149. slice2[axis] = -3
  1150. slice3[axis] = -2
  1151. slice4[axis] = -1
  1152. if uniform_spacing:
  1153. a = 0.5 / ax_dx
  1154. b = -2. / ax_dx
  1155. c = 1.5 / ax_dx
  1156. else:
  1157. dx1 = ax_dx[-2]
  1158. dx2 = ax_dx[-1]
  1159. a = (dx2) / (dx1 * (dx1 + dx2))
  1160. b = - (dx2 + dx1) / (dx1 * dx2)
  1161. c = (2. * dx2 + dx1) / (dx2 * (dx1 + dx2))
  1162. # 1D equivalent -- out[-1] = a * f[-3] + b * f[-2] + c * f[-1]
  1163. out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] \
  1164. + c * f[tuple(slice4)]
  1165. outvals.append(out)
  1166. # reset the slice object in this dimension to ":"
  1167. slice1[axis] = slice(None)
  1168. slice2[axis] = slice(None)
  1169. slice3[axis] = slice(None)
  1170. slice4[axis] = slice(None)
  1171. if len_axes == 1:
  1172. return outvals[0]
  1173. return tuple(outvals)
  1174. def _diff_dispatcher(a, n=None, axis=None, prepend=None, append=None):
  1175. return (a, prepend, append)
  1176. @array_function_dispatch(_diff_dispatcher)
  1177. def diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue):
  1178. """
  1179. Calculate the n-th discrete difference along the given axis.
  1180. The first difference is given by ``out[i] = a[i+1] - a[i]`` along
  1181. the given axis, higher differences are calculated by using `diff`
  1182. recursively.
  1183. Parameters
  1184. ----------
  1185. a : array_like
  1186. Input array
  1187. n : int, optional
  1188. The number of times values are differenced. If zero, the input
  1189. is returned as-is.
  1190. axis : int, optional
  1191. The axis along which the difference is taken, default is the
  1192. last axis.
  1193. prepend, append : array_like, optional
  1194. Values to prepend or append to `a` along axis prior to
  1195. performing the difference. Scalar values are expanded to
  1196. arrays with length 1 in the direction of axis and the shape
  1197. of the input array in along all other axes. Otherwise the
  1198. dimension and shape must match `a` except along axis.
  1199. Returns
  1200. -------
  1201. diff : ndarray
  1202. The n-th differences. The shape of the output is the same as `a`
  1203. except along `axis` where the dimension is smaller by `n`. The
  1204. type of the output is the same as the type of the difference
  1205. between any two elements of `a`. This is the same as the type of
  1206. `a` in most cases. A notable exception is `datetime64`, which
  1207. results in a `timedelta64` output array.
  1208. See Also
  1209. --------
  1210. gradient, ediff1d, cumsum
  1211. Notes
  1212. -----
  1213. Type is preserved for boolean arrays, so the result will contain
  1214. `False` when consecutive elements are the same and `True` when they
  1215. differ.
  1216. For unsigned integer arrays, the results will also be unsigned. This
  1217. should not be surprising, as the result is consistent with
  1218. calculating the difference directly:
  1219. >>> u8_arr = np.array([1, 0], dtype=np.uint8)
  1220. >>> np.diff(u8_arr)
  1221. array([255], dtype=uint8)
  1222. >>> u8_arr[1,...] - u8_arr[0,...]
  1223. np.uint8(255)
  1224. If this is not desirable, then the array should be cast to a larger
  1225. integer type first:
  1226. >>> i16_arr = u8_arr.astype(np.int16)
  1227. >>> np.diff(i16_arr)
  1228. array([-1], dtype=int16)
  1229. Examples
  1230. --------
  1231. >>> import numpy as np
  1232. >>> x = np.array([1, 2, 4, 7, 0])
  1233. >>> np.diff(x)
  1234. array([ 1, 2, 3, -7])
  1235. >>> np.diff(x, n=2)
  1236. array([ 1, 1, -10])
  1237. >>> x = np.array([[1, 3, 6, 10], [0, 5, 6, 8]])
  1238. >>> np.diff(x)
  1239. array([[2, 3, 4],
  1240. [5, 1, 2]])
  1241. >>> np.diff(x, axis=0)
  1242. array([[-1, 2, 0, -2]])
  1243. >>> x = np.arange('1066-10-13', '1066-10-16', dtype=np.datetime64)
  1244. >>> np.diff(x)
  1245. array([1, 1], dtype='timedelta64[D]')
  1246. """
  1247. if n == 0:
  1248. return a
  1249. if n < 0:
  1250. raise ValueError(
  1251. "order must be non-negative but got " + repr(n))
  1252. a = asanyarray(a)
  1253. nd = a.ndim
  1254. if nd == 0:
  1255. raise ValueError("diff requires input that is at least one dimensional")
  1256. axis = normalize_axis_index(axis, nd)
  1257. combined = []
  1258. if prepend is not np._NoValue:
  1259. prepend = np.asanyarray(prepend)
  1260. if prepend.ndim == 0:
  1261. shape = list(a.shape)
  1262. shape[axis] = 1
  1263. prepend = np.broadcast_to(prepend, tuple(shape))
  1264. combined.append(prepend)
  1265. combined.append(a)
  1266. if append is not np._NoValue:
  1267. append = np.asanyarray(append)
  1268. if append.ndim == 0:
  1269. shape = list(a.shape)
  1270. shape[axis] = 1
  1271. append = np.broadcast_to(append, tuple(shape))
  1272. combined.append(append)
  1273. if len(combined) > 1:
  1274. a = np.concatenate(combined, axis)
  1275. slice1 = [slice(None)] * nd
  1276. slice2 = [slice(None)] * nd
  1277. slice1[axis] = slice(1, None)
  1278. slice2[axis] = slice(None, -1)
  1279. slice1 = tuple(slice1)
  1280. slice2 = tuple(slice2)
  1281. op = not_equal if a.dtype == np.bool else subtract
  1282. for _ in range(n):
  1283. a = op(a[slice1], a[slice2])
  1284. return a
  1285. def _interp_dispatcher(x, xp, fp, left=None, right=None, period=None):
  1286. return (x, xp, fp)
  1287. @array_function_dispatch(_interp_dispatcher)
  1288. def interp(x, xp, fp, left=None, right=None, period=None):
  1289. """
  1290. One-dimensional linear interpolation for monotonically increasing sample points.
  1291. Returns the one-dimensional piecewise linear interpolant to a function
  1292. with given discrete data points (`xp`, `fp`), evaluated at `x`.
  1293. Parameters
  1294. ----------
  1295. x : array_like
  1296. The x-coordinates at which to evaluate the interpolated values.
  1297. xp : 1-D sequence of floats
  1298. The x-coordinates of the data points, must be increasing if argument
  1299. `period` is not specified. Otherwise, `xp` is internally sorted after
  1300. normalizing the periodic boundaries with ``xp = xp % period``.
  1301. fp : 1-D sequence of float or complex
  1302. The y-coordinates of the data points, same length as `xp`.
  1303. left : optional float or complex corresponding to fp
  1304. Value to return for `x < xp[0]`, default is `fp[0]`.
  1305. right : optional float or complex corresponding to fp
  1306. Value to return for `x > xp[-1]`, default is `fp[-1]`.
  1307. period : None or float, optional
  1308. A period for the x-coordinates. This parameter allows the proper
  1309. interpolation of angular x-coordinates. Parameters `left` and `right`
  1310. are ignored if `period` is specified.
  1311. Returns
  1312. -------
  1313. y : float or complex (corresponding to fp) or ndarray
  1314. The interpolated values, same shape as `x`.
  1315. Raises
  1316. ------
  1317. ValueError
  1318. If `xp` and `fp` have different length
  1319. If `xp` or `fp` are not 1-D sequences
  1320. If `period == 0`
  1321. See Also
  1322. --------
  1323. scipy.interpolate
  1324. Warnings
  1325. --------
  1326. The x-coordinate sequence is expected to be increasing, but this is not
  1327. explicitly enforced. However, if the sequence `xp` is non-increasing,
  1328. interpolation results are meaningless.
  1329. Note that, since NaN is unsortable, `xp` also cannot contain NaNs.
  1330. A simple check for `xp` being strictly increasing is::
  1331. np.all(np.diff(xp) > 0)
  1332. Examples
  1333. --------
  1334. >>> import numpy as np
  1335. >>> xp = [1, 2, 3]
  1336. >>> fp = [3, 2, 0]
  1337. >>> np.interp(2.5, xp, fp)
  1338. 1.0
  1339. >>> np.interp([0, 1, 1.5, 2.72, 3.14], xp, fp)
  1340. array([3. , 3. , 2.5 , 0.56, 0. ])
  1341. >>> UNDEF = -99.0
  1342. >>> np.interp(3.14, xp, fp, right=UNDEF)
  1343. -99.0
  1344. Plot an interpolant to the sine function:
  1345. >>> x = np.linspace(0, 2*np.pi, 10)
  1346. >>> y = np.sin(x)
  1347. >>> xvals = np.linspace(0, 2*np.pi, 50)
  1348. >>> yinterp = np.interp(xvals, x, y)
  1349. >>> import matplotlib.pyplot as plt
  1350. >>> plt.plot(x, y, 'o')
  1351. [<matplotlib.lines.Line2D object at 0x...>]
  1352. >>> plt.plot(xvals, yinterp, '-x')
  1353. [<matplotlib.lines.Line2D object at 0x...>]
  1354. >>> plt.show()
  1355. Interpolation with periodic x-coordinates:
  1356. >>> x = [-180, -170, -185, 185, -10, -5, 0, 365]
  1357. >>> xp = [190, -190, 350, -350]
  1358. >>> fp = [5, 10, 3, 4]
  1359. >>> np.interp(x, xp, fp, period=360)
  1360. array([7.5 , 5. , 8.75, 6.25, 3. , 3.25, 3.5 , 3.75])
  1361. Complex interpolation:
  1362. >>> x = [1.5, 4.0]
  1363. >>> xp = [2,3,5]
  1364. >>> fp = [1.0j, 0, 2+3j]
  1365. >>> np.interp(x, xp, fp)
  1366. array([0.+1.j , 1.+1.5j])
  1367. """
  1368. fp = np.asarray(fp)
  1369. if np.iscomplexobj(fp):
  1370. interp_func = compiled_interp_complex
  1371. input_dtype = np.complex128
  1372. else:
  1373. interp_func = compiled_interp
  1374. input_dtype = np.float64
  1375. if period is not None:
  1376. if period == 0:
  1377. raise ValueError("period must be a non-zero value")
  1378. period = abs(period)
  1379. left = None
  1380. right = None
  1381. x = np.asarray(x, dtype=np.float64)
  1382. xp = np.asarray(xp, dtype=np.float64)
  1383. fp = np.asarray(fp, dtype=input_dtype)
  1384. if xp.ndim != 1 or fp.ndim != 1:
  1385. raise ValueError("Data points must be 1-D sequences")
  1386. if xp.shape[0] != fp.shape[0]:
  1387. raise ValueError("fp and xp are not of the same length")
  1388. # normalizing periodic boundaries
  1389. x = x % period
  1390. xp = xp % period
  1391. asort_xp = np.argsort(xp)
  1392. xp = xp[asort_xp]
  1393. fp = fp[asort_xp]
  1394. xp = np.concatenate((xp[-1:] - period, xp, xp[0:1] + period))
  1395. fp = np.concatenate((fp[-1:], fp, fp[0:1]))
  1396. return interp_func(x, xp, fp, left, right)
  1397. def _angle_dispatcher(z, deg=None):
  1398. return (z,)
  1399. @array_function_dispatch(_angle_dispatcher)
  1400. def angle(z, deg=False):
  1401. """
  1402. Return the angle of the complex argument.
  1403. Parameters
  1404. ----------
  1405. z : array_like
  1406. A complex number or sequence of complex numbers.
  1407. deg : bool, optional
  1408. Return angle in degrees if True, radians if False (default).
  1409. Returns
  1410. -------
  1411. angle : ndarray or scalar
  1412. The counterclockwise angle from the positive real axis on the complex
  1413. plane in the range ``(-pi, pi]``, with dtype as numpy.float64.
  1414. See Also
  1415. --------
  1416. arctan2
  1417. absolute
  1418. Notes
  1419. -----
  1420. This function passes the imaginary and real parts of the argument to
  1421. `arctan2` to compute the result; consequently, it follows the convention
  1422. of `arctan2` when the magnitude of the argument is zero. See example.
  1423. Examples
  1424. --------
  1425. >>> import numpy as np
  1426. >>> np.angle([1.0, 1.0j, 1+1j]) # in radians
  1427. array([ 0. , 1.57079633, 0.78539816]) # may vary
  1428. >>> np.angle(1+1j, deg=True) # in degrees
  1429. 45.0
  1430. >>> np.angle([0., -0., complex(0., -0.), complex(-0., -0.)]) # convention
  1431. array([ 0. , 3.14159265, -0. , -3.14159265])
  1432. """
  1433. z = asanyarray(z)
  1434. if issubclass(z.dtype.type, _nx.complexfloating):
  1435. zimag = z.imag
  1436. zreal = z.real
  1437. else:
  1438. zimag = 0
  1439. zreal = z
  1440. a = arctan2(zimag, zreal)
  1441. if deg:
  1442. a *= 180 / pi
  1443. return a
  1444. def _unwrap_dispatcher(p, discont=None, axis=None, *, period=None):
  1445. return (p,)
  1446. @array_function_dispatch(_unwrap_dispatcher)
  1447. def unwrap(p, discont=None, axis=-1, *, period=2 * pi):
  1448. r"""
  1449. Unwrap by taking the complement of large deltas with respect to the period.
  1450. This unwraps a signal `p` by changing elements which have an absolute
  1451. difference from their predecessor of more than ``max(discont, period/2)``
  1452. to their `period`-complementary values.
  1453. For the default case where `period` is :math:`2\pi` and `discont` is
  1454. :math:`\pi`, this unwraps a radian phase `p` such that adjacent differences
  1455. are never greater than :math:`\pi` by adding :math:`2k\pi` for some
  1456. integer :math:`k`.
  1457. Parameters
  1458. ----------
  1459. p : array_like
  1460. Input array.
  1461. discont : float, optional
  1462. Maximum discontinuity between values, default is ``period/2``.
  1463. Values below ``period/2`` are treated as if they were ``period/2``.
  1464. To have an effect different from the default, `discont` should be
  1465. larger than ``period/2``.
  1466. axis : int, optional
  1467. Axis along which unwrap will operate, default is the last axis.
  1468. period : float, optional
  1469. Size of the range over which the input wraps. By default, it is
  1470. ``2 pi``.
  1471. .. versionadded:: 1.21.0
  1472. Returns
  1473. -------
  1474. out : ndarray
  1475. Output array.
  1476. See Also
  1477. --------
  1478. rad2deg, deg2rad
  1479. Notes
  1480. -----
  1481. If the discontinuity in `p` is smaller than ``period/2``,
  1482. but larger than `discont`, no unwrapping is done because taking
  1483. the complement would only make the discontinuity larger.
  1484. Examples
  1485. --------
  1486. >>> import numpy as np
  1487. >>> phase = np.linspace(0, np.pi, num=5)
  1488. >>> phase[3:] += np.pi
  1489. >>> phase
  1490. array([ 0. , 0.78539816, 1.57079633, 5.49778714, 6.28318531]) # may vary
  1491. >>> np.unwrap(phase)
  1492. array([ 0. , 0.78539816, 1.57079633, -0.78539816, 0. ]) # may vary
  1493. >>> np.unwrap([0, 1, 2, -1, 0], period=4)
  1494. array([0, 1, 2, 3, 4])
  1495. >>> np.unwrap([ 1, 2, 3, 4, 5, 6, 1, 2, 3], period=6)
  1496. array([1, 2, 3, 4, 5, 6, 7, 8, 9])
  1497. >>> np.unwrap([2, 3, 4, 5, 2, 3, 4, 5], period=4)
  1498. array([2, 3, 4, 5, 6, 7, 8, 9])
  1499. >>> phase_deg = np.mod(np.linspace(0 ,720, 19), 360) - 180
  1500. >>> np.unwrap(phase_deg, period=360)
  1501. array([-180., -140., -100., -60., -20., 20., 60., 100., 140.,
  1502. 180., 220., 260., 300., 340., 380., 420., 460., 500.,
  1503. 540.])
  1504. This example plots the unwrapping of the wrapped input signal `w`.
  1505. First generate `w`, then apply `unwrap` to get `u`.
  1506. >>> t = np.linspace(0, 25, 801)
  1507. >>> w = np.mod(1.5 * np.sin(1.1 * t + 0.26) * (1 - t / 6 + (t / 23) ** 3), 2.0) - 1
  1508. >>> u = np.unwrap(w, period=2.0)
  1509. Plot `w` and `u`.
  1510. >>> import matplotlib.pyplot as plt
  1511. >>> plt.plot(t, w, label='w (a signal wrapped to [-1, 1])')
  1512. >>> plt.plot(t, u, linewidth=2.5, alpha=0.5, label='unwrap(w, period=2)')
  1513. >>> plt.xlabel('t')
  1514. >>> plt.grid(alpha=0.6)
  1515. >>> plt.legend(framealpha=1, shadow=True)
  1516. >>> plt.show()
  1517. """
  1518. p = asarray(p)
  1519. nd = p.ndim
  1520. dd = diff(p, axis=axis)
  1521. if discont is None:
  1522. discont = period / 2
  1523. slice1 = [slice(None, None)] * nd # full slices
  1524. slice1[axis] = slice(1, None)
  1525. slice1 = tuple(slice1)
  1526. dtype = np.result_type(dd, period)
  1527. if _nx.issubdtype(dtype, _nx.integer):
  1528. interval_high, rem = divmod(period, 2)
  1529. boundary_ambiguous = rem == 0
  1530. else:
  1531. interval_high = period / 2
  1532. boundary_ambiguous = True
  1533. interval_low = -interval_high
  1534. ddmod = mod(dd - interval_low, period) + interval_low
  1535. if boundary_ambiguous:
  1536. # for `mask = (abs(dd) == period/2)`, the above line made
  1537. # `ddmod[mask] == -period/2`. correct these such that
  1538. # `ddmod[mask] == sign(dd[mask])*period/2`.
  1539. _nx.copyto(ddmod, interval_high,
  1540. where=(ddmod == interval_low) & (dd > 0))
  1541. ph_correct = ddmod - dd
  1542. _nx.copyto(ph_correct, 0, where=abs(dd) < discont)
  1543. up = array(p, copy=True, dtype=dtype)
  1544. up[slice1] = p[slice1] + ph_correct.cumsum(axis)
  1545. return up
  1546. def _sort_complex(a):
  1547. return (a,)
  1548. @array_function_dispatch(_sort_complex)
  1549. def sort_complex(a):
  1550. """
  1551. Sort a complex array using the real part first, then the imaginary part.
  1552. Parameters
  1553. ----------
  1554. a : array_like
  1555. Input array
  1556. Returns
  1557. -------
  1558. out : complex ndarray
  1559. Always returns a sorted complex array.
  1560. Examples
  1561. --------
  1562. >>> import numpy as np
  1563. >>> np.sort_complex([5, 3, 6, 2, 1])
  1564. array([1.+0.j, 2.+0.j, 3.+0.j, 5.+0.j, 6.+0.j])
  1565. >>> np.sort_complex([1 + 2j, 2 - 1j, 3 - 2j, 3 - 3j, 3 + 5j])
  1566. array([1.+2.j, 2.-1.j, 3.-3.j, 3.-2.j, 3.+5.j])
  1567. """
  1568. b = array(a, copy=True)
  1569. b.sort()
  1570. if not issubclass(b.dtype.type, _nx.complexfloating):
  1571. if b.dtype.char in 'bhBH':
  1572. return b.astype('F')
  1573. elif b.dtype.char == 'g':
  1574. return b.astype('G')
  1575. else:
  1576. return b.astype('D')
  1577. else:
  1578. return b
  1579. def _arg_trim_zeros(filt):
  1580. """Return indices of the first and last non-zero element.
  1581. Parameters
  1582. ----------
  1583. filt : array_like
  1584. Input array.
  1585. Returns
  1586. -------
  1587. start, stop : ndarray
  1588. Two arrays containing the indices of the first and last non-zero
  1589. element in each dimension.
  1590. See also
  1591. --------
  1592. trim_zeros
  1593. Examples
  1594. --------
  1595. >>> import numpy as np
  1596. >>> _arg_trim_zeros(np.array([0, 0, 1, 1, 0]))
  1597. (array([2]), array([3]))
  1598. """
  1599. nonzero = (
  1600. np.argwhere(filt)
  1601. if filt.dtype != np.object_
  1602. # Historically, `trim_zeros` treats `None` in an object array
  1603. # as non-zero while argwhere doesn't, account for that
  1604. else np.argwhere(filt != 0)
  1605. )
  1606. if nonzero.size == 0:
  1607. start = stop = np.array([], dtype=np.intp)
  1608. else:
  1609. start = nonzero.min(axis=0)
  1610. stop = nonzero.max(axis=0)
  1611. return start, stop
  1612. def _trim_zeros(filt, trim=None, axis=None):
  1613. return (filt,)
  1614. @array_function_dispatch(_trim_zeros)
  1615. def trim_zeros(filt, trim='fb', axis=None):
  1616. """Remove values along a dimension which are zero along all other.
  1617. Parameters
  1618. ----------
  1619. filt : array_like
  1620. Input array.
  1621. trim : {"fb", "f", "b"}, optional
  1622. A string with 'f' representing trim from front and 'b' to trim from
  1623. back. By default, zeros are trimmed on both sides.
  1624. Front and back refer to the edges of a dimension, with "front" referring
  1625. to the side with the lowest index 0, and "back" referring to the highest
  1626. index (or index -1).
  1627. axis : int or sequence, optional
  1628. If None, `filt` is cropped such that the smallest bounding box is
  1629. returned that still contains all values which are not zero.
  1630. If an axis is specified, `filt` will be sliced in that dimension only
  1631. on the sides specified by `trim`. The remaining area will be the
  1632. smallest that still contains all values wich are not zero.
  1633. .. versionadded:: 2.2.0
  1634. Returns
  1635. -------
  1636. trimmed : ndarray or sequence
  1637. The result of trimming the input. The number of dimensions and the
  1638. input data type are preserved.
  1639. Notes
  1640. -----
  1641. For all-zero arrays, the first axis is trimmed first.
  1642. Examples
  1643. --------
  1644. >>> import numpy as np
  1645. >>> a = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))
  1646. >>> np.trim_zeros(a)
  1647. array([1, 2, 3, 0, 2, 1])
  1648. >>> np.trim_zeros(a, trim='b')
  1649. array([0, 0, 0, ..., 0, 2, 1])
  1650. Multiple dimensions are supported.
  1651. >>> b = np.array([[0, 0, 2, 3, 0, 0],
  1652. ... [0, 1, 0, 3, 0, 0],
  1653. ... [0, 0, 0, 0, 0, 0]])
  1654. >>> np.trim_zeros(b)
  1655. array([[0, 2, 3],
  1656. [1, 0, 3]])
  1657. >>> np.trim_zeros(b, axis=-1)
  1658. array([[0, 2, 3],
  1659. [1, 0, 3],
  1660. [0, 0, 0]])
  1661. The input data type is preserved, list/tuple in means list/tuple out.
  1662. >>> np.trim_zeros([0, 1, 2, 0])
  1663. [1, 2]
  1664. """
  1665. filt_ = np.asarray(filt)
  1666. trim = trim.lower()
  1667. if trim not in {"fb", "bf", "f", "b"}:
  1668. raise ValueError(f"unexpected character(s) in `trim`: {trim!r}")
  1669. if axis is None:
  1670. axis_tuple = tuple(range(filt_.ndim))
  1671. else:
  1672. axis_tuple = _nx.normalize_axis_tuple(axis, filt_.ndim, argname="axis")
  1673. if not axis_tuple:
  1674. # No trimming requested -> return input unmodified.
  1675. return filt
  1676. start, stop = _arg_trim_zeros(filt_)
  1677. stop += 1 # Adjust for slicing
  1678. if start.size == 0:
  1679. # filt is all-zero -> assign same values to start and stop so that
  1680. # resulting slice will be empty
  1681. start = stop = np.zeros(filt_.ndim, dtype=np.intp)
  1682. else:
  1683. if 'f' not in trim:
  1684. start = (None,) * filt_.ndim
  1685. if 'b' not in trim:
  1686. stop = (None,) * filt_.ndim
  1687. sl = tuple(slice(start[ax], stop[ax]) if ax in axis_tuple else slice(None)
  1688. for ax in range(filt_.ndim))
  1689. if len(sl) == 1:
  1690. # filt is 1D -> avoid multi-dimensional slicing to preserve
  1691. # non-array input types
  1692. return filt[sl[0]]
  1693. return filt[sl]
  1694. def _extract_dispatcher(condition, arr):
  1695. return (condition, arr)
  1696. @array_function_dispatch(_extract_dispatcher)
  1697. def extract(condition, arr):
  1698. """
  1699. Return the elements of an array that satisfy some condition.
  1700. This is equivalent to ``np.compress(ravel(condition), ravel(arr))``. If
  1701. `condition` is boolean ``np.extract`` is equivalent to ``arr[condition]``.
  1702. Note that `place` does the exact opposite of `extract`.
  1703. Parameters
  1704. ----------
  1705. condition : array_like
  1706. An array whose nonzero or True entries indicate the elements of `arr`
  1707. to extract.
  1708. arr : array_like
  1709. Input array of the same size as `condition`.
  1710. Returns
  1711. -------
  1712. extract : ndarray
  1713. Rank 1 array of values from `arr` where `condition` is True.
  1714. See Also
  1715. --------
  1716. take, put, copyto, compress, place
  1717. Examples
  1718. --------
  1719. >>> import numpy as np
  1720. >>> arr = np.arange(12).reshape((3, 4))
  1721. >>> arr
  1722. array([[ 0, 1, 2, 3],
  1723. [ 4, 5, 6, 7],
  1724. [ 8, 9, 10, 11]])
  1725. >>> condition = np.mod(arr, 3)==0
  1726. >>> condition
  1727. array([[ True, False, False, True],
  1728. [False, False, True, False],
  1729. [False, True, False, False]])
  1730. >>> np.extract(condition, arr)
  1731. array([0, 3, 6, 9])
  1732. If `condition` is boolean:
  1733. >>> arr[condition]
  1734. array([0, 3, 6, 9])
  1735. """
  1736. return _nx.take(ravel(arr), nonzero(ravel(condition))[0])
  1737. def _place_dispatcher(arr, mask, vals):
  1738. return (arr, mask, vals)
  1739. @array_function_dispatch(_place_dispatcher)
  1740. def place(arr, mask, vals):
  1741. """
  1742. Change elements of an array based on conditional and input values.
  1743. Similar to ``np.copyto(arr, vals, where=mask)``, the difference is that
  1744. `place` uses the first N elements of `vals`, where N is the number of
  1745. True values in `mask`, while `copyto` uses the elements where `mask`
  1746. is True.
  1747. Note that `extract` does the exact opposite of `place`.
  1748. Parameters
  1749. ----------
  1750. arr : ndarray
  1751. Array to put data into.
  1752. mask : array_like
  1753. Boolean mask array. Must have the same size as `a`.
  1754. vals : 1-D sequence
  1755. Values to put into `a`. Only the first N elements are used, where
  1756. N is the number of True values in `mask`. If `vals` is smaller
  1757. than N, it will be repeated, and if elements of `a` are to be masked,
  1758. this sequence must be non-empty.
  1759. See Also
  1760. --------
  1761. copyto, put, take, extract
  1762. Examples
  1763. --------
  1764. >>> import numpy as np
  1765. >>> arr = np.arange(6).reshape(2, 3)
  1766. >>> np.place(arr, arr>2, [44, 55])
  1767. >>> arr
  1768. array([[ 0, 1, 2],
  1769. [44, 55, 44]])
  1770. """
  1771. return _place(arr, mask, vals)
  1772. # See https://docs.scipy.org/doc/numpy/reference/c-api.generalized-ufuncs.html
  1773. _DIMENSION_NAME = r'\w+'
  1774. _CORE_DIMENSION_LIST = f'(?:{_DIMENSION_NAME}(?:,{_DIMENSION_NAME})*)?'
  1775. _ARGUMENT = fr'\({_CORE_DIMENSION_LIST}\)'
  1776. _ARGUMENT_LIST = f'{_ARGUMENT}(?:,{_ARGUMENT})*'
  1777. _SIGNATURE = f'^{_ARGUMENT_LIST}->{_ARGUMENT_LIST}$'
  1778. def _parse_gufunc_signature(signature):
  1779. """
  1780. Parse string signatures for a generalized universal function.
  1781. Arguments
  1782. ---------
  1783. signature : string
  1784. Generalized universal function signature, e.g., ``(m,n),(n,p)->(m,p)``
  1785. for ``np.matmul``.
  1786. Returns
  1787. -------
  1788. Tuple of input and output core dimensions parsed from the signature, each
  1789. of the form List[Tuple[str, ...]].
  1790. """
  1791. signature = re.sub(r'\s+', '', signature)
  1792. if not re.match(_SIGNATURE, signature):
  1793. raise ValueError(
  1794. f'not a valid gufunc signature: {signature}')
  1795. return tuple([tuple(re.findall(_DIMENSION_NAME, arg))
  1796. for arg in re.findall(_ARGUMENT, arg_list)]
  1797. for arg_list in signature.split('->'))
  1798. def _update_dim_sizes(dim_sizes, arg, core_dims):
  1799. """
  1800. Incrementally check and update core dimension sizes for a single argument.
  1801. Arguments
  1802. ---------
  1803. dim_sizes : Dict[str, int]
  1804. Sizes of existing core dimensions. Will be updated in-place.
  1805. arg : ndarray
  1806. Argument to examine.
  1807. core_dims : Tuple[str, ...]
  1808. Core dimensions for this argument.
  1809. """
  1810. if not core_dims:
  1811. return
  1812. num_core_dims = len(core_dims)
  1813. if arg.ndim < num_core_dims:
  1814. raise ValueError(
  1815. '%d-dimensional argument does not have enough '
  1816. 'dimensions for all core dimensions %r'
  1817. % (arg.ndim, core_dims))
  1818. core_shape = arg.shape[-num_core_dims:]
  1819. for dim, size in zip(core_dims, core_shape):
  1820. if dim in dim_sizes:
  1821. if size != dim_sizes[dim]:
  1822. raise ValueError(
  1823. 'inconsistent size for core dimension %r: %r vs %r'
  1824. % (dim, size, dim_sizes[dim]))
  1825. else:
  1826. dim_sizes[dim] = size
  1827. def _parse_input_dimensions(args, input_core_dims):
  1828. """
  1829. Parse broadcast and core dimensions for vectorize with a signature.
  1830. Arguments
  1831. ---------
  1832. args : Tuple[ndarray, ...]
  1833. Tuple of input arguments to examine.
  1834. input_core_dims : List[Tuple[str, ...]]
  1835. List of core dimensions corresponding to each input.
  1836. Returns
  1837. -------
  1838. broadcast_shape : Tuple[int, ...]
  1839. Common shape to broadcast all non-core dimensions to.
  1840. dim_sizes : Dict[str, int]
  1841. Common sizes for named core dimensions.
  1842. """
  1843. broadcast_args = []
  1844. dim_sizes = {}
  1845. for arg, core_dims in zip(args, input_core_dims):
  1846. _update_dim_sizes(dim_sizes, arg, core_dims)
  1847. ndim = arg.ndim - len(core_dims)
  1848. dummy_array = np.lib.stride_tricks.as_strided(0, arg.shape[:ndim])
  1849. broadcast_args.append(dummy_array)
  1850. broadcast_shape = np.lib._stride_tricks_impl._broadcast_shape(
  1851. *broadcast_args
  1852. )
  1853. return broadcast_shape, dim_sizes
  1854. def _calculate_shapes(broadcast_shape, dim_sizes, list_of_core_dims):
  1855. """Helper for calculating broadcast shapes with core dimensions."""
  1856. return [broadcast_shape + tuple(dim_sizes[dim] for dim in core_dims)
  1857. for core_dims in list_of_core_dims]
  1858. def _create_arrays(broadcast_shape, dim_sizes, list_of_core_dims, dtypes,
  1859. results=None):
  1860. """Helper for creating output arrays in vectorize."""
  1861. shapes = _calculate_shapes(broadcast_shape, dim_sizes, list_of_core_dims)
  1862. if dtypes is None:
  1863. dtypes = [None] * len(shapes)
  1864. if results is None:
  1865. arrays = tuple(np.empty(shape=shape, dtype=dtype)
  1866. for shape, dtype in zip(shapes, dtypes))
  1867. else:
  1868. arrays = tuple(np.empty_like(result, shape=shape, dtype=dtype)
  1869. for result, shape, dtype
  1870. in zip(results, shapes, dtypes))
  1871. return arrays
  1872. def _get_vectorize_dtype(dtype):
  1873. if dtype.char in "SU":
  1874. return dtype.char
  1875. return dtype
  1876. @set_module('numpy')
  1877. class vectorize:
  1878. """
  1879. vectorize(pyfunc=np._NoValue, otypes=None, doc=None, excluded=None,
  1880. cache=False, signature=None)
  1881. Returns an object that acts like pyfunc, but takes arrays as input.
  1882. Define a vectorized function which takes a nested sequence of objects or
  1883. numpy arrays as inputs and returns a single numpy array or a tuple of numpy
  1884. arrays. The vectorized function evaluates `pyfunc` over successive tuples
  1885. of the input arrays like the python map function, except it uses the
  1886. broadcasting rules of numpy.
  1887. The data type of the output of `vectorized` is determined by calling
  1888. the function with the first element of the input. This can be avoided
  1889. by specifying the `otypes` argument.
  1890. Parameters
  1891. ----------
  1892. pyfunc : callable, optional
  1893. A python function or method.
  1894. Can be omitted to produce a decorator with keyword arguments.
  1895. otypes : str or list of dtypes, optional
  1896. The output data type. It must be specified as either a string of
  1897. typecode characters or a list of data type specifiers. There should
  1898. be one data type specifier for each output.
  1899. doc : str, optional
  1900. The docstring for the function. If None, the docstring will be the
  1901. ``pyfunc.__doc__``.
  1902. excluded : set, optional
  1903. Set of strings or integers representing the positional or keyword
  1904. arguments for which the function will not be vectorized. These will be
  1905. passed directly to `pyfunc` unmodified.
  1906. cache : bool, optional
  1907. If neither `otypes` nor `signature` are provided, and `cache` is ``True``, then
  1908. cache the number of outputs.
  1909. signature : string, optional
  1910. Generalized universal function signature, e.g., ``(m,n),(n)->(m)`` for
  1911. vectorized matrix-vector multiplication. If provided, ``pyfunc`` will
  1912. be called with (and expected to return) arrays with shapes given by the
  1913. size of corresponding core dimensions. By default, ``pyfunc`` is
  1914. assumed to take scalars as input and output.
  1915. Returns
  1916. -------
  1917. out : callable
  1918. A vectorized function if ``pyfunc`` was provided,
  1919. a decorator otherwise.
  1920. See Also
  1921. --------
  1922. frompyfunc : Takes an arbitrary Python function and returns a ufunc
  1923. Notes
  1924. -----
  1925. The `vectorize` function is provided primarily for convenience, not for
  1926. performance. The implementation is essentially a for loop.
  1927. If neither `otypes` nor `signature` are specified, then a call to the function with
  1928. the first argument will be used to determine the number of outputs. The results of
  1929. this call will be cached if `cache` is `True` to prevent calling the function
  1930. twice. However, to implement the cache, the original function must be wrapped
  1931. which will slow down subsequent calls, so only do this if your function is
  1932. expensive.
  1933. The new keyword argument interface and `excluded` argument support
  1934. further degrades performance.
  1935. References
  1936. ----------
  1937. .. [1] :doc:`/reference/c-api/generalized-ufuncs`
  1938. Examples
  1939. --------
  1940. >>> import numpy as np
  1941. >>> def myfunc(a, b):
  1942. ... "Return a-b if a>b, otherwise return a+b"
  1943. ... if a > b:
  1944. ... return a - b
  1945. ... else:
  1946. ... return a + b
  1947. >>> vfunc = np.vectorize(myfunc)
  1948. >>> vfunc([1, 2, 3, 4], 2)
  1949. array([3, 4, 1, 2])
  1950. The docstring is taken from the input function to `vectorize` unless it
  1951. is specified:
  1952. >>> vfunc.__doc__
  1953. 'Return a-b if a>b, otherwise return a+b'
  1954. >>> vfunc = np.vectorize(myfunc, doc='Vectorized `myfunc`')
  1955. >>> vfunc.__doc__
  1956. 'Vectorized `myfunc`'
  1957. The output type is determined by evaluating the first element of the input,
  1958. unless it is specified:
  1959. >>> out = vfunc([1, 2, 3, 4], 2)
  1960. >>> type(out[0])
  1961. <class 'numpy.int64'>
  1962. >>> vfunc = np.vectorize(myfunc, otypes=[float])
  1963. >>> out = vfunc([1, 2, 3, 4], 2)
  1964. >>> type(out[0])
  1965. <class 'numpy.float64'>
  1966. The `excluded` argument can be used to prevent vectorizing over certain
  1967. arguments. This can be useful for array-like arguments of a fixed length
  1968. such as the coefficients for a polynomial as in `polyval`:
  1969. >>> def mypolyval(p, x):
  1970. ... _p = list(p)
  1971. ... res = _p.pop(0)
  1972. ... while _p:
  1973. ... res = res*x + _p.pop(0)
  1974. ... return res
  1975. Here, we exclude the zeroth argument from vectorization whether it is
  1976. passed by position or keyword.
  1977. >>> vpolyval = np.vectorize(mypolyval, excluded={0, 'p'})
  1978. >>> vpolyval([1, 2, 3], x=[0, 1])
  1979. array([3, 6])
  1980. >>> vpolyval(p=[1, 2, 3], x=[0, 1])
  1981. array([3, 6])
  1982. The `signature` argument allows for vectorizing functions that act on
  1983. non-scalar arrays of fixed length. For example, you can use it for a
  1984. vectorized calculation of Pearson correlation coefficient and its p-value:
  1985. >>> import scipy.stats
  1986. >>> pearsonr = np.vectorize(scipy.stats.pearsonr,
  1987. ... signature='(n),(n)->(),()')
  1988. >>> pearsonr([[0, 1, 2, 3]], [[1, 2, 3, 4], [4, 3, 2, 1]])
  1989. (array([ 1., -1.]), array([ 0., 0.]))
  1990. Or for a vectorized convolution:
  1991. >>> convolve = np.vectorize(np.convolve, signature='(n),(m)->(k)')
  1992. >>> convolve(np.eye(4), [1, 2, 1])
  1993. array([[1., 2., 1., 0., 0., 0.],
  1994. [0., 1., 2., 1., 0., 0.],
  1995. [0., 0., 1., 2., 1., 0.],
  1996. [0., 0., 0., 1., 2., 1.]])
  1997. Decorator syntax is supported. The decorator can be called as
  1998. a function to provide keyword arguments:
  1999. >>> @np.vectorize
  2000. ... def identity(x):
  2001. ... return x
  2002. ...
  2003. >>> identity([0, 1, 2])
  2004. array([0, 1, 2])
  2005. >>> @np.vectorize(otypes=[float])
  2006. ... def as_float(x):
  2007. ... return x
  2008. ...
  2009. >>> as_float([0, 1, 2])
  2010. array([0., 1., 2.])
  2011. """
  2012. def __init__(self, pyfunc=np._NoValue, otypes=None, doc=None,
  2013. excluded=None, cache=False, signature=None):
  2014. if (pyfunc != np._NoValue) and (not callable(pyfunc)):
  2015. # Splitting the error message to keep
  2016. # the length below 79 characters.
  2017. part1 = "When used as a decorator, "
  2018. part2 = "only accepts keyword arguments."
  2019. raise TypeError(part1 + part2)
  2020. self.pyfunc = pyfunc
  2021. self.cache = cache
  2022. self.signature = signature
  2023. if pyfunc != np._NoValue and hasattr(pyfunc, '__name__'):
  2024. self.__name__ = pyfunc.__name__
  2025. self._ufunc = {} # Caching to improve default performance
  2026. self._doc = None
  2027. self.__doc__ = doc
  2028. if doc is None and hasattr(pyfunc, '__doc__'):
  2029. self.__doc__ = pyfunc.__doc__
  2030. else:
  2031. self._doc = doc
  2032. if isinstance(otypes, str):
  2033. for char in otypes:
  2034. if char not in typecodes['All']:
  2035. raise ValueError(f"Invalid otype specified: {char}")
  2036. elif iterable(otypes):
  2037. otypes = [_get_vectorize_dtype(_nx.dtype(x)) for x in otypes]
  2038. elif otypes is not None:
  2039. raise ValueError("Invalid otype specification")
  2040. self.otypes = otypes
  2041. # Excluded variable support
  2042. if excluded is None:
  2043. excluded = set()
  2044. self.excluded = set(excluded)
  2045. if signature is not None:
  2046. self._in_and_out_core_dims = _parse_gufunc_signature(signature)
  2047. else:
  2048. self._in_and_out_core_dims = None
  2049. def _init_stage_2(self, pyfunc, *args, **kwargs):
  2050. self.__name__ = pyfunc.__name__
  2051. self.pyfunc = pyfunc
  2052. if self._doc is None:
  2053. self.__doc__ = pyfunc.__doc__
  2054. else:
  2055. self.__doc__ = self._doc
  2056. def _call_as_normal(self, *args, **kwargs):
  2057. """
  2058. Return arrays with the results of `pyfunc` broadcast (vectorized) over
  2059. `args` and `kwargs` not in `excluded`.
  2060. """
  2061. excluded = self.excluded
  2062. if not kwargs and not excluded:
  2063. func = self.pyfunc
  2064. vargs = args
  2065. else:
  2066. # The wrapper accepts only positional arguments: we use `names` and
  2067. # `inds` to mutate `the_args` and `kwargs` to pass to the original
  2068. # function.
  2069. nargs = len(args)
  2070. names = [_n for _n in kwargs if _n not in excluded]
  2071. inds = [_i for _i in range(nargs) if _i not in excluded]
  2072. the_args = list(args)
  2073. def func(*vargs):
  2074. for _n, _i in enumerate(inds):
  2075. the_args[_i] = vargs[_n]
  2076. kwargs.update(zip(names, vargs[len(inds):]))
  2077. return self.pyfunc(*the_args, **kwargs)
  2078. vargs = [args[_i] for _i in inds]
  2079. vargs.extend([kwargs[_n] for _n in names])
  2080. return self._vectorize_call(func=func, args=vargs)
  2081. def __call__(self, *args, **kwargs):
  2082. if self.pyfunc is np._NoValue:
  2083. self._init_stage_2(*args, **kwargs)
  2084. return self
  2085. return self._call_as_normal(*args, **kwargs)
  2086. def _get_ufunc_and_otypes(self, func, args):
  2087. """Return (ufunc, otypes)."""
  2088. # frompyfunc will fail if args is empty
  2089. if not args:
  2090. raise ValueError('args can not be empty')
  2091. if self.otypes is not None:
  2092. otypes = self.otypes
  2093. # self._ufunc is a dictionary whose keys are the number of
  2094. # arguments (i.e. len(args)) and whose values are ufuncs created
  2095. # by frompyfunc. len(args) can be different for different calls if
  2096. # self.pyfunc has parameters with default values. We only use the
  2097. # cache when func is self.pyfunc, which occurs when the call uses
  2098. # only positional arguments and no arguments are excluded.
  2099. nin = len(args)
  2100. nout = len(self.otypes)
  2101. if func is not self.pyfunc or nin not in self._ufunc:
  2102. ufunc = frompyfunc(func, nin, nout)
  2103. else:
  2104. ufunc = None # We'll get it from self._ufunc
  2105. if func is self.pyfunc:
  2106. ufunc = self._ufunc.setdefault(nin, ufunc)
  2107. else:
  2108. # Get number of outputs and output types by calling the function on
  2109. # the first entries of args. We also cache the result to prevent
  2110. # the subsequent call when the ufunc is evaluated.
  2111. # Assumes that ufunc first evaluates the 0th elements in the input
  2112. # arrays (the input values are not checked to ensure this)
  2113. args = [asarray(a) for a in args]
  2114. if builtins.any(arg.size == 0 for arg in args):
  2115. raise ValueError('cannot call `vectorize` on size 0 inputs '
  2116. 'unless `otypes` is set')
  2117. inputs = [arg.flat[0] for arg in args]
  2118. outputs = func(*inputs)
  2119. # Performance note: profiling indicates that -- for simple
  2120. # functions at least -- this wrapping can almost double the
  2121. # execution time.
  2122. # Hence we make it optional.
  2123. if self.cache:
  2124. _cache = [outputs]
  2125. def _func(*vargs):
  2126. if _cache:
  2127. return _cache.pop()
  2128. else:
  2129. return func(*vargs)
  2130. else:
  2131. _func = func
  2132. if isinstance(outputs, tuple):
  2133. nout = len(outputs)
  2134. else:
  2135. nout = 1
  2136. outputs = (outputs,)
  2137. otypes = ''.join([asarray(outputs[_k]).dtype.char
  2138. for _k in range(nout)])
  2139. # Performance note: profiling indicates that creating the ufunc is
  2140. # not a significant cost compared with wrapping so it seems not
  2141. # worth trying to cache this.
  2142. ufunc = frompyfunc(_func, len(args), nout)
  2143. return ufunc, otypes
  2144. def _vectorize_call(self, func, args):
  2145. """Vectorized call to `func` over positional `args`."""
  2146. if self.signature is not None:
  2147. res = self._vectorize_call_with_signature(func, args)
  2148. elif not args:
  2149. res = func()
  2150. else:
  2151. ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
  2152. # gh-29196: `dtype=object` should eventually be removed
  2153. args = [asanyarray(a, dtype=object) for a in args]
  2154. outputs = ufunc(*args, out=...)
  2155. if ufunc.nout == 1:
  2156. res = asanyarray(outputs, dtype=otypes[0])
  2157. else:
  2158. res = tuple(asanyarray(x, dtype=t)
  2159. for x, t in zip(outputs, otypes))
  2160. return res
  2161. def _vectorize_call_with_signature(self, func, args):
  2162. """Vectorized call over positional arguments with a signature."""
  2163. input_core_dims, output_core_dims = self._in_and_out_core_dims
  2164. if len(args) != len(input_core_dims):
  2165. raise TypeError('wrong number of positional arguments: '
  2166. 'expected %r, got %r'
  2167. % (len(input_core_dims), len(args)))
  2168. args = tuple(asanyarray(arg) for arg in args)
  2169. broadcast_shape, dim_sizes = _parse_input_dimensions(
  2170. args, input_core_dims)
  2171. input_shapes = _calculate_shapes(broadcast_shape, dim_sizes,
  2172. input_core_dims)
  2173. args = [np.broadcast_to(arg, shape, subok=True)
  2174. for arg, shape in zip(args, input_shapes)]
  2175. outputs = None
  2176. otypes = self.otypes
  2177. nout = len(output_core_dims)
  2178. for index in np.ndindex(*broadcast_shape):
  2179. results = func(*(arg[index] for arg in args))
  2180. n_results = len(results) if isinstance(results, tuple) else 1
  2181. if nout != n_results:
  2182. raise ValueError(
  2183. 'wrong number of outputs from pyfunc: expected %r, got %r'
  2184. % (nout, n_results))
  2185. if nout == 1:
  2186. results = (results,)
  2187. if outputs is None:
  2188. for result, core_dims in zip(results, output_core_dims):
  2189. _update_dim_sizes(dim_sizes, result, core_dims)
  2190. outputs = _create_arrays(broadcast_shape, dim_sizes,
  2191. output_core_dims, otypes, results)
  2192. for output, result in zip(outputs, results):
  2193. output[index] = result
  2194. if outputs is None:
  2195. # did not call the function even once
  2196. if otypes is None:
  2197. raise ValueError('cannot call `vectorize` on size 0 inputs '
  2198. 'unless `otypes` is set')
  2199. if builtins.any(dim not in dim_sizes
  2200. for dims in output_core_dims
  2201. for dim in dims):
  2202. raise ValueError('cannot call `vectorize` with a signature '
  2203. 'including new output dimensions on size 0 '
  2204. 'inputs')
  2205. outputs = _create_arrays(broadcast_shape, dim_sizes,
  2206. output_core_dims, otypes)
  2207. return outputs[0] if nout == 1 else outputs
  2208. def _cov_dispatcher(m, y=None, rowvar=None, bias=None, ddof=None,
  2209. fweights=None, aweights=None, *, dtype=None):
  2210. return (m, y, fweights, aweights)
  2211. @array_function_dispatch(_cov_dispatcher)
  2212. def cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,
  2213. aweights=None, *, dtype=None):
  2214. """
  2215. Estimate a covariance matrix, given data and weights.
  2216. Covariance indicates the level to which two variables vary together.
  2217. If we examine N-dimensional samples, :math:`X = [x_1, x_2, ..., x_N]^T`,
  2218. then the covariance matrix element :math:`C_{ij}` is the covariance of
  2219. :math:`x_i` and :math:`x_j`. The element :math:`C_{ii}` is the variance
  2220. of :math:`x_i`.
  2221. See the notes for an outline of the algorithm.
  2222. Parameters
  2223. ----------
  2224. m : array_like
  2225. A 1-D or 2-D array containing multiple variables and observations.
  2226. Each row of `m` represents a variable, and each column a single
  2227. observation of all those variables. Also see `rowvar` below.
  2228. y : array_like, optional
  2229. An additional set of variables and observations. `y` has the same form
  2230. as that of `m`.
  2231. rowvar : bool, optional
  2232. If `rowvar` is True (default), then each row represents a
  2233. variable, with observations in the columns. Otherwise, the relationship
  2234. is transposed: each column represents a variable, while the rows
  2235. contain observations.
  2236. bias : bool, optional
  2237. Default normalization (False) is by ``(N - 1)``, where ``N`` is the
  2238. number of observations given (unbiased estimate). If `bias` is True,
  2239. then normalization is by ``N``. These values can be overridden by using
  2240. the keyword ``ddof`` in numpy versions >= 1.5.
  2241. ddof : int, optional
  2242. If not ``None`` the default value implied by `bias` is overridden.
  2243. Note that ``ddof=1`` will return the unbiased estimate, even if both
  2244. `fweights` and `aweights` are specified, and ``ddof=0`` will return
  2245. the simple average. See the notes for the details. The default value
  2246. is ``None``.
  2247. fweights : array_like, int, optional
  2248. 1-D array of integer frequency weights; the number of times each
  2249. observation vector should be repeated.
  2250. aweights : array_like, optional
  2251. 1-D array of observation vector weights. These relative weights are
  2252. typically large for observations considered "important" and smaller for
  2253. observations considered less "important". If ``ddof=0`` the array of
  2254. weights can be used to assign probabilities to observation vectors.
  2255. dtype : data-type, optional
  2256. Data-type of the result. By default, the return data-type will have
  2257. at least `numpy.float64` precision.
  2258. .. versionadded:: 1.20
  2259. Returns
  2260. -------
  2261. out : ndarray
  2262. The covariance matrix of the variables.
  2263. See Also
  2264. --------
  2265. corrcoef : Normalized covariance matrix
  2266. Notes
  2267. -----
  2268. Assume that the observations are in the columns of the observation
  2269. array `m` and let ``f = fweights`` and ``a = aweights`` for brevity. The
  2270. steps to compute the weighted covariance are as follows::
  2271. >>> m = np.arange(10, dtype=np.float64)
  2272. >>> f = np.arange(10) * 2
  2273. >>> a = np.arange(10) ** 2.
  2274. >>> ddof = 1
  2275. >>> w = f * a
  2276. >>> v1 = np.sum(w)
  2277. >>> v2 = np.sum(w * a)
  2278. >>> m -= np.sum(m * w, axis=None, keepdims=True) / v1
  2279. >>> cov = np.dot(m * w, m.T) * v1 / (v1**2 - ddof * v2)
  2280. Note that when ``a == 1``, the normalization factor
  2281. ``v1 / (v1**2 - ddof * v2)`` goes over to ``1 / (np.sum(f) - ddof)``
  2282. as it should.
  2283. Examples
  2284. --------
  2285. >>> import numpy as np
  2286. Consider two variables, :math:`x_0` and :math:`x_1`, which
  2287. correlate perfectly, but in opposite directions:
  2288. >>> x = np.array([[0, 2], [1, 1], [2, 0]]).T
  2289. >>> x
  2290. array([[0, 1, 2],
  2291. [2, 1, 0]])
  2292. Note how :math:`x_0` increases while :math:`x_1` decreases. The covariance
  2293. matrix shows this clearly:
  2294. >>> np.cov(x)
  2295. array([[ 1., -1.],
  2296. [-1., 1.]])
  2297. Note that element :math:`C_{0,1}`, which shows the correlation between
  2298. :math:`x_0` and :math:`x_1`, is negative.
  2299. Further, note how `x` and `y` are combined:
  2300. >>> x = [-2.1, -1, 4.3]
  2301. >>> y = [3, 1.1, 0.12]
  2302. >>> X = np.stack((x, y), axis=0)
  2303. >>> np.cov(X)
  2304. array([[11.71 , -4.286 ], # may vary
  2305. [-4.286 , 2.144133]])
  2306. >>> np.cov(x, y)
  2307. array([[11.71 , -4.286 ], # may vary
  2308. [-4.286 , 2.144133]])
  2309. >>> np.cov(x)
  2310. array(11.71)
  2311. """
  2312. # Check inputs
  2313. if ddof is not None and ddof != int(ddof):
  2314. raise ValueError(
  2315. "ddof must be integer")
  2316. # Handles complex arrays too
  2317. m = np.asarray(m)
  2318. if m.ndim > 2:
  2319. raise ValueError("m has more than 2 dimensions")
  2320. if y is not None:
  2321. y = np.asarray(y)
  2322. if y.ndim > 2:
  2323. raise ValueError("y has more than 2 dimensions")
  2324. if dtype is None:
  2325. if y is None:
  2326. dtype = np.result_type(m, np.float64)
  2327. else:
  2328. dtype = np.result_type(m, y, np.float64)
  2329. X = array(m, ndmin=2, dtype=dtype)
  2330. if not rowvar and m.ndim != 1:
  2331. X = X.T
  2332. if X.shape[0] == 0:
  2333. return np.array([]).reshape(0, 0)
  2334. if y is not None:
  2335. y = array(y, copy=None, ndmin=2, dtype=dtype)
  2336. if not rowvar and y.shape[0] != 1:
  2337. y = y.T
  2338. X = np.concatenate((X, y), axis=0)
  2339. if ddof is None:
  2340. if bias == 0:
  2341. ddof = 1
  2342. else:
  2343. ddof = 0
  2344. # Get the product of frequencies and weights
  2345. w = None
  2346. if fweights is not None:
  2347. fweights = np.asarray(fweights, dtype=float)
  2348. if not np.all(fweights == np.around(fweights)):
  2349. raise TypeError(
  2350. "fweights must be integer")
  2351. if fweights.ndim > 1:
  2352. raise RuntimeError(
  2353. "cannot handle multidimensional fweights")
  2354. if fweights.shape[0] != X.shape[1]:
  2355. raise RuntimeError(
  2356. "incompatible numbers of samples and fweights")
  2357. if any(fweights < 0):
  2358. raise ValueError(
  2359. "fweights cannot be negative")
  2360. w = fweights
  2361. if aweights is not None:
  2362. aweights = np.asarray(aweights, dtype=float)
  2363. if aweights.ndim > 1:
  2364. raise RuntimeError(
  2365. "cannot handle multidimensional aweights")
  2366. if aweights.shape[0] != X.shape[1]:
  2367. raise RuntimeError(
  2368. "incompatible numbers of samples and aweights")
  2369. if any(aweights < 0):
  2370. raise ValueError(
  2371. "aweights cannot be negative")
  2372. if w is None:
  2373. w = aweights
  2374. else:
  2375. w *= aweights
  2376. avg, w_sum = average(X, axis=1, weights=w, returned=True)
  2377. w_sum = w_sum[0]
  2378. # Determine the normalization
  2379. if w is None:
  2380. fact = X.shape[1] - ddof
  2381. elif ddof == 0:
  2382. fact = w_sum
  2383. elif aweights is None:
  2384. fact = w_sum - ddof
  2385. else:
  2386. fact = w_sum - ddof * sum(w * aweights) / w_sum
  2387. if fact <= 0:
  2388. warnings.warn("Degrees of freedom <= 0 for slice",
  2389. RuntimeWarning, stacklevel=2)
  2390. fact = 0.0
  2391. X -= avg[:, None]
  2392. if w is None:
  2393. X_T = X.T
  2394. else:
  2395. X_T = (X * w).T
  2396. c = dot(X, X_T.conj())
  2397. c *= np.true_divide(1, fact)
  2398. return c.squeeze()
  2399. def _corrcoef_dispatcher(x, y=None, rowvar=None, *,
  2400. dtype=None):
  2401. return (x, y)
  2402. @array_function_dispatch(_corrcoef_dispatcher)
  2403. def corrcoef(x, y=None, rowvar=True, *,
  2404. dtype=None):
  2405. """
  2406. Return Pearson product-moment correlation coefficients.
  2407. Please refer to the documentation for `cov` for more detail. The
  2408. relationship between the correlation coefficient matrix, `R`, and the
  2409. covariance matrix, `C`, is
  2410. .. math:: R_{ij} = \\frac{ C_{ij} } { \\sqrt{ C_{ii} C_{jj} } }
  2411. The values of `R` are between -1 and 1, inclusive.
  2412. Parameters
  2413. ----------
  2414. x : array_like
  2415. A 1-D or 2-D array containing multiple variables and observations.
  2416. Each row of `x` represents a variable, and each column a single
  2417. observation of all those variables. Also see `rowvar` below.
  2418. y : array_like, optional
  2419. An additional set of variables and observations. `y` has the same
  2420. shape as `x`.
  2421. rowvar : bool, optional
  2422. If `rowvar` is True (default), then each row represents a
  2423. variable, with observations in the columns. Otherwise, the relationship
  2424. is transposed: each column represents a variable, while the rows
  2425. contain observations.
  2426. dtype : data-type, optional
  2427. Data-type of the result. By default, the return data-type will have
  2428. at least `numpy.float64` precision.
  2429. .. versionadded:: 1.20
  2430. Returns
  2431. -------
  2432. R : ndarray
  2433. The correlation coefficient matrix of the variables.
  2434. See Also
  2435. --------
  2436. cov : Covariance matrix
  2437. Notes
  2438. -----
  2439. Due to floating point rounding the resulting array may not be Hermitian,
  2440. the diagonal elements may not be 1, and the elements may not satisfy the
  2441. inequality abs(a) <= 1. The real and imaginary parts are clipped to the
  2442. interval [-1, 1] in an attempt to improve on that situation but is not
  2443. much help in the complex case.
  2444. Examples
  2445. --------
  2446. >>> import numpy as np
  2447. In this example we generate two random arrays, ``xarr`` and ``yarr``, and
  2448. compute the row-wise and column-wise Pearson correlation coefficients,
  2449. ``R``. Since ``rowvar`` is true by default, we first find the row-wise
  2450. Pearson correlation coefficients between the variables of ``xarr``.
  2451. >>> import numpy as np
  2452. >>> rng = np.random.default_rng(seed=42)
  2453. >>> xarr = rng.random((3, 3))
  2454. >>> xarr
  2455. array([[0.77395605, 0.43887844, 0.85859792],
  2456. [0.69736803, 0.09417735, 0.97562235],
  2457. [0.7611397 , 0.78606431, 0.12811363]])
  2458. >>> R1 = np.corrcoef(xarr)
  2459. >>> R1
  2460. array([[ 1. , 0.99256089, -0.68080986],
  2461. [ 0.99256089, 1. , -0.76492172],
  2462. [-0.68080986, -0.76492172, 1. ]])
  2463. If we add another set of variables and observations ``yarr``, we can
  2464. compute the row-wise Pearson correlation coefficients between the
  2465. variables in ``xarr`` and ``yarr``.
  2466. >>> yarr = rng.random((3, 3))
  2467. >>> yarr
  2468. array([[0.45038594, 0.37079802, 0.92676499],
  2469. [0.64386512, 0.82276161, 0.4434142 ],
  2470. [0.22723872, 0.55458479, 0.06381726]])
  2471. >>> R2 = np.corrcoef(xarr, yarr)
  2472. >>> R2
  2473. array([[ 1. , 0.99256089, -0.68080986, 0.75008178, -0.934284 ,
  2474. -0.99004057],
  2475. [ 0.99256089, 1. , -0.76492172, 0.82502011, -0.97074098,
  2476. -0.99981569],
  2477. [-0.68080986, -0.76492172, 1. , -0.99507202, 0.89721355,
  2478. 0.77714685],
  2479. [ 0.75008178, 0.82502011, -0.99507202, 1. , -0.93657855,
  2480. -0.83571711],
  2481. [-0.934284 , -0.97074098, 0.89721355, -0.93657855, 1. ,
  2482. 0.97517215],
  2483. [-0.99004057, -0.99981569, 0.77714685, -0.83571711, 0.97517215,
  2484. 1. ]])
  2485. Finally if we use the option ``rowvar=False``, the columns are now
  2486. being treated as the variables and we will find the column-wise Pearson
  2487. correlation coefficients between variables in ``xarr`` and ``yarr``.
  2488. >>> R3 = np.corrcoef(xarr, yarr, rowvar=False)
  2489. >>> R3
  2490. array([[ 1. , 0.77598074, -0.47458546, -0.75078643, -0.9665554 ,
  2491. 0.22423734],
  2492. [ 0.77598074, 1. , -0.92346708, -0.99923895, -0.58826587,
  2493. -0.44069024],
  2494. [-0.47458546, -0.92346708, 1. , 0.93773029, 0.23297648,
  2495. 0.75137473],
  2496. [-0.75078643, -0.99923895, 0.93773029, 1. , 0.55627469,
  2497. 0.47536961],
  2498. [-0.9665554 , -0.58826587, 0.23297648, 0.55627469, 1. ,
  2499. -0.46666491],
  2500. [ 0.22423734, -0.44069024, 0.75137473, 0.47536961, -0.46666491,
  2501. 1. ]])
  2502. """
  2503. c = cov(x, y, rowvar, dtype=dtype)
  2504. try:
  2505. d = diag(c)
  2506. except ValueError:
  2507. # scalar covariance
  2508. # nan if incorrect value (nan, inf, 0), 1 otherwise
  2509. return c / c
  2510. stddev = sqrt(d.real)
  2511. c /= stddev[:, None]
  2512. c /= stddev[None, :]
  2513. # Clip real and imaginary parts to [-1, 1]. This does not guarantee
  2514. # abs(a[i,j]) <= 1 for complex arrays, but is the best we can do without
  2515. # excessive work.
  2516. np.clip(c.real, -1, 1, out=c.real)
  2517. if np.iscomplexobj(c):
  2518. np.clip(c.imag, -1, 1, out=c.imag)
  2519. return c
  2520. @set_module('numpy')
  2521. def blackman(M):
  2522. """
  2523. Return the Blackman window.
  2524. The Blackman window is a taper formed by using the first three
  2525. terms of a summation of cosines. It was designed to have close to the
  2526. minimal leakage possible. It is close to optimal, only slightly worse
  2527. than a Kaiser window.
  2528. Parameters
  2529. ----------
  2530. M : int
  2531. Number of points in the output window. If zero or less, an empty
  2532. array is returned.
  2533. Returns
  2534. -------
  2535. out : ndarray
  2536. The window, with the maximum value normalized to one (the value one
  2537. appears only if the number of samples is odd).
  2538. See Also
  2539. --------
  2540. bartlett, hamming, hanning, kaiser
  2541. Notes
  2542. -----
  2543. The Blackman window is defined as
  2544. .. math:: w(n) = 0.42 - 0.5 \\cos(2\\pi n/M) + 0.08 \\cos(4\\pi n/M)
  2545. Most references to the Blackman window come from the signal processing
  2546. literature, where it is used as one of many windowing functions for
  2547. smoothing values. It is also known as an apodization (which means
  2548. "removing the foot", i.e. smoothing discontinuities at the beginning
  2549. and end of the sampled signal) or tapering function. It is known as a
  2550. "near optimal" tapering function, almost as good (by some measures)
  2551. as the kaiser window.
  2552. References
  2553. ----------
  2554. Blackman, R.B. and Tukey, J.W., (1958) The measurement of power spectra,
  2555. Dover Publications, New York.
  2556. Oppenheim, A.V., and R.W. Schafer. Discrete-Time Signal Processing.
  2557. Upper Saddle River, NJ: Prentice-Hall, 1999, pp. 468-471.
  2558. Examples
  2559. --------
  2560. >>> import numpy as np
  2561. >>> import matplotlib.pyplot as plt
  2562. >>> np.blackman(12)
  2563. array([-1.38777878e-17, 3.26064346e-02, 1.59903635e-01, # may vary
  2564. 4.14397981e-01, 7.36045180e-01, 9.67046769e-01,
  2565. 9.67046769e-01, 7.36045180e-01, 4.14397981e-01,
  2566. 1.59903635e-01, 3.26064346e-02, -1.38777878e-17])
  2567. Plot the window and the frequency response.
  2568. .. plot::
  2569. :include-source:
  2570. import matplotlib.pyplot as plt
  2571. from numpy.fft import fft, fftshift
  2572. window = np.blackman(51)
  2573. plt.plot(window)
  2574. plt.title("Blackman window")
  2575. plt.ylabel("Amplitude")
  2576. plt.xlabel("Sample")
  2577. plt.show() # doctest: +SKIP
  2578. plt.figure()
  2579. A = fft(window, 2048) / 25.5
  2580. mag = np.abs(fftshift(A))
  2581. freq = np.linspace(-0.5, 0.5, len(A))
  2582. with np.errstate(divide='ignore', invalid='ignore'):
  2583. response = 20 * np.log10(mag)
  2584. response = np.clip(response, -100, 100)
  2585. plt.plot(freq, response)
  2586. plt.title("Frequency response of Blackman window")
  2587. plt.ylabel("Magnitude [dB]")
  2588. plt.xlabel("Normalized frequency [cycles per sample]")
  2589. plt.axis('tight')
  2590. plt.show()
  2591. """
  2592. # Ensures at least float64 via 0.0. M should be an integer, but conversion
  2593. # to double is safe for a range.
  2594. values = np.array([0.0, M])
  2595. M = values[1]
  2596. if M < 1:
  2597. return array([], dtype=values.dtype)
  2598. if M == 1:
  2599. return ones(1, dtype=values.dtype)
  2600. n = arange(1 - M, M, 2)
  2601. return 0.42 + 0.5 * cos(pi * n / (M - 1)) + 0.08 * cos(2.0 * pi * n / (M - 1))
  2602. @set_module('numpy')
  2603. def bartlett(M):
  2604. """
  2605. Return the Bartlett window.
  2606. The Bartlett window is very similar to a triangular window, except
  2607. that the end points are at zero. It is often used in signal
  2608. processing for tapering a signal, without generating too much
  2609. ripple in the frequency domain.
  2610. Parameters
  2611. ----------
  2612. M : int
  2613. Number of points in the output window. If zero or less, an
  2614. empty array is returned.
  2615. Returns
  2616. -------
  2617. out : array
  2618. The triangular window, with the maximum value normalized to one
  2619. (the value one appears only if the number of samples is odd), with
  2620. the first and last samples equal to zero.
  2621. See Also
  2622. --------
  2623. blackman, hamming, hanning, kaiser
  2624. Notes
  2625. -----
  2626. The Bartlett window is defined as
  2627. .. math:: w(n) = \\frac{2}{M-1} \\left(
  2628. \\frac{M-1}{2} - \\left|n - \\frac{M-1}{2}\\right|
  2629. \\right)
  2630. Most references to the Bartlett window come from the signal processing
  2631. literature, where it is used as one of many windowing functions for
  2632. smoothing values. Note that convolution with this window produces linear
  2633. interpolation. It is also known as an apodization (which means "removing
  2634. the foot", i.e. smoothing discontinuities at the beginning and end of the
  2635. sampled signal) or tapering function. The Fourier transform of the
  2636. Bartlett window is the product of two sinc functions. Note the excellent
  2637. discussion in Kanasewich [2]_.
  2638. References
  2639. ----------
  2640. .. [1] M.S. Bartlett, "Periodogram Analysis and Continuous Spectra",
  2641. Biometrika 37, 1-16, 1950.
  2642. .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics",
  2643. The University of Alberta Press, 1975, pp. 109-110.
  2644. .. [3] A.V. Oppenheim and R.W. Schafer, "Discrete-Time Signal
  2645. Processing", Prentice-Hall, 1999, pp. 468-471.
  2646. .. [4] Wikipedia, "Window function",
  2647. https://en.wikipedia.org/wiki/Window_function
  2648. .. [5] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling,
  2649. "Numerical Recipes", Cambridge University Press, 1986, page 429.
  2650. Examples
  2651. --------
  2652. >>> import numpy as np
  2653. >>> import matplotlib.pyplot as plt
  2654. >>> np.bartlett(12)
  2655. array([ 0. , 0.18181818, 0.36363636, 0.54545455, 0.72727273, # may vary
  2656. 0.90909091, 0.90909091, 0.72727273, 0.54545455, 0.36363636,
  2657. 0.18181818, 0. ])
  2658. Plot the window and its frequency response (requires SciPy and matplotlib).
  2659. .. plot::
  2660. :include-source:
  2661. import matplotlib.pyplot as plt
  2662. from numpy.fft import fft, fftshift
  2663. window = np.bartlett(51)
  2664. plt.plot(window)
  2665. plt.title("Bartlett window")
  2666. plt.ylabel("Amplitude")
  2667. plt.xlabel("Sample")
  2668. plt.show()
  2669. plt.figure()
  2670. A = fft(window, 2048) / 25.5
  2671. mag = np.abs(fftshift(A))
  2672. freq = np.linspace(-0.5, 0.5, len(A))
  2673. with np.errstate(divide='ignore', invalid='ignore'):
  2674. response = 20 * np.log10(mag)
  2675. response = np.clip(response, -100, 100)
  2676. plt.plot(freq, response)
  2677. plt.title("Frequency response of Bartlett window")
  2678. plt.ylabel("Magnitude [dB]")
  2679. plt.xlabel("Normalized frequency [cycles per sample]")
  2680. plt.axis('tight')
  2681. plt.show()
  2682. """
  2683. # Ensures at least float64 via 0.0. M should be an integer, but conversion
  2684. # to double is safe for a range.
  2685. values = np.array([0.0, M])
  2686. M = values[1]
  2687. if M < 1:
  2688. return array([], dtype=values.dtype)
  2689. if M == 1:
  2690. return ones(1, dtype=values.dtype)
  2691. n = arange(1 - M, M, 2)
  2692. return where(less_equal(n, 0), 1 + n / (M - 1), 1 - n / (M - 1))
  2693. @set_module('numpy')
  2694. def hanning(M):
  2695. """
  2696. Return the Hanning window.
  2697. The Hanning window is a taper formed by using a weighted cosine.
  2698. Parameters
  2699. ----------
  2700. M : int
  2701. Number of points in the output window. If zero or less, an
  2702. empty array is returned.
  2703. Returns
  2704. -------
  2705. out : ndarray, shape(M,)
  2706. The window, with the maximum value normalized to one (the value
  2707. one appears only if `M` is odd).
  2708. See Also
  2709. --------
  2710. bartlett, blackman, hamming, kaiser
  2711. Notes
  2712. -----
  2713. The Hanning window is defined as
  2714. .. math:: w(n) = 0.5 - 0.5\\cos\\left(\\frac{2\\pi{n}}{M-1}\\right)
  2715. \\qquad 0 \\leq n \\leq M-1
  2716. The Hanning was named for Julius von Hann, an Austrian meteorologist.
  2717. It is also known as the Cosine Bell. Some authors prefer that it be
  2718. called a Hann window, to help avoid confusion with the very similar
  2719. Hamming window.
  2720. Most references to the Hanning window come from the signal processing
  2721. literature, where it is used as one of many windowing functions for
  2722. smoothing values. It is also known as an apodization (which means
  2723. "removing the foot", i.e. smoothing discontinuities at the beginning
  2724. and end of the sampled signal) or tapering function.
  2725. References
  2726. ----------
  2727. .. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power
  2728. spectra, Dover Publications, New York.
  2729. .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics",
  2730. The University of Alberta Press, 1975, pp. 106-108.
  2731. .. [3] Wikipedia, "Window function",
  2732. https://en.wikipedia.org/wiki/Window_function
  2733. .. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling,
  2734. "Numerical Recipes", Cambridge University Press, 1986, page 425.
  2735. Examples
  2736. --------
  2737. >>> import numpy as np
  2738. >>> np.hanning(12)
  2739. array([0. , 0.07937323, 0.29229249, 0.57115742, 0.82743037,
  2740. 0.97974649, 0.97974649, 0.82743037, 0.57115742, 0.29229249,
  2741. 0.07937323, 0. ])
  2742. Plot the window and its frequency response.
  2743. .. plot::
  2744. :include-source:
  2745. import matplotlib.pyplot as plt
  2746. from numpy.fft import fft, fftshift
  2747. window = np.hanning(51)
  2748. plt.plot(window)
  2749. plt.title("Hann window")
  2750. plt.ylabel("Amplitude")
  2751. plt.xlabel("Sample")
  2752. plt.show()
  2753. plt.figure()
  2754. A = fft(window, 2048) / 25.5
  2755. mag = np.abs(fftshift(A))
  2756. freq = np.linspace(-0.5, 0.5, len(A))
  2757. with np.errstate(divide='ignore', invalid='ignore'):
  2758. response = 20 * np.log10(mag)
  2759. response = np.clip(response, -100, 100)
  2760. plt.plot(freq, response)
  2761. plt.title("Frequency response of the Hann window")
  2762. plt.ylabel("Magnitude [dB]")
  2763. plt.xlabel("Normalized frequency [cycles per sample]")
  2764. plt.axis('tight')
  2765. plt.show()
  2766. """
  2767. # Ensures at least float64 via 0.0. M should be an integer, but conversion
  2768. # to double is safe for a range.
  2769. values = np.array([0.0, M])
  2770. M = values[1]
  2771. if M < 1:
  2772. return array([], dtype=values.dtype)
  2773. if M == 1:
  2774. return ones(1, dtype=values.dtype)
  2775. n = arange(1 - M, M, 2)
  2776. return 0.5 + 0.5 * cos(pi * n / (M - 1))
  2777. @set_module('numpy')
  2778. def hamming(M):
  2779. """
  2780. Return the Hamming window.
  2781. The Hamming window is a taper formed by using a weighted cosine.
  2782. Parameters
  2783. ----------
  2784. M : int
  2785. Number of points in the output window. If zero or less, an
  2786. empty array is returned.
  2787. Returns
  2788. -------
  2789. out : ndarray
  2790. The window, with the maximum value normalized to one (the value
  2791. one appears only if the number of samples is odd).
  2792. See Also
  2793. --------
  2794. bartlett, blackman, hanning, kaiser
  2795. Notes
  2796. -----
  2797. The Hamming window is defined as
  2798. .. math:: w(n) = 0.54 - 0.46\\cos\\left(\\frac{2\\pi{n}}{M-1}\\right)
  2799. \\qquad 0 \\leq n \\leq M-1
  2800. The Hamming was named for R. W. Hamming, an associate of J. W. Tukey
  2801. and is described in Blackman and Tukey. It was recommended for
  2802. smoothing the truncated autocovariance function in the time domain.
  2803. Most references to the Hamming window come from the signal processing
  2804. literature, where it is used as one of many windowing functions for
  2805. smoothing values. It is also known as an apodization (which means
  2806. "removing the foot", i.e. smoothing discontinuities at the beginning
  2807. and end of the sampled signal) or tapering function.
  2808. References
  2809. ----------
  2810. .. [1] Blackman, R.B. and Tukey, J.W., (1958) The measurement of power
  2811. spectra, Dover Publications, New York.
  2812. .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", The
  2813. University of Alberta Press, 1975, pp. 109-110.
  2814. .. [3] Wikipedia, "Window function",
  2815. https://en.wikipedia.org/wiki/Window_function
  2816. .. [4] W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling,
  2817. "Numerical Recipes", Cambridge University Press, 1986, page 425.
  2818. Examples
  2819. --------
  2820. >>> import numpy as np
  2821. >>> np.hamming(12)
  2822. array([ 0.08 , 0.15302337, 0.34890909, 0.60546483, 0.84123594, # may vary
  2823. 0.98136677, 0.98136677, 0.84123594, 0.60546483, 0.34890909,
  2824. 0.15302337, 0.08 ])
  2825. Plot the window and the frequency response.
  2826. .. plot::
  2827. :include-source:
  2828. import matplotlib.pyplot as plt
  2829. from numpy.fft import fft, fftshift
  2830. window = np.hamming(51)
  2831. plt.plot(window)
  2832. plt.title("Hamming window")
  2833. plt.ylabel("Amplitude")
  2834. plt.xlabel("Sample")
  2835. plt.show()
  2836. plt.figure()
  2837. A = fft(window, 2048) / 25.5
  2838. mag = np.abs(fftshift(A))
  2839. freq = np.linspace(-0.5, 0.5, len(A))
  2840. response = 20 * np.log10(mag)
  2841. response = np.clip(response, -100, 100)
  2842. plt.plot(freq, response)
  2843. plt.title("Frequency response of Hamming window")
  2844. plt.ylabel("Magnitude [dB]")
  2845. plt.xlabel("Normalized frequency [cycles per sample]")
  2846. plt.axis('tight')
  2847. plt.show()
  2848. """
  2849. # Ensures at least float64 via 0.0. M should be an integer, but conversion
  2850. # to double is safe for a range.
  2851. values = np.array([0.0, M])
  2852. M = values[1]
  2853. if M < 1:
  2854. return array([], dtype=values.dtype)
  2855. if M == 1:
  2856. return ones(1, dtype=values.dtype)
  2857. n = arange(1 - M, M, 2)
  2858. return 0.54 + 0.46 * cos(pi * n / (M - 1))
  2859. ## Code from cephes for i0
  2860. _i0A = [
  2861. -4.41534164647933937950E-18,
  2862. 3.33079451882223809783E-17,
  2863. -2.43127984654795469359E-16,
  2864. 1.71539128555513303061E-15,
  2865. -1.16853328779934516808E-14,
  2866. 7.67618549860493561688E-14,
  2867. -4.85644678311192946090E-13,
  2868. 2.95505266312963983461E-12,
  2869. -1.72682629144155570723E-11,
  2870. 9.67580903537323691224E-11,
  2871. -5.18979560163526290666E-10,
  2872. 2.65982372468238665035E-9,
  2873. -1.30002500998624804212E-8,
  2874. 6.04699502254191894932E-8,
  2875. -2.67079385394061173391E-7,
  2876. 1.11738753912010371815E-6,
  2877. -4.41673835845875056359E-6,
  2878. 1.64484480707288970893E-5,
  2879. -5.75419501008210370398E-5,
  2880. 1.88502885095841655729E-4,
  2881. -5.76375574538582365885E-4,
  2882. 1.63947561694133579842E-3,
  2883. -4.32430999505057594430E-3,
  2884. 1.05464603945949983183E-2,
  2885. -2.37374148058994688156E-2,
  2886. 4.93052842396707084878E-2,
  2887. -9.49010970480476444210E-2,
  2888. 1.71620901522208775349E-1,
  2889. -3.04682672343198398683E-1,
  2890. 6.76795274409476084995E-1
  2891. ]
  2892. _i0B = [
  2893. -7.23318048787475395456E-18,
  2894. -4.83050448594418207126E-18,
  2895. 4.46562142029675999901E-17,
  2896. 3.46122286769746109310E-17,
  2897. -2.82762398051658348494E-16,
  2898. -3.42548561967721913462E-16,
  2899. 1.77256013305652638360E-15,
  2900. 3.81168066935262242075E-15,
  2901. -9.55484669882830764870E-15,
  2902. -4.15056934728722208663E-14,
  2903. 1.54008621752140982691E-14,
  2904. 3.85277838274214270114E-13,
  2905. 7.18012445138366623367E-13,
  2906. -1.79417853150680611778E-12,
  2907. -1.32158118404477131188E-11,
  2908. -3.14991652796324136454E-11,
  2909. 1.18891471078464383424E-11,
  2910. 4.94060238822496958910E-10,
  2911. 3.39623202570838634515E-9,
  2912. 2.26666899049817806459E-8,
  2913. 2.04891858946906374183E-7,
  2914. 2.89137052083475648297E-6,
  2915. 6.88975834691682398426E-5,
  2916. 3.36911647825569408990E-3,
  2917. 8.04490411014108831608E-1
  2918. ]
  2919. def _chbevl(x, vals):
  2920. b0 = vals[0]
  2921. b1 = 0.0
  2922. for i in range(1, len(vals)):
  2923. b2 = b1
  2924. b1 = b0
  2925. b0 = x * b1 - b2 + vals[i]
  2926. return 0.5 * (b0 - b2)
  2927. def _i0_1(x):
  2928. return exp(x) * _chbevl(x / 2.0 - 2, _i0A)
  2929. def _i0_2(x):
  2930. return exp(x) * _chbevl(32.0 / x - 2.0, _i0B) / sqrt(x)
  2931. def _i0_dispatcher(x):
  2932. return (x,)
  2933. @array_function_dispatch(_i0_dispatcher)
  2934. def i0(x):
  2935. """
  2936. Modified Bessel function of the first kind, order 0.
  2937. Usually denoted :math:`I_0`.
  2938. Parameters
  2939. ----------
  2940. x : array_like of float
  2941. Argument of the Bessel function.
  2942. Returns
  2943. -------
  2944. out : ndarray, shape = x.shape, dtype = float
  2945. The modified Bessel function evaluated at each of the elements of `x`.
  2946. See Also
  2947. --------
  2948. scipy.special.i0, scipy.special.iv, scipy.special.ive
  2949. Notes
  2950. -----
  2951. The scipy implementation is recommended over this function: it is a
  2952. proper ufunc written in C, and more than an order of magnitude faster.
  2953. We use the algorithm published by Clenshaw [1]_ and referenced by
  2954. Abramowitz and Stegun [2]_, for which the function domain is
  2955. partitioned into the two intervals [0,8] and (8,inf), and Chebyshev
  2956. polynomial expansions are employed in each interval. Relative error on
  2957. the domain [0,30] using IEEE arithmetic is documented [3]_ as having a
  2958. peak of 5.8e-16 with an rms of 1.4e-16 (n = 30000).
  2959. References
  2960. ----------
  2961. .. [1] C. W. Clenshaw, "Chebyshev series for mathematical functions", in
  2962. *National Physical Laboratory Mathematical Tables*, vol. 5, London:
  2963. Her Majesty's Stationery Office, 1962.
  2964. .. [2] M. Abramowitz and I. A. Stegun, *Handbook of Mathematical
  2965. Functions*, 10th printing, New York: Dover, 1964, pp. 379.
  2966. https://personal.math.ubc.ca/~cbm/aands/page_379.htm
  2967. .. [3] https://metacpan.org/pod/distribution/Math-Cephes/lib/Math/Cephes.pod#i0:-Modified-Bessel-function-of-order-zero
  2968. Examples
  2969. --------
  2970. >>> import numpy as np
  2971. >>> np.i0(0.)
  2972. array(1.0)
  2973. >>> np.i0([0, 1, 2, 3])
  2974. array([1. , 1.26606588, 2.2795853 , 4.88079259])
  2975. """
  2976. x = np.asanyarray(x)
  2977. if x.dtype.kind == 'c':
  2978. raise TypeError("i0 not supported for complex values")
  2979. if x.dtype.kind != 'f':
  2980. x = x.astype(float)
  2981. x = np.abs(x)
  2982. return piecewise(x, [x <= 8.0], [_i0_1, _i0_2])
  2983. ## End of cephes code for i0
  2984. @set_module('numpy')
  2985. def kaiser(M, beta):
  2986. """
  2987. Return the Kaiser window.
  2988. The Kaiser window is a taper formed by using a Bessel function.
  2989. Parameters
  2990. ----------
  2991. M : int
  2992. Number of points in the output window. If zero or less, an
  2993. empty array is returned.
  2994. beta : float
  2995. Shape parameter for window.
  2996. Returns
  2997. -------
  2998. out : array
  2999. The window, with the maximum value normalized to one (the value
  3000. one appears only if the number of samples is odd).
  3001. See Also
  3002. --------
  3003. bartlett, blackman, hamming, hanning
  3004. Notes
  3005. -----
  3006. The Kaiser window is defined as
  3007. .. math:: w(n) = I_0\\left( \\beta \\sqrt{1-\\frac{4n^2}{(M-1)^2}}
  3008. \\right)/I_0(\\beta)
  3009. with
  3010. .. math:: \\quad -\\frac{M-1}{2} \\leq n \\leq \\frac{M-1}{2},
  3011. where :math:`I_0` is the modified zeroth-order Bessel function.
  3012. The Kaiser was named for Jim Kaiser, who discovered a simple
  3013. approximation to the DPSS window based on Bessel functions. The Kaiser
  3014. window is a very good approximation to the Digital Prolate Spheroidal
  3015. Sequence, or Slepian window, which is the transform which maximizes the
  3016. energy in the main lobe of the window relative to total energy.
  3017. The Kaiser can approximate many other windows by varying the beta
  3018. parameter.
  3019. ==== =======================
  3020. beta Window shape
  3021. ==== =======================
  3022. 0 Rectangular
  3023. 5 Similar to a Hamming
  3024. 6 Similar to a Hanning
  3025. 8.6 Similar to a Blackman
  3026. ==== =======================
  3027. A beta value of 14 is probably a good starting point. Note that as beta
  3028. gets large, the window narrows, and so the number of samples needs to be
  3029. large enough to sample the increasingly narrow spike, otherwise NaNs will
  3030. get returned.
  3031. Most references to the Kaiser window come from the signal processing
  3032. literature, where it is used as one of many windowing functions for
  3033. smoothing values. It is also known as an apodization (which means
  3034. "removing the foot", i.e. smoothing discontinuities at the beginning
  3035. and end of the sampled signal) or tapering function.
  3036. References
  3037. ----------
  3038. .. [1] J. F. Kaiser, "Digital Filters" - Ch 7 in "Systems analysis by
  3039. digital computer", Editors: F.F. Kuo and J.F. Kaiser, p 218-285.
  3040. John Wiley and Sons, New York, (1966).
  3041. .. [2] E.R. Kanasewich, "Time Sequence Analysis in Geophysics", The
  3042. University of Alberta Press, 1975, pp. 177-178.
  3043. .. [3] Wikipedia, "Window function",
  3044. https://en.wikipedia.org/wiki/Window_function
  3045. Examples
  3046. --------
  3047. >>> import numpy as np
  3048. >>> import matplotlib.pyplot as plt
  3049. >>> np.kaiser(12, 14)
  3050. array([7.72686684e-06, 3.46009194e-03, 4.65200189e-02, # may vary
  3051. 2.29737120e-01, 5.99885316e-01, 9.45674898e-01,
  3052. 9.45674898e-01, 5.99885316e-01, 2.29737120e-01,
  3053. 4.65200189e-02, 3.46009194e-03, 7.72686684e-06])
  3054. Plot the window and the frequency response.
  3055. .. plot::
  3056. :include-source:
  3057. import matplotlib.pyplot as plt
  3058. from numpy.fft import fft, fftshift
  3059. window = np.kaiser(51, 14)
  3060. plt.plot(window)
  3061. plt.title("Kaiser window")
  3062. plt.ylabel("Amplitude")
  3063. plt.xlabel("Sample")
  3064. plt.show()
  3065. plt.figure()
  3066. A = fft(window, 2048) / 25.5
  3067. mag = np.abs(fftshift(A))
  3068. freq = np.linspace(-0.5, 0.5, len(A))
  3069. response = 20 * np.log10(mag)
  3070. response = np.clip(response, -100, 100)
  3071. plt.plot(freq, response)
  3072. plt.title("Frequency response of Kaiser window")
  3073. plt.ylabel("Magnitude [dB]")
  3074. plt.xlabel("Normalized frequency [cycles per sample]")
  3075. plt.axis('tight')
  3076. plt.show()
  3077. """
  3078. # Ensures at least float64 via 0.0. M should be an integer, but conversion
  3079. # to double is safe for a range. (Simplified result_type with 0.0
  3080. # strongly typed. result-type is not/less order sensitive, but that mainly
  3081. # matters for integers anyway.)
  3082. values = np.array([0.0, M, beta])
  3083. M = values[1]
  3084. beta = values[2]
  3085. if M == 1:
  3086. return np.ones(1, dtype=values.dtype)
  3087. n = arange(0, M)
  3088. alpha = (M - 1) / 2.0
  3089. return i0(beta * sqrt(1 - ((n - alpha) / alpha)**2.0)) / i0(beta)
  3090. def _sinc_dispatcher(x):
  3091. return (x,)
  3092. @array_function_dispatch(_sinc_dispatcher)
  3093. def sinc(x):
  3094. r"""
  3095. Return the normalized sinc function.
  3096. The sinc function is equal to :math:`\sin(\pi x)/(\pi x)` for any argument
  3097. :math:`x\ne 0`. ``sinc(0)`` takes the limit value 1, making ``sinc`` not
  3098. only everywhere continuous but also infinitely differentiable.
  3099. .. note::
  3100. Note the normalization factor of ``pi`` used in the definition.
  3101. This is the most commonly used definition in signal processing.
  3102. Use ``sinc(x / np.pi)`` to obtain the unnormalized sinc function
  3103. :math:`\sin(x)/x` that is more common in mathematics.
  3104. Parameters
  3105. ----------
  3106. x : ndarray
  3107. Array (possibly multi-dimensional) of values for which to calculate
  3108. ``sinc(x)``.
  3109. Returns
  3110. -------
  3111. out : ndarray
  3112. ``sinc(x)``, which has the same shape as the input.
  3113. Notes
  3114. -----
  3115. The name sinc is short for "sine cardinal" or "sinus cardinalis".
  3116. The sinc function is used in various signal processing applications,
  3117. including in anti-aliasing, in the construction of a Lanczos resampling
  3118. filter, and in interpolation.
  3119. For bandlimited interpolation of discrete-time signals, the ideal
  3120. interpolation kernel is proportional to the sinc function.
  3121. References
  3122. ----------
  3123. .. [1] Weisstein, Eric W. "Sinc Function." From MathWorld--A Wolfram Web
  3124. Resource. https://mathworld.wolfram.com/SincFunction.html
  3125. .. [2] Wikipedia, "Sinc function",
  3126. https://en.wikipedia.org/wiki/Sinc_function
  3127. Examples
  3128. --------
  3129. >>> import numpy as np
  3130. >>> import matplotlib.pyplot as plt
  3131. >>> x = np.linspace(-4, 4, 41)
  3132. >>> np.sinc(x)
  3133. array([-3.89804309e-17, -4.92362781e-02, -8.40918587e-02, # may vary
  3134. -8.90384387e-02, -5.84680802e-02, 3.89804309e-17,
  3135. 6.68206631e-02, 1.16434881e-01, 1.26137788e-01,
  3136. 8.50444803e-02, -3.89804309e-17, -1.03943254e-01,
  3137. -1.89206682e-01, -2.16236208e-01, -1.55914881e-01,
  3138. 3.89804309e-17, 2.33872321e-01, 5.04551152e-01,
  3139. 7.56826729e-01, 9.35489284e-01, 1.00000000e+00,
  3140. 9.35489284e-01, 7.56826729e-01, 5.04551152e-01,
  3141. 2.33872321e-01, 3.89804309e-17, -1.55914881e-01,
  3142. -2.16236208e-01, -1.89206682e-01, -1.03943254e-01,
  3143. -3.89804309e-17, 8.50444803e-02, 1.26137788e-01,
  3144. 1.16434881e-01, 6.68206631e-02, 3.89804309e-17,
  3145. -5.84680802e-02, -8.90384387e-02, -8.40918587e-02,
  3146. -4.92362781e-02, -3.89804309e-17])
  3147. >>> plt.plot(x, np.sinc(x))
  3148. [<matplotlib.lines.Line2D object at 0x...>]
  3149. >>> plt.title("Sinc Function")
  3150. Text(0.5, 1.0, 'Sinc Function')
  3151. >>> plt.ylabel("Amplitude")
  3152. Text(0, 0.5, 'Amplitude')
  3153. >>> plt.xlabel("X")
  3154. Text(0.5, 0, 'X')
  3155. >>> plt.show()
  3156. """
  3157. x = np.asanyarray(x)
  3158. x = pi * x
  3159. # Hope that 1e-20 is sufficient for objects...
  3160. eps = np.finfo(x.dtype).eps if x.dtype.kind == "f" else 1e-20
  3161. y = where(x, x, eps)
  3162. return sin(y) / y
  3163. def _ureduce(a, func, keepdims=False, **kwargs):
  3164. """
  3165. Internal Function.
  3166. Call `func` with `a` as first argument swapping the axes to use extended
  3167. axis on functions that don't support it natively.
  3168. Returns result and a.shape with axis dims set to 1.
  3169. Parameters
  3170. ----------
  3171. a : array_like
  3172. Input array or object that can be converted to an array.
  3173. func : callable
  3174. Reduction function capable of receiving a single axis argument.
  3175. It is called with `a` as first argument followed by `kwargs`.
  3176. kwargs : keyword arguments
  3177. additional keyword arguments to pass to `func`.
  3178. Returns
  3179. -------
  3180. result : tuple
  3181. Result of func(a, **kwargs) and a.shape with axis dims set to 1
  3182. which can be used to reshape the result to the same shape a ufunc with
  3183. keepdims=True would produce.
  3184. """
  3185. a = np.asanyarray(a)
  3186. axis = kwargs.get('axis')
  3187. out = kwargs.get('out')
  3188. if keepdims is np._NoValue:
  3189. keepdims = False
  3190. nd = a.ndim
  3191. if axis is not None:
  3192. axis = _nx.normalize_axis_tuple(axis, nd)
  3193. if keepdims and out is not None:
  3194. index_out = tuple(
  3195. 0 if i in axis else slice(None) for i in range(nd))
  3196. kwargs['out'] = out[(Ellipsis, ) + index_out]
  3197. if len(axis) == 1:
  3198. kwargs['axis'] = axis[0]
  3199. else:
  3200. keep = sorted(set(range(nd)) - set(axis))
  3201. nkeep = len(keep)
  3202. def reshape_arr(a):
  3203. # move axis that should not be reduced to front
  3204. a = np.moveaxis(a, keep, range(nkeep))
  3205. # merge reduced axis
  3206. return a.reshape(a.shape[:nkeep] + (-1,))
  3207. a = reshape_arr(a)
  3208. weights = kwargs.get("weights")
  3209. if weights is not None:
  3210. kwargs["weights"] = reshape_arr(weights)
  3211. kwargs['axis'] = -1
  3212. elif keepdims and out is not None:
  3213. index_out = (0, ) * nd
  3214. kwargs['out'] = out[(Ellipsis, ) + index_out]
  3215. r = func(a, **kwargs)
  3216. if out is not None:
  3217. return out
  3218. if keepdims:
  3219. if axis is None:
  3220. index_r = (np.newaxis, ) * nd
  3221. else:
  3222. index_r = tuple(
  3223. np.newaxis if i in axis else slice(None)
  3224. for i in range(nd))
  3225. r = r[(Ellipsis, ) + index_r]
  3226. return r
  3227. def _median_dispatcher(
  3228. a, axis=None, out=None, overwrite_input=None, keepdims=None):
  3229. return (a, out)
  3230. @array_function_dispatch(_median_dispatcher)
  3231. def median(a, axis=None, out=None, overwrite_input=False, keepdims=False):
  3232. """
  3233. Compute the median along the specified axis.
  3234. Returns the median of the array elements.
  3235. Parameters
  3236. ----------
  3237. a : array_like
  3238. Input array or object that can be converted to an array.
  3239. axis : {int, sequence of int, None}, optional
  3240. Axis or axes along which the medians are computed. The default,
  3241. axis=None, will compute the median along a flattened version of
  3242. the array. If a sequence of axes, the array is first flattened
  3243. along the given axes, then the median is computed along the
  3244. resulting flattened axis.
  3245. out : ndarray, optional
  3246. Alternative output array in which to place the result. It must
  3247. have the same shape and buffer length as the expected output,
  3248. but the type (of the output) will be cast if necessary.
  3249. overwrite_input : bool, optional
  3250. If True, then allow use of memory of input array `a` for
  3251. calculations. The input array will be modified by the call to
  3252. `median`. This will save memory when you do not need to preserve
  3253. the contents of the input array. Treat the input as undefined,
  3254. but it will probably be fully or partially sorted. Default is
  3255. False. If `overwrite_input` is ``True`` and `a` is not already an
  3256. `ndarray`, an error will be raised.
  3257. keepdims : bool, optional
  3258. If this is set to True, the axes which are reduced are left
  3259. in the result as dimensions with size one. With this option,
  3260. the result will broadcast correctly against the original `arr`.
  3261. Returns
  3262. -------
  3263. median : ndarray
  3264. A new array holding the result. If the input contains integers
  3265. or floats smaller than ``float64``, then the output data-type is
  3266. ``np.float64``. Otherwise, the data-type of the output is the
  3267. same as that of the input. If `out` is specified, that array is
  3268. returned instead.
  3269. See Also
  3270. --------
  3271. mean, percentile
  3272. Notes
  3273. -----
  3274. Given a vector ``V`` of length ``N``, the median of ``V`` is the
  3275. middle value of a sorted copy of ``V``, ``V_sorted`` - i
  3276. e., ``V_sorted[(N-1)/2]``, when ``N`` is odd, and the average of the
  3277. two middle values of ``V_sorted`` when ``N`` is even.
  3278. Examples
  3279. --------
  3280. >>> import numpy as np
  3281. >>> a = np.array([[10, 7, 4], [3, 2, 1]])
  3282. >>> a
  3283. array([[10, 7, 4],
  3284. [ 3, 2, 1]])
  3285. >>> np.median(a)
  3286. np.float64(3.5)
  3287. >>> np.median(a, axis=0)
  3288. array([6.5, 4.5, 2.5])
  3289. >>> np.median(a, axis=1)
  3290. array([7., 2.])
  3291. >>> np.median(a, axis=(0, 1))
  3292. np.float64(3.5)
  3293. >>> m = np.median(a, axis=0)
  3294. >>> out = np.zeros_like(m)
  3295. >>> np.median(a, axis=0, out=m)
  3296. array([6.5, 4.5, 2.5])
  3297. >>> m
  3298. array([6.5, 4.5, 2.5])
  3299. >>> b = a.copy()
  3300. >>> np.median(b, axis=1, overwrite_input=True)
  3301. array([7., 2.])
  3302. >>> assert not np.all(a==b)
  3303. >>> b = a.copy()
  3304. >>> np.median(b, axis=None, overwrite_input=True)
  3305. np.float64(3.5)
  3306. >>> assert not np.all(a==b)
  3307. """
  3308. return _ureduce(a, func=_median, keepdims=keepdims, axis=axis, out=out,
  3309. overwrite_input=overwrite_input)
  3310. def _median(a, axis=None, out=None, overwrite_input=False):
  3311. # can't be reasonably be implemented in terms of percentile as we have to
  3312. # call mean to not break astropy
  3313. a = np.asanyarray(a)
  3314. # Set the partition indexes
  3315. if axis is None:
  3316. sz = a.size
  3317. else:
  3318. sz = a.shape[axis]
  3319. if sz % 2 == 0:
  3320. szh = sz // 2
  3321. kth = [szh - 1, szh]
  3322. else:
  3323. kth = [(sz - 1) // 2]
  3324. # We have to check for NaNs (as of writing 'M' doesn't actually work).
  3325. supports_nans = np.issubdtype(a.dtype, np.inexact) or a.dtype.kind in 'Mm'
  3326. if supports_nans:
  3327. kth.append(-1)
  3328. if overwrite_input:
  3329. if axis is None:
  3330. part = a.ravel()
  3331. part.partition(kth)
  3332. else:
  3333. a.partition(kth, axis=axis)
  3334. part = a
  3335. else:
  3336. part = partition(a, kth, axis=axis)
  3337. if part.shape == ():
  3338. # make 0-D arrays work
  3339. return part.item()
  3340. if axis is None:
  3341. axis = 0
  3342. indexer = [slice(None)] * part.ndim
  3343. index = part.shape[axis] // 2
  3344. if part.shape[axis] % 2 == 1:
  3345. # index with slice to allow mean (below) to work
  3346. indexer[axis] = slice(index, index + 1)
  3347. else:
  3348. indexer[axis] = slice(index - 1, index + 1)
  3349. indexer = tuple(indexer)
  3350. # Use mean in both odd and even case to coerce data type,
  3351. # using out array if needed.
  3352. rout = mean(part[indexer], axis=axis, out=out)
  3353. if supports_nans and sz > 0:
  3354. # If nans are possible, warn and replace by nans like mean would.
  3355. rout = np.lib._utils_impl._median_nancheck(part, rout, axis)
  3356. return rout
  3357. def _percentile_dispatcher(a, q, axis=None, out=None, overwrite_input=None,
  3358. method=None, keepdims=None, *, weights=None):
  3359. return (a, q, out, weights)
  3360. @array_function_dispatch(_percentile_dispatcher)
  3361. def percentile(a,
  3362. q,
  3363. axis=None,
  3364. out=None,
  3365. overwrite_input=False,
  3366. method="linear",
  3367. keepdims=False,
  3368. *,
  3369. weights=None):
  3370. """
  3371. Compute the q-th percentile of the data along the specified axis.
  3372. Returns the q-th percentile(s) of the array elements.
  3373. Parameters
  3374. ----------
  3375. a : array_like of real numbers
  3376. Input array or object that can be converted to an array.
  3377. q : array_like of float
  3378. Percentage or sequence of percentages for the percentiles to compute.
  3379. Values must be between 0 and 100 inclusive.
  3380. axis : {int, tuple of int, None}, optional
  3381. Axis or axes along which the percentiles are computed. The
  3382. default is to compute the percentile(s) along a flattened
  3383. version of the array.
  3384. out : ndarray, optional
  3385. Alternative output array in which to place the result. It must
  3386. have the same shape and buffer length as the expected output,
  3387. but the type (of the output) will be cast if necessary.
  3388. overwrite_input : bool, optional
  3389. If True, then allow the input array `a` to be modified by intermediate
  3390. calculations, to save memory. In this case, the contents of the input
  3391. `a` after this function completes is undefined.
  3392. method : str, optional
  3393. This parameter specifies the method to use for estimating the
  3394. percentile. There are many different methods, some unique to NumPy.
  3395. See the notes for explanation. The options sorted by their R type
  3396. as summarized in the H&F paper [1]_ are:
  3397. 1. 'inverted_cdf'
  3398. 2. 'averaged_inverted_cdf'
  3399. 3. 'closest_observation'
  3400. 4. 'interpolated_inverted_cdf'
  3401. 5. 'hazen'
  3402. 6. 'weibull'
  3403. 7. 'linear' (default)
  3404. 8. 'median_unbiased'
  3405. 9. 'normal_unbiased'
  3406. The first three methods are discontinuous. NumPy further defines the
  3407. following discontinuous variations of the default 'linear' (7.) option:
  3408. * 'lower'
  3409. * 'higher',
  3410. * 'midpoint'
  3411. * 'nearest'
  3412. .. versionchanged:: 1.22.0
  3413. This argument was previously called "interpolation" and only
  3414. offered the "linear" default and last four options.
  3415. keepdims : bool, optional
  3416. If this is set to True, the axes which are reduced are left in
  3417. the result as dimensions with size one. With this option, the
  3418. result will broadcast correctly against the original array `a`.
  3419. weights : array_like, optional
  3420. An array of weights associated with the values in `a`. Each value in
  3421. `a` contributes to the percentile according to its associated weight.
  3422. The weights array can either be 1-D (in which case its length must be
  3423. the size of `a` along the given axis) or of the same shape as `a`.
  3424. If `weights=None`, then all data in `a` are assumed to have a
  3425. weight equal to one.
  3426. Only `method="inverted_cdf"` supports weights.
  3427. See the notes for more details.
  3428. .. versionadded:: 2.0.0
  3429. Returns
  3430. -------
  3431. percentile : scalar or ndarray
  3432. If `q` is a single percentile and `axis=None`, then the result
  3433. is a scalar. If multiple percentiles are given, first axis of
  3434. the result corresponds to the percentiles. The other axes are
  3435. the axes that remain after the reduction of `a`. If the input
  3436. contains integers or floats smaller than ``float64``, the output
  3437. data-type is ``float64``. Otherwise, the output data-type is the
  3438. same as that of the input. If `out` is specified, that array is
  3439. returned instead.
  3440. See Also
  3441. --------
  3442. mean
  3443. median : equivalent to ``percentile(..., 50)``
  3444. nanpercentile
  3445. quantile : equivalent to percentile, except q in the range [0, 1].
  3446. Notes
  3447. -----
  3448. The behavior of `numpy.percentile` with percentage `q` is
  3449. that of `numpy.quantile` with argument ``q/100``.
  3450. For more information, please see `numpy.quantile`.
  3451. Examples
  3452. --------
  3453. >>> import numpy as np
  3454. >>> a = np.array([[10, 7, 4], [3, 2, 1]])
  3455. >>> a
  3456. array([[10, 7, 4],
  3457. [ 3, 2, 1]])
  3458. >>> np.percentile(a, 50)
  3459. 3.5
  3460. >>> np.percentile(a, 50, axis=0)
  3461. array([6.5, 4.5, 2.5])
  3462. >>> np.percentile(a, 50, axis=1)
  3463. array([7., 2.])
  3464. >>> np.percentile(a, 50, axis=1, keepdims=True)
  3465. array([[7.],
  3466. [2.]])
  3467. >>> m = np.percentile(a, 50, axis=0)
  3468. >>> out = np.zeros_like(m)
  3469. >>> np.percentile(a, 50, axis=0, out=out)
  3470. array([6.5, 4.5, 2.5])
  3471. >>> m
  3472. array([6.5, 4.5, 2.5])
  3473. >>> b = a.copy()
  3474. >>> np.percentile(b, 50, axis=1, overwrite_input=True)
  3475. array([7., 2.])
  3476. >>> assert not np.all(a == b)
  3477. The different methods can be visualized graphically:
  3478. .. plot::
  3479. import matplotlib.pyplot as plt
  3480. a = np.arange(4)
  3481. p = np.linspace(0, 100, 6001)
  3482. ax = plt.gca()
  3483. lines = [
  3484. ('linear', '-', 'C0'),
  3485. ('inverted_cdf', ':', 'C1'),
  3486. # Almost the same as `inverted_cdf`:
  3487. ('averaged_inverted_cdf', '-.', 'C1'),
  3488. ('closest_observation', ':', 'C2'),
  3489. ('interpolated_inverted_cdf', '--', 'C1'),
  3490. ('hazen', '--', 'C3'),
  3491. ('weibull', '-.', 'C4'),
  3492. ('median_unbiased', '--', 'C5'),
  3493. ('normal_unbiased', '-.', 'C6'),
  3494. ]
  3495. for method, style, color in lines:
  3496. ax.plot(
  3497. p, np.percentile(a, p, method=method),
  3498. label=method, linestyle=style, color=color)
  3499. ax.set(
  3500. title='Percentiles for different methods and data: ' + str(a),
  3501. xlabel='Percentile',
  3502. ylabel='Estimated percentile value',
  3503. yticks=a)
  3504. ax.legend(bbox_to_anchor=(1.03, 1))
  3505. plt.tight_layout()
  3506. plt.show()
  3507. References
  3508. ----------
  3509. .. [1] R. J. Hyndman and Y. Fan,
  3510. "Sample quantiles in statistical packages,"
  3511. The American Statistician, 50(4), pp. 361-365, 1996
  3512. """
  3513. a = np.asanyarray(a)
  3514. if a.dtype.kind == "c":
  3515. raise TypeError("a must be an array of real numbers")
  3516. weak_q = type(q) in (int, float) # use weak promotion for final result type
  3517. q = np.true_divide(q, 100, out=...)
  3518. if not _quantile_is_valid(q):
  3519. raise ValueError("Percentiles must be in the range [0, 100]")
  3520. if weights is not None:
  3521. if method != "inverted_cdf":
  3522. msg = ("Only method 'inverted_cdf' supports weights. "
  3523. f"Got: {method}.")
  3524. raise ValueError(msg)
  3525. if axis is not None:
  3526. axis = _nx.normalize_axis_tuple(axis, a.ndim, argname="axis")
  3527. weights = _weights_are_valid(weights=weights, a=a, axis=axis)
  3528. if np.any(weights < 0):
  3529. raise ValueError("Weights must be non-negative.")
  3530. return _quantile_unchecked(
  3531. a, q, axis, out, overwrite_input, method, keepdims, weights, weak_q)
  3532. def _quantile_dispatcher(a, q, axis=None, out=None, overwrite_input=None,
  3533. method=None, keepdims=None, *, weights=None):
  3534. return (a, q, out, weights)
  3535. @array_function_dispatch(_quantile_dispatcher)
  3536. def quantile(a,
  3537. q,
  3538. axis=None,
  3539. out=None,
  3540. overwrite_input=False,
  3541. method="linear",
  3542. keepdims=False,
  3543. *,
  3544. weights=None):
  3545. """
  3546. Compute the q-th quantile of the data along the specified axis.
  3547. Parameters
  3548. ----------
  3549. a : array_like of real numbers
  3550. Input array or object that can be converted to an array.
  3551. q : array_like of float
  3552. Probability or sequence of probabilities of the quantiles to compute.
  3553. Values must be between 0 and 1 inclusive.
  3554. axis : {int, tuple of int, None}, optional
  3555. Axis or axes along which the quantiles are computed. The default is
  3556. to compute the quantile(s) along a flattened version of the array.
  3557. out : ndarray, optional
  3558. Alternative output array in which to place the result. It must have
  3559. the same shape and buffer length as the expected output, but the
  3560. type (of the output) will be cast if necessary.
  3561. overwrite_input : bool, optional
  3562. If True, then allow the input array `a` to be modified by
  3563. intermediate calculations, to save memory. In this case, the
  3564. contents of the input `a` after this function completes is
  3565. undefined.
  3566. method : str, optional
  3567. This parameter specifies the method to use for estimating the
  3568. quantile. There are many different methods, some unique to NumPy.
  3569. The recommended options, numbered as they appear in [1]_, are:
  3570. 1. 'inverted_cdf'
  3571. 2. 'averaged_inverted_cdf'
  3572. 3. 'closest_observation'
  3573. 4. 'interpolated_inverted_cdf'
  3574. 5. 'hazen'
  3575. 6. 'weibull'
  3576. 7. 'linear' (default)
  3577. 8. 'median_unbiased'
  3578. 9. 'normal_unbiased'
  3579. The first three methods are discontinuous. For backward compatibility
  3580. with previous versions of NumPy, the following discontinuous variations
  3581. of the default 'linear' (7.) option are available:
  3582. * 'lower'
  3583. * 'higher',
  3584. * 'midpoint'
  3585. * 'nearest'
  3586. See Notes for details.
  3587. .. versionchanged:: 1.22.0
  3588. This argument was previously called "interpolation" and only
  3589. offered the "linear" default and last four options.
  3590. keepdims : bool, optional
  3591. If this is set to True, the axes which are reduced are left in
  3592. the result as dimensions with size one. With this option, the
  3593. result will broadcast correctly against the original array `a`.
  3594. weights : array_like, optional
  3595. An array of weights associated with the values in `a`. Each value in
  3596. `a` contributes to the quantile according to its associated weight.
  3597. The weights array can either be 1-D (in which case its length must be
  3598. the size of `a` along the given axis) or of the same shape as `a`.
  3599. If `weights=None`, then all data in `a` are assumed to have a
  3600. weight equal to one.
  3601. Only `method="inverted_cdf"` supports weights.
  3602. See the notes for more details.
  3603. .. versionadded:: 2.0.0
  3604. Returns
  3605. -------
  3606. quantile : scalar or ndarray
  3607. If `q` is a single probability and `axis=None`, then the result
  3608. is a scalar. If multiple probability levels are given, first axis
  3609. of the result corresponds to the quantiles. The other axes are
  3610. the axes that remain after the reduction of `a`. If the input
  3611. contains integers or floats smaller than ``float64``, the output
  3612. data-type is ``float64``. Otherwise, the output data-type is the
  3613. same as that of the input. If `out` is specified, that array is
  3614. returned instead.
  3615. See Also
  3616. --------
  3617. mean
  3618. percentile : equivalent to quantile, but with q in the range [0, 100].
  3619. median : equivalent to ``quantile(..., 0.5)``
  3620. nanquantile
  3621. Notes
  3622. -----
  3623. Given a sample `a` from an underlying distribution, `quantile` provides a
  3624. nonparametric estimate of the inverse cumulative distribution function.
  3625. By default, this is done by interpolating between adjacent elements in
  3626. ``y``, a sorted copy of `a`::
  3627. (1-g)*y[j] + g*y[j+1]
  3628. where the index ``j`` and coefficient ``g`` are the integral and
  3629. fractional components of ``q * (n-1)``, and ``n`` is the number of
  3630. elements in the sample.
  3631. This is a special case of Equation 1 of H&F [1]_. More generally,
  3632. - ``j = (q*n + m - 1) // 1``, and
  3633. - ``g = (q*n + m - 1) % 1``,
  3634. where ``m`` may be defined according to several different conventions.
  3635. The preferred convention may be selected using the ``method`` parameter:
  3636. =============================== =============== ===============
  3637. ``method`` number in H&F ``m``
  3638. =============================== =============== ===============
  3639. ``interpolated_inverted_cdf`` 4 ``0``
  3640. ``hazen`` 5 ``1/2``
  3641. ``weibull`` 6 ``q``
  3642. ``linear`` (default) 7 ``1 - q``
  3643. ``median_unbiased`` 8 ``q/3 + 1/3``
  3644. ``normal_unbiased`` 9 ``q/4 + 3/8``
  3645. =============================== =============== ===============
  3646. Note that indices ``j`` and ``j + 1`` are clipped to the range ``0`` to
  3647. ``n - 1`` when the results of the formula would be outside the allowed
  3648. range of non-negative indices. The ``- 1`` in the formulas for ``j`` and
  3649. ``g`` accounts for Python's 0-based indexing.
  3650. The table above includes only the estimators from H&F that are continuous
  3651. functions of probability `q` (estimators 4-9). NumPy also provides the
  3652. three discontinuous estimators from H&F (estimators 1-3), where ``j`` is
  3653. defined as above, ``m`` is defined as follows, and ``g`` is a function
  3654. of the real-valued ``index = q*n + m - 1`` and ``j``.
  3655. 1. ``inverted_cdf``: ``m = 0`` and ``g = int(index - j > 0)``
  3656. 2. ``averaged_inverted_cdf``: ``m = 0`` and
  3657. ``g = (1 + int(index - j > 0)) / 2``
  3658. 3. ``closest_observation``: ``m = -1/2`` and
  3659. ``g = 1 - int((index == j) & (j%2 == 1))``
  3660. For backward compatibility with previous versions of NumPy, `quantile`
  3661. provides four additional discontinuous estimators. Like
  3662. ``method='linear'``, all have ``m = 1 - q`` so that ``j = q*(n-1) // 1``,
  3663. but ``g`` is defined as follows.
  3664. - ``lower``: ``g = 0``
  3665. - ``midpoint``: ``g = 0.5``
  3666. - ``higher``: ``g = 1``
  3667. - ``nearest``: ``g = (q*(n-1) % 1) > 0.5``
  3668. **Weighted quantiles:**
  3669. More formally, the quantile at probability level :math:`q` of a cumulative
  3670. distribution function :math:`F(y)=P(Y \\leq y)` with probability measure
  3671. :math:`P` is defined as any number :math:`x` that fulfills the
  3672. *coverage conditions*
  3673. .. math:: P(Y < x) \\leq q \\quad\\text{and}\\quad P(Y \\leq x) \\geq q
  3674. with random variable :math:`Y\\sim P`.
  3675. Sample quantiles, the result of `quantile`, provide nonparametric
  3676. estimation of the underlying population counterparts, represented by the
  3677. unknown :math:`F`, given a data vector `a` of length ``n``.
  3678. Some of the estimators above arise when one considers :math:`F` as the
  3679. empirical distribution function of the data, i.e.
  3680. :math:`F(y) = \\frac{1}{n} \\sum_i 1_{a_i \\leq y}`.
  3681. Then, different methods correspond to different choices of :math:`x` that
  3682. fulfill the above coverage conditions. Methods that follow this approach
  3683. are ``inverted_cdf`` and ``averaged_inverted_cdf``.
  3684. For weighted quantiles, the coverage conditions still hold. The
  3685. empirical cumulative distribution is simply replaced by its weighted
  3686. version, i.e.
  3687. :math:`P(Y \\leq t) = \\frac{1}{\\sum_i w_i} \\sum_i w_i 1_{x_i \\leq t}`.
  3688. Only ``method="inverted_cdf"`` supports weights.
  3689. Examples
  3690. --------
  3691. >>> import numpy as np
  3692. >>> a = np.array([[10, 7, 4], [3, 2, 1]])
  3693. >>> a
  3694. array([[10, 7, 4],
  3695. [ 3, 2, 1]])
  3696. >>> np.quantile(a, 0.5)
  3697. 3.5
  3698. >>> np.quantile(a, 0.5, axis=0)
  3699. array([6.5, 4.5, 2.5])
  3700. >>> np.quantile(a, 0.5, axis=1)
  3701. array([7., 2.])
  3702. >>> np.quantile(a, 0.5, axis=1, keepdims=True)
  3703. array([[7.],
  3704. [2.]])
  3705. >>> m = np.quantile(a, 0.5, axis=0)
  3706. >>> out = np.zeros_like(m)
  3707. >>> np.quantile(a, 0.5, axis=0, out=out)
  3708. array([6.5, 4.5, 2.5])
  3709. >>> m
  3710. array([6.5, 4.5, 2.5])
  3711. >>> b = a.copy()
  3712. >>> np.quantile(b, 0.5, axis=1, overwrite_input=True)
  3713. array([7., 2.])
  3714. >>> assert not np.all(a == b)
  3715. See also `numpy.percentile` for a visualization of most methods.
  3716. References
  3717. ----------
  3718. .. [1] R. J. Hyndman and Y. Fan,
  3719. "Sample quantiles in statistical packages,"
  3720. The American Statistician, 50(4), pp. 361-365, 1996
  3721. """
  3722. a = np.asanyarray(a)
  3723. if a.dtype.kind == "c":
  3724. raise TypeError("a must be an array of real numbers")
  3725. weak_q = type(q) in (int, float) # use weak promotion for final result type
  3726. q = np.asanyarray(q)
  3727. if not _quantile_is_valid(q):
  3728. raise ValueError("Quantiles must be in the range [0, 1]")
  3729. if weights is not None:
  3730. if method != "inverted_cdf":
  3731. msg = ("Only method 'inverted_cdf' supports weights. "
  3732. f"Got: {method}.")
  3733. raise ValueError(msg)
  3734. if axis is not None:
  3735. axis = _nx.normalize_axis_tuple(axis, a.ndim, argname="axis")
  3736. weights = _weights_are_valid(weights=weights, a=a, axis=axis)
  3737. if np.any(weights < 0):
  3738. raise ValueError("Weights must be non-negative.")
  3739. return _quantile_unchecked(
  3740. a, q, axis, out, overwrite_input, method, keepdims, weights, weak_q)
  3741. def _quantile_unchecked(a,
  3742. q,
  3743. axis=None,
  3744. out=None,
  3745. overwrite_input=False,
  3746. method="linear",
  3747. keepdims=False,
  3748. weights=None,
  3749. weak_q=False):
  3750. """Assumes that q is in [0, 1], and is an ndarray"""
  3751. return _ureduce(a,
  3752. func=_quantile_ureduce_func,
  3753. q=q,
  3754. weights=weights,
  3755. keepdims=keepdims,
  3756. axis=axis,
  3757. out=out,
  3758. overwrite_input=overwrite_input,
  3759. method=method,
  3760. weak_q=weak_q)
  3761. def _quantile_is_valid(q):
  3762. # avoid expensive reductions, relevant for arrays with < O(1000) elements
  3763. if q.ndim == 1 and q.size < 10:
  3764. for i in range(q.size):
  3765. if not (0.0 <= q[i] <= 1.0):
  3766. return False
  3767. elif not (q.min() >= 0 and q.max() <= 1):
  3768. return False
  3769. return True
  3770. def _compute_virtual_index(n, quantiles, alpha: float, beta: float):
  3771. """
  3772. Compute the floating point indexes of an array for the linear
  3773. interpolation of quantiles.
  3774. n : array_like
  3775. The sample sizes.
  3776. quantiles : array_like
  3777. The quantiles values.
  3778. alpha : float
  3779. A constant used to correct the index computed.
  3780. beta : float
  3781. A constant used to correct the index computed.
  3782. alpha and beta values depend on the chosen method
  3783. (see quantile documentation)
  3784. Reference:
  3785. Hyndman&Fan paper "Sample Quantiles in Statistical Packages",
  3786. DOI: 10.1080/00031305.1996.10473566
  3787. """
  3788. return n * quantiles + (
  3789. alpha + quantiles * (1 - alpha - beta)
  3790. ) - 1
  3791. def _get_gamma(virtual_indexes, previous_indexes, method):
  3792. """
  3793. Compute gamma (a.k.a 'm' or 'weight') for the linear interpolation
  3794. of quantiles.
  3795. virtual_indexes : array_like
  3796. The indexes where the percentile is supposed to be found in the sorted
  3797. sample.
  3798. previous_indexes : array_like
  3799. The floor values of virtual_indexes.
  3800. method : dict
  3801. The interpolation method chosen, which may have a specific rule
  3802. modifying gamma.
  3803. gamma is usually the fractional part of virtual_indexes but can be modified
  3804. by the interpolation method.
  3805. """
  3806. gamma = np.asanyarray(virtual_indexes - previous_indexes)
  3807. gamma = method["fix_gamma"](gamma, virtual_indexes)
  3808. # Ensure both that we have an array, and that we keep the dtype
  3809. # (which may have been matched to the input array).
  3810. return np.asanyarray(gamma, dtype=virtual_indexes.dtype)
  3811. def _lerp(a, b, t, out=None):
  3812. """
  3813. Compute the linear interpolation weighted by gamma on each point of
  3814. two same shape array.
  3815. a : array_like
  3816. Left bound.
  3817. b : array_like
  3818. Right bound.
  3819. t : array_like
  3820. The interpolation weight.
  3821. out : array_like
  3822. Output array.
  3823. """
  3824. diff_b_a = b - a
  3825. lerp_interpolation = add(a, diff_b_a * t, out=... if out is None else out)
  3826. subtract(b, diff_b_a * (1 - t), out=lerp_interpolation, where=t >= 0.5,
  3827. casting='unsafe', dtype=type(lerp_interpolation.dtype))
  3828. if lerp_interpolation.ndim == 0 and out is None:
  3829. lerp_interpolation = lerp_interpolation[()] # unpack 0d arrays
  3830. return lerp_interpolation
  3831. def _get_gamma_mask(shape, default_value, conditioned_value, where):
  3832. out = np.full(shape, default_value)
  3833. np.copyto(out, conditioned_value, where=where, casting="unsafe")
  3834. return out
  3835. def _discrete_interpolation_to_boundaries(index, gamma_condition_fun):
  3836. previous = np.floor(index)
  3837. next = previous + 1
  3838. gamma = index - previous
  3839. res = _get_gamma_mask(shape=index.shape,
  3840. default_value=next,
  3841. conditioned_value=previous,
  3842. where=gamma_condition_fun(gamma, index)
  3843. ).astype(np.intp)
  3844. # Some methods can lead to out-of-bound integers, clip them:
  3845. res[res < 0] = 0
  3846. return res
  3847. def _closest_observation(n, quantiles):
  3848. # "choose the nearest even order statistic at g=0" (H&F (1996) pp. 362).
  3849. # Order is 1-based so for zero-based indexing round to nearest odd index.
  3850. gamma_fun = lambda gamma, index: (gamma == 0) & (np.floor(index) % 2 == 1)
  3851. return _discrete_interpolation_to_boundaries((n * quantiles) - 1 - 0.5,
  3852. gamma_fun)
  3853. def _inverted_cdf(n, quantiles):
  3854. gamma_fun = lambda gamma, _: (gamma == 0)
  3855. return _discrete_interpolation_to_boundaries((n * quantiles) - 1,
  3856. gamma_fun)
  3857. def _quantile_ureduce_func(
  3858. a: np.ndarray,
  3859. q: np.ndarray,
  3860. weights: np.ndarray | None,
  3861. axis: int | None = None,
  3862. out: np.ndarray | None = None,
  3863. overwrite_input: bool = False,
  3864. method: str = "linear",
  3865. weak_q: bool = False,
  3866. ) -> np.ndarray:
  3867. if q.ndim > 2:
  3868. # The code below works fine for nd, but it might not have useful
  3869. # semantics. For now, keep the supported dimensions the same as it was
  3870. # before.
  3871. raise ValueError("q must be a scalar or 1d")
  3872. if overwrite_input:
  3873. if axis is None:
  3874. axis = 0
  3875. arr = a.ravel()
  3876. wgt = None if weights is None else weights.ravel()
  3877. else:
  3878. arr = a
  3879. wgt = weights
  3880. elif axis is None:
  3881. axis = 0
  3882. arr = a.flatten()
  3883. wgt = None if weights is None else weights.flatten()
  3884. else:
  3885. arr = a.copy()
  3886. wgt = weights
  3887. result = _quantile(arr,
  3888. quantiles=q,
  3889. axis=axis,
  3890. method=method,
  3891. out=out,
  3892. weights=wgt,
  3893. weak_q=weak_q)
  3894. return result
  3895. def _get_indexes(arr, virtual_indexes, valid_values_count):
  3896. """
  3897. Get the valid indexes of arr neighbouring virtual_indexes.
  3898. Note
  3899. This is a companion function to linear interpolation of
  3900. Quantiles
  3901. Returns
  3902. -------
  3903. (previous_indexes, next_indexes): Tuple
  3904. A Tuple of virtual_indexes neighbouring indexes
  3905. """
  3906. previous_indexes = floor(virtual_indexes, out=...)
  3907. next_indexes = add(previous_indexes, 1, out=...)
  3908. indexes_above_bounds = virtual_indexes >= valid_values_count - 1
  3909. # When indexes is above max index, take the max value of the array
  3910. if indexes_above_bounds.any():
  3911. previous_indexes[indexes_above_bounds] = -1
  3912. next_indexes[indexes_above_bounds] = -1
  3913. # When indexes is below min index, take the min value of the array
  3914. indexes_below_bounds = virtual_indexes < 0
  3915. if indexes_below_bounds.any():
  3916. previous_indexes[indexes_below_bounds] = 0
  3917. next_indexes[indexes_below_bounds] = 0
  3918. if np.issubdtype(arr.dtype, np.inexact):
  3919. # After the sort, slices having NaNs will have for last element a NaN
  3920. virtual_indexes_nans = np.isnan(virtual_indexes)
  3921. if virtual_indexes_nans.any():
  3922. previous_indexes[virtual_indexes_nans] = -1
  3923. next_indexes[virtual_indexes_nans] = -1
  3924. previous_indexes = previous_indexes.astype(np.intp)
  3925. next_indexes = next_indexes.astype(np.intp)
  3926. return previous_indexes, next_indexes
  3927. def _quantile(
  3928. arr: "np.typing.ArrayLike",
  3929. quantiles: np.ndarray,
  3930. axis: int = -1,
  3931. method: str = "linear",
  3932. out: np.ndarray | None = None,
  3933. weights: "np.typing.ArrayLike | None" = None,
  3934. weak_q: bool = False,
  3935. ) -> np.ndarray:
  3936. """
  3937. Private function that doesn't support extended axis or keepdims.
  3938. These methods are extended to this function using _ureduce
  3939. See nanpercentile for parameter usage
  3940. It computes the quantiles of the array for the given axis.
  3941. A linear interpolation is performed based on the `method`.
  3942. By default, the method is "linear" where alpha == beta == 1 which
  3943. performs the 7th method of Hyndman&Fan.
  3944. With "median_unbiased" we get alpha == beta == 1/3
  3945. thus the 8th method of Hyndman&Fan.
  3946. """
  3947. # --- Setup
  3948. arr = np.asanyarray(arr)
  3949. values_count = arr.shape[axis]
  3950. # The dimensions of `q` are prepended to the output shape, so we need the
  3951. # axis being sampled from `arr` to be last.
  3952. if axis != 0: # But moveaxis is slow, so only call it if necessary.
  3953. arr = np.moveaxis(arr, axis, destination=0)
  3954. supports_nans = (
  3955. np.issubdtype(arr.dtype, np.inexact) or arr.dtype.kind in 'Mm'
  3956. )
  3957. if weights is None:
  3958. # --- Computation of indexes
  3959. # Index where to find the value in the sorted array.
  3960. # Virtual because it is a floating point value, not a valid index.
  3961. # The nearest neighbours are used for interpolation
  3962. try:
  3963. method_props = _QuantileMethods[method]
  3964. except KeyError:
  3965. raise ValueError(
  3966. f"{method!r} is not a valid method. Use one of: "
  3967. f"{_QuantileMethods.keys()}") from None
  3968. virtual_indexes = method_props["get_virtual_index"](values_count,
  3969. quantiles)
  3970. virtual_indexes = np.asanyarray(virtual_indexes)
  3971. if method_props["fix_gamma"] is None:
  3972. supports_integers = True
  3973. else:
  3974. int_virtual_indices = np.issubdtype(virtual_indexes.dtype,
  3975. np.integer)
  3976. supports_integers = method == 'linear' and int_virtual_indices
  3977. if supports_integers:
  3978. # No interpolation needed, take the points along axis
  3979. if supports_nans:
  3980. # may contain nan, which would sort to the end
  3981. arr.partition(
  3982. concatenate((virtual_indexes.ravel(), [-1])), axis=0,
  3983. )
  3984. slices_having_nans = np.isnan(arr[-1, ...])
  3985. else:
  3986. # cannot contain nan
  3987. arr.partition(virtual_indexes.ravel(), axis=0)
  3988. slices_having_nans = np.array(False, dtype=bool)
  3989. result = take(arr, virtual_indexes, axis=0, out=out)
  3990. else:
  3991. previous_indexes, next_indexes = _get_indexes(arr,
  3992. virtual_indexes,
  3993. values_count)
  3994. # --- Sorting
  3995. arr.partition(
  3996. np.unique(np.concatenate(([0, -1],
  3997. previous_indexes.ravel(),
  3998. next_indexes.ravel(),
  3999. ))),
  4000. axis=0)
  4001. if supports_nans:
  4002. slices_having_nans = np.isnan(arr[-1, ...])
  4003. else:
  4004. slices_having_nans = None
  4005. # --- Get values from indexes
  4006. previous = arr[previous_indexes]
  4007. next = arr[next_indexes]
  4008. # --- Linear interpolation
  4009. gamma = _get_gamma(virtual_indexes, previous_indexes,
  4010. method_props)
  4011. if weak_q:
  4012. gamma = float(gamma)
  4013. else:
  4014. result_shape = virtual_indexes.shape + (1,) * (arr.ndim - 1)
  4015. gamma = gamma.reshape(result_shape)
  4016. result = _lerp(previous,
  4017. next,
  4018. gamma,
  4019. out=out)
  4020. else:
  4021. # Weighted case
  4022. # This implements method="inverted_cdf", the only supported weighted
  4023. # method, which needs to sort anyway.
  4024. weights = np.asanyarray(weights)
  4025. if axis != 0:
  4026. weights = np.moveaxis(weights, axis, destination=0)
  4027. index_array = np.argsort(arr, axis=0)
  4028. # arr = arr[index_array, ...] # but this adds trailing dimensions of
  4029. # 1.
  4030. arr = np.take_along_axis(arr, index_array, axis=0)
  4031. if weights.shape == arr.shape:
  4032. weights = np.take_along_axis(weights, index_array, axis=0)
  4033. else:
  4034. # weights is 1d
  4035. weights = weights.reshape(-1)[index_array, ...]
  4036. if supports_nans:
  4037. # may contain nan, which would sort to the end
  4038. slices_having_nans = np.isnan(arr[-1, ...])
  4039. else:
  4040. # cannot contain nan
  4041. slices_having_nans = np.array(False, dtype=bool)
  4042. # We use the weights to calculate the empirical cumulative
  4043. # distribution function cdf
  4044. cdf = weights.cumsum(axis=0, dtype=np.float64)
  4045. cdf /= cdf[-1, ...] # normalization to 1
  4046. if np.isnan(cdf[-1]).any():
  4047. # Above calculations should normally warn for the zero/inf case.
  4048. raise ValueError("Weights included NaN, inf or were all zero.")
  4049. # Search index i such that
  4050. # sum(weights[j], j=0..i-1) < quantile <= sum(weights[j], j=0..i)
  4051. # is then equivalent to
  4052. # cdf[i-1] < quantile <= cdf[i]
  4053. # Unfortunately, searchsorted only accepts 1-d arrays as first
  4054. # argument, so we will need to iterate over dimensions.
  4055. # Without the following cast, searchsorted can return surprising
  4056. # results, e.g.
  4057. # np.searchsorted(np.array([0.2, 0.4, 0.6, 0.8, 1.]),
  4058. # np.array(0.4, dtype=np.float32), side="left")
  4059. # returns 2 instead of 1 because 0.4 is not binary representable.
  4060. if quantiles.dtype.kind == "f":
  4061. cdf = cdf.astype(quantiles.dtype)
  4062. # Weights must be non-negative, so we might have zero weights at the
  4063. # beginning leading to some leading zeros in cdf. The call to
  4064. # np.searchsorted for quantiles=0 will then pick the first element,
  4065. # but should pick the first one larger than zero. We
  4066. # therefore simply set 0 values in cdf to -1.
  4067. if np.any(cdf[0, ...] == 0):
  4068. cdf[cdf == 0] = -1
  4069. def find_cdf_1d(arr, cdf):
  4070. indices = np.searchsorted(cdf, quantiles, side="left")
  4071. # We might have reached the maximum with i = len(arr), e.g. for
  4072. # quantiles = 1, and need to cut it to len(arr) - 1.
  4073. indices = minimum(indices, values_count - 1)
  4074. result = take(arr, indices, axis=0)
  4075. return result
  4076. r_shape = arr.shape[1:]
  4077. if quantiles.ndim > 0:
  4078. r_shape = quantiles.shape + r_shape
  4079. if out is None:
  4080. result = np.empty_like(arr, shape=r_shape)
  4081. else:
  4082. if out.shape != r_shape:
  4083. msg = (f"Wrong shape of argument 'out', shape={r_shape} is "
  4084. f"required; got shape={out.shape}.")
  4085. raise ValueError(msg)
  4086. result = out
  4087. # See apply_along_axis, which we do for axis=0. Note that Ni = (,)
  4088. # always, so we remove it here.
  4089. Nk = arr.shape[1:]
  4090. for kk in np.ndindex(Nk):
  4091. result[(...,) + kk] = find_cdf_1d(
  4092. arr[np.s_[:, ] + kk], cdf[np.s_[:, ] + kk]
  4093. )
  4094. # Make result the same as in unweighted inverted_cdf.
  4095. if result.shape == () and result.dtype == np.dtype("O"):
  4096. result = result.item()
  4097. if np.any(slices_having_nans):
  4098. if result.ndim == 0 and out is None:
  4099. # can't write to a scalar, but indexing will be correct
  4100. result = arr[-1]
  4101. else:
  4102. np.copyto(result, arr[-1, ...], where=slices_having_nans)
  4103. return result
  4104. def _trapezoid_dispatcher(y, x=None, dx=None, axis=None):
  4105. return (y, x)
  4106. @array_function_dispatch(_trapezoid_dispatcher)
  4107. def trapezoid(y, x=None, dx=1.0, axis=-1):
  4108. r"""
  4109. Integrate along the given axis using the composite trapezoidal rule.
  4110. If `x` is provided, the integration happens in sequence along its
  4111. elements - they are not sorted.
  4112. Integrate `y` (`x`) along each 1d slice on the given axis, compute
  4113. :math:`\int y(x) dx`.
  4114. When `x` is specified, this integrates along the parametric curve,
  4115. computing :math:`\int_t y(t) dt =
  4116. \int_t y(t) \left.\frac{dx}{dt}\right|_{x=x(t)} dt`.
  4117. .. versionadded:: 2.0.0
  4118. Parameters
  4119. ----------
  4120. y : array_like
  4121. Input array to integrate.
  4122. x : array_like, optional
  4123. The sample points corresponding to the `y` values. If `x` is None,
  4124. the sample points are assumed to be evenly spaced `dx` apart. The
  4125. default is None.
  4126. dx : scalar, optional
  4127. The spacing between sample points when `x` is None. The default is 1.
  4128. axis : int, optional
  4129. The axis along which to integrate.
  4130. Returns
  4131. -------
  4132. trapezoid : float or ndarray
  4133. Definite integral of `y` = n-dimensional array as approximated along
  4134. a single axis by the trapezoidal rule. If `y` is a 1-dimensional array,
  4135. then the result is a float. If `n` is greater than 1, then the result
  4136. is an `n`-1 dimensional array.
  4137. See Also
  4138. --------
  4139. sum, cumsum
  4140. Notes
  4141. -----
  4142. Image [2]_ illustrates trapezoidal rule -- y-axis locations of points
  4143. will be taken from `y` array, by default x-axis distances between
  4144. points will be 1.0, alternatively they can be provided with `x` array
  4145. or with `dx` scalar. Return value will be equal to combined area under
  4146. the red lines.
  4147. References
  4148. ----------
  4149. .. [1] Wikipedia page: https://en.wikipedia.org/wiki/Trapezoidal_rule
  4150. .. [2] Illustration image:
  4151. https://en.wikipedia.org/wiki/File:Composite_trapezoidal_rule_illustration.png
  4152. Examples
  4153. --------
  4154. >>> import numpy as np
  4155. Use the trapezoidal rule on evenly spaced points:
  4156. >>> np.trapezoid([1, 2, 3])
  4157. 4.0
  4158. The spacing between sample points can be selected by either the
  4159. ``x`` or ``dx`` arguments:
  4160. >>> np.trapezoid([1, 2, 3], x=[4, 6, 8])
  4161. 8.0
  4162. >>> np.trapezoid([1, 2, 3], dx=2)
  4163. 8.0
  4164. Using a decreasing ``x`` corresponds to integrating in reverse:
  4165. >>> np.trapezoid([1, 2, 3], x=[8, 6, 4])
  4166. -8.0
  4167. More generally ``x`` is used to integrate along a parametric curve. We can
  4168. estimate the integral :math:`\int_0^1 x^2 = 1/3` using:
  4169. >>> x = np.linspace(0, 1, num=50)
  4170. >>> y = x**2
  4171. >>> np.trapezoid(y, x)
  4172. 0.33340274885464394
  4173. Or estimate the area of a circle, noting we repeat the sample which closes
  4174. the curve:
  4175. >>> theta = np.linspace(0, 2 * np.pi, num=1000, endpoint=True)
  4176. >>> np.trapezoid(np.cos(theta), x=np.sin(theta))
  4177. 3.141571941375841
  4178. ``np.trapezoid`` can be applied along a specified axis to do multiple
  4179. computations in one call:
  4180. >>> a = np.arange(6).reshape(2, 3)
  4181. >>> a
  4182. array([[0, 1, 2],
  4183. [3, 4, 5]])
  4184. >>> np.trapezoid(a, axis=0)
  4185. array([1.5, 2.5, 3.5])
  4186. >>> np.trapezoid(a, axis=1)
  4187. array([2., 8.])
  4188. """
  4189. y = asanyarray(y)
  4190. if x is None:
  4191. d = dx
  4192. else:
  4193. x = asanyarray(x)
  4194. if x.ndim == 1:
  4195. d = diff(x)
  4196. # reshape to correct shape
  4197. shape = [1] * y.ndim
  4198. shape[axis] = d.shape[0]
  4199. d = d.reshape(shape)
  4200. else:
  4201. d = diff(x, axis=axis)
  4202. nd = y.ndim
  4203. slice1 = [slice(None)] * nd
  4204. slice2 = [slice(None)] * nd
  4205. slice1[axis] = slice(1, None)
  4206. slice2[axis] = slice(None, -1)
  4207. try:
  4208. ret = (d * (y[tuple(slice1)] + y[tuple(slice2)]) / 2.0).sum(axis)
  4209. except ValueError:
  4210. # Operations didn't work, cast to ndarray
  4211. d = np.asarray(d)
  4212. y = np.asarray(y)
  4213. ret = add.reduce(d * (y[tuple(slice1)] + y[tuple(slice2)]) / 2.0, axis)
  4214. return ret
  4215. def _meshgrid_dispatcher(*xi, copy=None, sparse=None, indexing=None):
  4216. return xi
  4217. # Based on scitools meshgrid
  4218. @array_function_dispatch(_meshgrid_dispatcher)
  4219. def meshgrid(*xi, copy=True, sparse=False, indexing='xy'):
  4220. """
  4221. Return a tuple of coordinate matrices from coordinate vectors.
  4222. Make N-D coordinate arrays for vectorized evaluations of
  4223. N-D scalar/vector fields over N-D grids, given
  4224. one-dimensional coordinate arrays x1, x2,..., xn.
  4225. Parameters
  4226. ----------
  4227. x1, x2,..., xn : array_like
  4228. 1-D arrays representing the coordinates of a grid.
  4229. indexing : {'xy', 'ij'}, optional
  4230. Cartesian ('xy', default) or matrix ('ij') indexing of output.
  4231. See Notes for more details.
  4232. sparse : bool, optional
  4233. If True the shape of the returned coordinate array for dimension *i*
  4234. is reduced from ``(N1, ..., Ni, ... Nn)`` to
  4235. ``(1, ..., 1, Ni, 1, ..., 1)``. These sparse coordinate grids are
  4236. intended to be used with :ref:`basics.broadcasting`. When all
  4237. coordinates are used in an expression, broadcasting still leads to a
  4238. fully-dimensonal result array.
  4239. Default is False.
  4240. copy : bool, optional
  4241. If False, a view into the original arrays are returned in order to
  4242. conserve memory. Default is True. Please note that
  4243. ``sparse=False, copy=False`` will likely return non-contiguous
  4244. arrays. Furthermore, more than one element of a broadcast array
  4245. may refer to a single memory location. If you need to write to the
  4246. arrays, make copies first.
  4247. Returns
  4248. -------
  4249. X1, X2,..., XN : tuple of ndarrays
  4250. For vectors `x1`, `x2`,..., `xn` with lengths ``Ni=len(xi)``,
  4251. returns ``(N1, N2, N3,..., Nn)`` shaped arrays if indexing='ij'
  4252. or ``(N2, N1, N3,..., Nn)`` shaped arrays if indexing='xy'
  4253. with the elements of `xi` repeated to fill the matrix along
  4254. the first dimension for `x1`, the second for `x2` and so on.
  4255. Notes
  4256. -----
  4257. This function supports both indexing conventions through the indexing
  4258. keyword argument. Giving the string 'ij' returns a meshgrid with
  4259. matrix indexing, while 'xy' returns a meshgrid with Cartesian indexing.
  4260. In the 2-D case with inputs of length M and N, the outputs are of shape
  4261. (N, M) for 'xy' indexing and (M, N) for 'ij' indexing. In the 3-D case
  4262. with inputs of length M, N and P, outputs are of shape (N, M, P) for
  4263. 'xy' indexing and (M, N, P) for 'ij' indexing. The difference is
  4264. illustrated by the following code snippet::
  4265. xv, yv = np.meshgrid(x, y, indexing='ij')
  4266. for i in range(nx):
  4267. for j in range(ny):
  4268. # treat xv[i,j], yv[i,j]
  4269. xv, yv = np.meshgrid(x, y, indexing='xy')
  4270. for i in range(nx):
  4271. for j in range(ny):
  4272. # treat xv[j,i], yv[j,i]
  4273. In the 1-D and 0-D case, the indexing and sparse keywords have no effect.
  4274. See Also
  4275. --------
  4276. mgrid : Construct a multi-dimensional "meshgrid" using indexing notation.
  4277. ogrid : Construct an open multi-dimensional "meshgrid" using indexing
  4278. notation.
  4279. :ref:`how-to-index`
  4280. Examples
  4281. --------
  4282. >>> import numpy as np
  4283. >>> nx, ny = (3, 2)
  4284. >>> x = np.linspace(0, 1, nx)
  4285. >>> y = np.linspace(0, 1, ny)
  4286. >>> xv, yv = np.meshgrid(x, y)
  4287. >>> xv
  4288. array([[0. , 0.5, 1. ],
  4289. [0. , 0.5, 1. ]])
  4290. >>> yv
  4291. array([[0., 0., 0.],
  4292. [1., 1., 1.]])
  4293. The result of `meshgrid` is a coordinate grid:
  4294. >>> import matplotlib.pyplot as plt
  4295. >>> plt.plot(xv, yv, marker='o', color='k', linestyle='none')
  4296. >>> plt.show()
  4297. You can create sparse output arrays to save memory and computation time.
  4298. >>> xv, yv = np.meshgrid(x, y, sparse=True)
  4299. >>> xv
  4300. array([[0. , 0.5, 1. ]])
  4301. >>> yv
  4302. array([[0.],
  4303. [1.]])
  4304. `meshgrid` is very useful to evaluate functions on a grid. If the
  4305. function depends on all coordinates, both dense and sparse outputs can be
  4306. used.
  4307. >>> x = np.linspace(-5, 5, 101)
  4308. >>> y = np.linspace(-5, 5, 101)
  4309. >>> # full coordinate arrays
  4310. >>> xx, yy = np.meshgrid(x, y)
  4311. >>> zz = np.sqrt(xx**2 + yy**2)
  4312. >>> xx.shape, yy.shape, zz.shape
  4313. ((101, 101), (101, 101), (101, 101))
  4314. >>> # sparse coordinate arrays
  4315. >>> xs, ys = np.meshgrid(x, y, sparse=True)
  4316. >>> zs = np.sqrt(xs**2 + ys**2)
  4317. >>> xs.shape, ys.shape, zs.shape
  4318. ((1, 101), (101, 1), (101, 101))
  4319. >>> np.array_equal(zz, zs)
  4320. True
  4321. >>> h = plt.contourf(x, y, zs)
  4322. >>> plt.axis('scaled')
  4323. >>> plt.colorbar()
  4324. >>> plt.show()
  4325. """
  4326. ndim = len(xi)
  4327. if indexing not in ['xy', 'ij']:
  4328. raise ValueError(
  4329. "Valid values for `indexing` are 'xy' and 'ij'.")
  4330. s0 = (1,) * ndim
  4331. output = [np.asanyarray(x).reshape(s0[:i] + (-1,) + s0[i + 1:])
  4332. for i, x in enumerate(xi)]
  4333. if indexing == 'xy' and ndim > 1:
  4334. # switch first and second axis
  4335. output[0].shape = (1, -1) + s0[2:]
  4336. output[1].shape = (-1, 1) + s0[2:]
  4337. if not sparse:
  4338. # Return the full N-D matrix (not only the 1-D vector)
  4339. output = np.broadcast_arrays(*output, subok=True)
  4340. if copy:
  4341. output = tuple(x.copy() for x in output)
  4342. return output
  4343. def _delete_dispatcher(arr, obj, axis=None):
  4344. return (arr, obj)
  4345. @array_function_dispatch(_delete_dispatcher)
  4346. def delete(arr, obj, axis=None):
  4347. """
  4348. Return a new array with sub-arrays along an axis deleted. For a one
  4349. dimensional array, this returns those entries not returned by
  4350. `arr[obj]`.
  4351. Parameters
  4352. ----------
  4353. arr : array_like
  4354. Input array.
  4355. obj : slice, int, array-like of ints or bools
  4356. Indicate indices of sub-arrays to remove along the specified axis.
  4357. .. versionchanged:: 1.19.0
  4358. Boolean indices are now treated as a mask of elements to remove,
  4359. rather than being cast to the integers 0 and 1.
  4360. axis : int, optional
  4361. The axis along which to delete the subarray defined by `obj`.
  4362. If `axis` is None, `obj` is applied to the flattened array.
  4363. Returns
  4364. -------
  4365. out : ndarray
  4366. A copy of `arr` with the elements specified by `obj` removed. Note
  4367. that `delete` does not occur in-place. If `axis` is None, `out` is
  4368. a flattened array.
  4369. See Also
  4370. --------
  4371. insert : Insert elements into an array.
  4372. append : Append elements at the end of an array.
  4373. Notes
  4374. -----
  4375. Often it is preferable to use a boolean mask. For example:
  4376. >>> arr = np.arange(12) + 1
  4377. >>> mask = np.ones(len(arr), dtype=bool)
  4378. >>> mask[[0,2,4]] = False
  4379. >>> result = arr[mask,...]
  4380. Is equivalent to ``np.delete(arr, [0,2,4], axis=0)``, but allows further
  4381. use of `mask`.
  4382. Examples
  4383. --------
  4384. >>> import numpy as np
  4385. >>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
  4386. >>> arr
  4387. array([[ 1, 2, 3, 4],
  4388. [ 5, 6, 7, 8],
  4389. [ 9, 10, 11, 12]])
  4390. >>> np.delete(arr, 1, 0)
  4391. array([[ 1, 2, 3, 4],
  4392. [ 9, 10, 11, 12]])
  4393. >>> np.delete(arr, np.s_[::2], 1)
  4394. array([[ 2, 4],
  4395. [ 6, 8],
  4396. [10, 12]])
  4397. >>> np.delete(arr, [1,3,5], None)
  4398. array([ 1, 3, 5, 7, 8, 9, 10, 11, 12])
  4399. """
  4400. conv = _array_converter(arr)
  4401. arr, = conv.as_arrays(subok=False)
  4402. ndim = arr.ndim
  4403. arrorder = 'F' if arr.flags.fnc else 'C'
  4404. if axis is None:
  4405. if ndim != 1:
  4406. arr = arr.ravel()
  4407. # needed for np.matrix, which is still not 1d after being ravelled
  4408. ndim = arr.ndim
  4409. axis = ndim - 1
  4410. else:
  4411. axis = normalize_axis_index(axis, ndim)
  4412. slobj = [slice(None)] * ndim
  4413. N = arr.shape[axis]
  4414. newshape = list(arr.shape)
  4415. if isinstance(obj, slice):
  4416. start, stop, step = obj.indices(N)
  4417. xr = range(start, stop, step)
  4418. numtodel = len(xr)
  4419. if numtodel <= 0:
  4420. return conv.wrap(arr.copy(order=arrorder), to_scalar=False)
  4421. # Invert if step is negative:
  4422. if step < 0:
  4423. step = -step
  4424. start = xr[-1]
  4425. stop = xr[0] + 1
  4426. newshape[axis] -= numtodel
  4427. new = empty(newshape, arr.dtype, arrorder)
  4428. # copy initial chunk
  4429. if start == 0:
  4430. pass
  4431. else:
  4432. slobj[axis] = slice(None, start)
  4433. new[tuple(slobj)] = arr[tuple(slobj)]
  4434. # copy end chunk
  4435. if stop == N:
  4436. pass
  4437. else:
  4438. slobj[axis] = slice(stop - numtodel, None)
  4439. slobj2 = [slice(None)] * ndim
  4440. slobj2[axis] = slice(stop, None)
  4441. new[tuple(slobj)] = arr[tuple(slobj2)]
  4442. # copy middle pieces
  4443. if step == 1:
  4444. pass
  4445. else: # use array indexing.
  4446. keep = ones(stop - start, dtype=bool)
  4447. keep[:stop - start:step] = False
  4448. slobj[axis] = slice(start, stop - numtodel)
  4449. slobj2 = [slice(None)] * ndim
  4450. slobj2[axis] = slice(start, stop)
  4451. arr = arr[tuple(slobj2)]
  4452. slobj2[axis] = keep
  4453. new[tuple(slobj)] = arr[tuple(slobj2)]
  4454. return conv.wrap(new, to_scalar=False)
  4455. if isinstance(obj, (int, integer)) and not isinstance(obj, bool):
  4456. single_value = True
  4457. else:
  4458. single_value = False
  4459. _obj = obj
  4460. obj = np.asarray(obj)
  4461. # `size == 0` to allow empty lists similar to indexing, but (as there)
  4462. # is really too generic:
  4463. if obj.size == 0 and not isinstance(_obj, np.ndarray):
  4464. obj = obj.astype(intp)
  4465. elif obj.size == 1 and obj.dtype.kind in "ui":
  4466. # For a size 1 integer array we can use the single-value path
  4467. # (most dtypes, except boolean, should just fail later).
  4468. obj = obj.item()
  4469. single_value = True
  4470. if single_value:
  4471. # optimization for a single value
  4472. if (obj < -N or obj >= N):
  4473. raise IndexError(
  4474. f"index {obj} is out of bounds for axis {axis} with "
  4475. f"size {N}")
  4476. if (obj < 0):
  4477. obj += N
  4478. newshape[axis] -= 1
  4479. new = empty(newshape, arr.dtype, arrorder)
  4480. slobj[axis] = slice(None, obj)
  4481. new[tuple(slobj)] = arr[tuple(slobj)]
  4482. slobj[axis] = slice(obj, None)
  4483. slobj2 = [slice(None)] * ndim
  4484. slobj2[axis] = slice(obj + 1, None)
  4485. new[tuple(slobj)] = arr[tuple(slobj2)]
  4486. else:
  4487. if obj.dtype == bool:
  4488. if obj.shape != (N,):
  4489. raise ValueError('boolean array argument obj to delete '
  4490. 'must be one dimensional and match the axis '
  4491. f'length of {N}')
  4492. # optimization, the other branch is slower
  4493. keep = ~obj
  4494. else:
  4495. keep = ones(N, dtype=bool)
  4496. keep[obj,] = False
  4497. slobj[axis] = keep
  4498. new = arr[tuple(slobj)]
  4499. return conv.wrap(new, to_scalar=False)
  4500. def _insert_dispatcher(arr, obj, values, axis=None):
  4501. return (arr, obj, values)
  4502. @array_function_dispatch(_insert_dispatcher)
  4503. def insert(arr, obj, values, axis=None):
  4504. """
  4505. Insert values along the given axis before the given indices.
  4506. Parameters
  4507. ----------
  4508. arr : array_like
  4509. Input array.
  4510. obj : slice, int, array-like of ints or bools
  4511. Object that defines the index or indices before which `values` is
  4512. inserted.
  4513. .. versionchanged:: 2.1.2
  4514. Boolean indices are now treated as a mask of elements to insert,
  4515. rather than being cast to the integers 0 and 1.
  4516. Support for multiple insertions when `obj` is a single scalar or a
  4517. sequence with one element (similar to calling insert multiple
  4518. times).
  4519. values : array_like
  4520. Values to insert into `arr`. If the type of `values` is different
  4521. from that of `arr`, `values` is converted to the type of `arr`.
  4522. `values` should be shaped so that ``arr[...,obj,...] = values``
  4523. is legal.
  4524. axis : int, optional
  4525. Axis along which to insert `values`. If `axis` is None then `arr`
  4526. is flattened first.
  4527. Returns
  4528. -------
  4529. out : ndarray
  4530. A copy of `arr` with `values` inserted. Note that `insert`
  4531. does not occur in-place: a new array is returned. If
  4532. `axis` is None, `out` is a flattened array.
  4533. See Also
  4534. --------
  4535. append : Append elements at the end of an array.
  4536. concatenate : Join a sequence of arrays along an existing axis.
  4537. delete : Delete elements from an array.
  4538. Notes
  4539. -----
  4540. Note that for higher dimensional inserts ``obj=0`` behaves very different
  4541. from ``obj=[0]`` just like ``arr[:,0,:] = values`` is different from
  4542. ``arr[:,[0],:] = values``. This is because of the difference between basic
  4543. and advanced :ref:`indexing <basics.indexing>`.
  4544. Examples
  4545. --------
  4546. >>> import numpy as np
  4547. >>> a = np.arange(6).reshape(3, 2)
  4548. >>> a
  4549. array([[0, 1],
  4550. [2, 3],
  4551. [4, 5]])
  4552. >>> np.insert(a, 1, 6)
  4553. array([0, 6, 1, 2, 3, 4, 5])
  4554. >>> np.insert(a, 1, 6, axis=1)
  4555. array([[0, 6, 1],
  4556. [2, 6, 3],
  4557. [4, 6, 5]])
  4558. Difference between sequence and scalars,
  4559. showing how ``obj=[1]`` behaves different from ``obj=1``:
  4560. >>> np.insert(a, [1], [[7],[8],[9]], axis=1)
  4561. array([[0, 7, 1],
  4562. [2, 8, 3],
  4563. [4, 9, 5]])
  4564. >>> np.insert(a, 1, [[7],[8],[9]], axis=1)
  4565. array([[0, 7, 8, 9, 1],
  4566. [2, 7, 8, 9, 3],
  4567. [4, 7, 8, 9, 5]])
  4568. >>> np.array_equal(np.insert(a, 1, [7, 8, 9], axis=1),
  4569. ... np.insert(a, [1], [[7],[8],[9]], axis=1))
  4570. True
  4571. >>> b = a.flatten()
  4572. >>> b
  4573. array([0, 1, 2, 3, 4, 5])
  4574. >>> np.insert(b, [2, 2], [6, 7])
  4575. array([0, 1, 6, 7, 2, 3, 4, 5])
  4576. >>> np.insert(b, slice(2, 4), [7, 8])
  4577. array([0, 1, 7, 2, 8, 3, 4, 5])
  4578. >>> np.insert(b, [2, 2], [7.13, False]) # type casting
  4579. array([0, 1, 7, 0, 2, 3, 4, 5])
  4580. >>> x = np.arange(8).reshape(2, 4)
  4581. >>> idx = (1, 3)
  4582. >>> np.insert(x, idx, 999, axis=1)
  4583. array([[ 0, 999, 1, 2, 999, 3],
  4584. [ 4, 999, 5, 6, 999, 7]])
  4585. """
  4586. conv = _array_converter(arr)
  4587. arr, = conv.as_arrays(subok=False)
  4588. ndim = arr.ndim
  4589. arrorder = 'F' if arr.flags.fnc else 'C'
  4590. if axis is None:
  4591. if ndim != 1:
  4592. arr = arr.ravel()
  4593. # needed for np.matrix, which is still not 1d after being ravelled
  4594. ndim = arr.ndim
  4595. axis = ndim - 1
  4596. else:
  4597. axis = normalize_axis_index(axis, ndim)
  4598. slobj = [slice(None)] * ndim
  4599. N = arr.shape[axis]
  4600. newshape = list(arr.shape)
  4601. if isinstance(obj, slice):
  4602. # turn it into a range object
  4603. indices = arange(*obj.indices(N), dtype=intp)
  4604. else:
  4605. # need to copy obj, because indices will be changed in-place
  4606. indices = np.array(obj)
  4607. if indices.dtype == bool:
  4608. if obj.ndim != 1:
  4609. raise ValueError('boolean array argument obj to insert '
  4610. 'must be one dimensional')
  4611. indices = np.flatnonzero(obj)
  4612. elif indices.ndim > 1:
  4613. raise ValueError(
  4614. "index array argument obj to insert must be one dimensional "
  4615. "or scalar")
  4616. if indices.size == 1:
  4617. index = indices.item()
  4618. if index < -N or index > N:
  4619. raise IndexError(f"index {obj} is out of bounds for axis {axis} "
  4620. f"with size {N}")
  4621. if (index < 0):
  4622. index += N
  4623. # There are some object array corner cases here, but we cannot avoid
  4624. # that:
  4625. values = array(values, copy=None, ndmin=arr.ndim, dtype=arr.dtype)
  4626. if indices.ndim == 0:
  4627. # broadcasting is very different here, since a[:,0,:] = ... behaves
  4628. # very different from a[:,[0],:] = ...! This changes values so that
  4629. # it works likes the second case. (here a[:,0:1,:])
  4630. values = np.moveaxis(values, 0, axis)
  4631. numnew = values.shape[axis]
  4632. newshape[axis] += numnew
  4633. new = empty(newshape, arr.dtype, arrorder)
  4634. slobj[axis] = slice(None, index)
  4635. new[tuple(slobj)] = arr[tuple(slobj)]
  4636. slobj[axis] = slice(index, index + numnew)
  4637. new[tuple(slobj)] = values
  4638. slobj[axis] = slice(index + numnew, None)
  4639. slobj2 = [slice(None)] * ndim
  4640. slobj2[axis] = slice(index, None)
  4641. new[tuple(slobj)] = arr[tuple(slobj2)]
  4642. return conv.wrap(new, to_scalar=False)
  4643. elif indices.size == 0 and not isinstance(obj, np.ndarray):
  4644. # Can safely cast the empty list to intp
  4645. indices = indices.astype(intp)
  4646. indices[indices < 0] += N
  4647. numnew = len(indices)
  4648. order = indices.argsort(kind='mergesort') # stable sort
  4649. indices[order] += np.arange(numnew)
  4650. newshape[axis] += numnew
  4651. old_mask = ones(newshape[axis], dtype=bool)
  4652. old_mask[indices] = False
  4653. new = empty(newshape, arr.dtype, arrorder)
  4654. slobj2 = [slice(None)] * ndim
  4655. slobj[axis] = indices
  4656. slobj2[axis] = old_mask
  4657. new[tuple(slobj)] = values
  4658. new[tuple(slobj2)] = arr
  4659. return conv.wrap(new, to_scalar=False)
  4660. def _append_dispatcher(arr, values, axis=None):
  4661. return (arr, values)
  4662. @array_function_dispatch(_append_dispatcher)
  4663. def append(arr, values, axis=None):
  4664. """
  4665. Append values to the end of an array.
  4666. Parameters
  4667. ----------
  4668. arr : array_like
  4669. Values are appended to a copy of this array.
  4670. values : array_like
  4671. These values are appended to a copy of `arr`. It must be of the
  4672. correct shape (the same shape as `arr`, excluding `axis`). If
  4673. `axis` is not specified, `values` can be any shape and will be
  4674. flattened before use.
  4675. axis : int, optional
  4676. The axis along which `values` are appended. If `axis` is not
  4677. given, both `arr` and `values` are flattened before use.
  4678. Returns
  4679. -------
  4680. append : ndarray
  4681. A copy of `arr` with `values` appended to `axis`. Note that
  4682. `append` does not occur in-place: a new array is allocated and
  4683. filled. If `axis` is None, `out` is a flattened array.
  4684. See Also
  4685. --------
  4686. insert : Insert elements into an array.
  4687. delete : Delete elements from an array.
  4688. Examples
  4689. --------
  4690. >>> import numpy as np
  4691. >>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
  4692. array([1, 2, 3, ..., 7, 8, 9])
  4693. When `axis` is specified, `values` must have the correct shape.
  4694. >>> np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0)
  4695. array([[1, 2, 3],
  4696. [4, 5, 6],
  4697. [7, 8, 9]])
  4698. >>> np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0)
  4699. Traceback (most recent call last):
  4700. ...
  4701. ValueError: all the input arrays must have same number of dimensions, but
  4702. the array at index 0 has 2 dimension(s) and the array at index 1 has 1
  4703. dimension(s)
  4704. >>> a = np.array([1, 2], dtype=int)
  4705. >>> c = np.append(a, [])
  4706. >>> c
  4707. array([1., 2.])
  4708. >>> c.dtype
  4709. float64
  4710. Default dtype for empty ndarrays is `float64` thus making the output of dtype
  4711. `float64` when appended with dtype `int64`
  4712. """
  4713. arr = asanyarray(arr)
  4714. if axis is None:
  4715. if arr.ndim != 1:
  4716. arr = arr.ravel()
  4717. values = ravel(values)
  4718. axis = arr.ndim - 1
  4719. return concatenate((arr, values), axis=axis)
  4720. def _digitize_dispatcher(x, bins, right=None):
  4721. return (x, bins)
  4722. @array_function_dispatch(_digitize_dispatcher)
  4723. def digitize(x, bins, right=False):
  4724. """
  4725. Return the indices of the bins to which each value in input array belongs.
  4726. ========= ============= ============================
  4727. `right` order of bins returned index `i` satisfies
  4728. ========= ============= ============================
  4729. ``False`` increasing ``bins[i-1] <= x < bins[i]``
  4730. ``True`` increasing ``bins[i-1] < x <= bins[i]``
  4731. ``False`` decreasing ``bins[i-1] > x >= bins[i]``
  4732. ``True`` decreasing ``bins[i-1] >= x > bins[i]``
  4733. ========= ============= ============================
  4734. If values in `x` are beyond the bounds of `bins`, 0 or ``len(bins)`` is
  4735. returned as appropriate.
  4736. Parameters
  4737. ----------
  4738. x : array_like
  4739. Input array to be binned. Prior to NumPy 1.10.0, this array had to
  4740. be 1-dimensional, but can now have any shape.
  4741. bins : array_like
  4742. Array of bins. It has to be 1-dimensional and monotonic.
  4743. right : bool, optional
  4744. Indicating whether the intervals include the right or the left bin
  4745. edge. Default behavior is (right==False) indicating that the interval
  4746. does not include the right edge. The left bin end is open in this
  4747. case, i.e., bins[i-1] <= x < bins[i] is the default behavior for
  4748. monotonically increasing bins.
  4749. Returns
  4750. -------
  4751. indices : ndarray of ints
  4752. Output array of indices, of same shape as `x`.
  4753. Raises
  4754. ------
  4755. ValueError
  4756. If `bins` is not monotonic.
  4757. TypeError
  4758. If the type of the input is complex.
  4759. See Also
  4760. --------
  4761. bincount, histogram, unique, searchsorted
  4762. Notes
  4763. -----
  4764. If values in `x` are such that they fall outside the bin range,
  4765. attempting to index `bins` with the indices that `digitize` returns
  4766. will result in an IndexError.
  4767. .. versionadded:: 1.10.0
  4768. `numpy.digitize` is implemented in terms of `numpy.searchsorted`.
  4769. This means that a binary search is used to bin the values, which scales
  4770. much better for larger number of bins than the previous linear search.
  4771. It also removes the requirement for the input array to be 1-dimensional.
  4772. For monotonically *increasing* `bins`, the following are equivalent::
  4773. np.digitize(x, bins, right=True)
  4774. np.searchsorted(bins, x, side='left')
  4775. Note that as the order of the arguments are reversed, the side must be too.
  4776. The `searchsorted` call is marginally faster, as it does not do any
  4777. monotonicity checks. Perhaps more importantly, it supports all dtypes.
  4778. Examples
  4779. --------
  4780. >>> import numpy as np
  4781. >>> x = np.array([0.2, 6.4, 3.0, 1.6])
  4782. >>> bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0])
  4783. >>> inds = np.digitize(x, bins)
  4784. >>> inds
  4785. array([1, 4, 3, 2])
  4786. >>> for n in range(x.size):
  4787. ... print(bins[inds[n]-1], "<=", x[n], "<", bins[inds[n]])
  4788. ...
  4789. 0.0 <= 0.2 < 1.0
  4790. 4.0 <= 6.4 < 10.0
  4791. 2.5 <= 3.0 < 4.0
  4792. 1.0 <= 1.6 < 2.5
  4793. >>> x = np.array([1.2, 10.0, 12.4, 15.5, 20.])
  4794. >>> bins = np.array([0, 5, 10, 15, 20])
  4795. >>> np.digitize(x,bins,right=True)
  4796. array([1, 2, 3, 4, 4])
  4797. >>> np.digitize(x,bins,right=False)
  4798. array([1, 3, 3, 4, 5])
  4799. """
  4800. x = _nx.asarray(x)
  4801. bins = _nx.asarray(bins)
  4802. # here for compatibility, searchsorted below is happy to take this
  4803. if np.issubdtype(x.dtype, _nx.complexfloating):
  4804. raise TypeError("x may not be complex")
  4805. mono = _monotonicity(bins)
  4806. if mono == 0:
  4807. raise ValueError("bins must be monotonically increasing or decreasing")
  4808. # this is backwards because the arguments below are swapped
  4809. side = 'left' if right else 'right'
  4810. if mono == -1:
  4811. # reverse the bins, and invert the results
  4812. return len(bins) - _nx.searchsorted(bins[::-1], x, side=side)
  4813. else:
  4814. return _nx.searchsorted(bins, x, side=side)