function_base.py 181 KB

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