讲义python中英文汇总data science programming_第1页
讲义python中英文汇总data science programming_第2页
讲义python中英文汇总data science programming_第3页
讲义python中英文汇总data science programming_第4页
讲义python中英文汇总data science programming_第5页
已阅读5页,还剩88页未读, 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

TableofDataScienceProgramminginPython-DataDataMungingandDataScienceProgramminginPython-GrouandAggregatingGrouandqueryingdataGrouandaggregatingDataScienceProgramminginPython-DataDataScienceProgramminginPython-TimeSeriesIntroduction-DataScienceProgramminginTheaimofthisbookistoshowhowtoapplydataysisprinciplestoapracticalusecasescenariousingPythonasthedataysislanguage.We’llgoonthisjourneybylookingatthethedataworkflowfrommungingtogroudatatovisualizingandalsoincludesometime-seriesysisaswell.Theformatincludesaskingquestionsofthedataandshowingtheprogrammingstepsneededtoanswerthequestion.Bytheendofreadingthisbook,youwillbeabletoapplythesetechniquestoyourowndata.AbouttheThisbookiswritteninali teprogrammingstylewheretext,code,andoutputarepresentedtogether.Thiswill izeyourlearningandunderstandingofcodeandthedataysisworkflow.ThebookteachesthetypeofinctivecodinganditiveysisthatisessentialtobesuccessfulindatascienceCodingInthecodesnippets,abackslashcharacter(\)meansthatthesamelineofcodeiswrappedtothenextlineinthebook.Youdonotneedtotypethischaracterintoaninterpreter.UseaREPL(/wiki/Read–eval–print_loop)tohaveaninctiveenvironmentwhereyoucanwritecodeandseetheresultingoutput.Trythemethodsyoulearninthisbookonyourowndatatoreinlearning.UseaPythoninterpretertocodeandyourfavoriteeditortotakenotes.欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍DataScienceProgramminginPython-DataBayAreaBikeSharecommencedit’spilotphaseofoperationintheSanFranciscobayareainAugust2013withnstoexpand.ItisthefirstbikesharingschemeinCalifornia.Asitismeantforshorttrips,thebikesshouldbereturnedtoadockinthirtyminutesorlessoranadditionalfeewouldbeincurredaccordingtothewebsite.Therearetwotypesofmemberships:customerandsubscriber.Asubscriberisanannualmembershipwhileacustomerisdefinedassomeoneusingeitherthetwenty-fourhourorthreedaypasses.Currently(Sept2014),itcostsninedollarsfortwenty-fourhours,twenty-twodollarsforthreedays,andeight-eightdollarsfortheyear.Overtimefeesarefourdollarsforanextrathirtyminutesandsevendollarsforeachthirtyminutesafterthat.Dataonthefirstsixmonthsofoperationswerereleasedaspartofadatachallenge.Thedataincludedthreefilesfortriphistory,weatherinformation,anddockavailability.Themergeddatawasusedforthefollowing DataMungingandFirst,we’llreadinthedataandinspectthedatacolumnsanddatatypesandthinkaboutwhatquestionswewanttoaskourdataandwhatthingsareweinterestedinlearningaboutthedata.BecuriousandempatheticinthinkingaboutwhatthevariousstakeholdersincludingtheCity,thecustomers,andotherinterestedpeoplewouldbeinterestedingleaningbykeecivicfiscal,civic,andsocialgoalsinmind.Inadditiontothat,therewillbequiteabitofcleaninganddatacarpentryneededtogetthedataintoaformatusefulfor ThedatasetcomesfromthreecsvfilesfromtheBayAreaBikesharedatachallenge.WemergedthedatainRaswestartedasimilar ysistherebutreallywantedtouseIPythonandthesuperbtimeseriesfunctionalityinPandas.Optionally,onecancacheorlogthecodeinIPythonwiththefollowingtwo%load_extActivatingauto-logging.Currentsessionstateplusfutureinputsaved.Filename:Mode:Outputlogging:FalseRawinputlog:FalseTimestam:FalseState:activeImportthelibrariesthatareimportnumpyasnpimportpandasaspd欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍fromdatetimeimportdatetime,%matplotlibinlineimportseabornassnsfromggplotimport*printpd. printnp. Readthedmerge4=pd.read_csv('finalmerge.csv',parse_dates=['Start.Date'])Formatcolumndmerge4.columns=dmerge4.columns.map(lambdax:x.rece('.','').lower())dmerge4['zip_name']=dmerge4.zip.rece({94107:'SanFrancisco','SanJose',94301:'PaloAlto',94041:'MountainView',94063:'RedwoodInspectInspectingthedatatypesandvariablesarethefirstthingsthatshouldbedoneindatamunging.And,we’lldoitagainafter ngourdatacleaningandformattingoperations.Noticethattheenddateandstartdatearenotcurrentlyindatetimeformatsolet’sremedythatandtakecareofsomeotherthingsforthequestionsrelatedtotime-series.lubstartdate:startstation:tripid:duration:startdate:startterminal:enddate:endstation:endterminal:bike:subscriptiontype:zipcode:date:max_temperature_f:mean_temperature_f:min_temperaturef:max_dew_point_f:>meandew_point_f:>min_dewpoint_f:>max_humidity:>mean_humidity:>min_humidity:>max_sea_level_pressure_in:>mean_sea_level_pressure_in:>min_sea_level_pressure_in: : : :>max_wind_speed_mph:>mean_wind_speed_mph:欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍>max_gust_speed_mph:>precipitation_in:>cloud_cover:>events:>wind_dir_degrees:>zip:>landmark:>station_id:>name:>lat:>long:>dockcount:>installation:>zip_name:>dtype:WorkingwithdatesandParsedatecolumnsintodatetimedmerge4['enddate']=pd.to_datetime(dmerge4['enddate'])dmerge4['lubstartdate']=pd.to_datetime(dmerge4['lubstartdate'])Setdatetimeindexdmerge4.set_index('startdate',ince=True,drop=False,dmerge4['day']=dmerge4.index.daydmerge4['hour']=dmerge4.index.hourdmerge4['month']=dmerge4.index.monthHavingthedateandtimeisgreat.Howeverforthe ysis,itwouldbereallyusefultogrouptimeintosubjectiveusefulcategoriesthatwouldbeusefulforunderstandingusagealittlebitbetter.Wethinkwewouldbereallyinterestedtolearnaboutthedifferencesandsimilaritiesinusageanddurationpatternsbetweenthemorningcommuteandeveningrushperiod.Itwouldalsobeinterestingtoknowtheprofileandusageofusersinthemiddleofthedayaswellasatnightandinwhatwerefertoastheweehours.So,We’vedecidedtosplitthetimeintothefollowingsegments.morning:evening:midday:night:weehours:dmerge4['timeofday']=dmerge4.hour.rece({15:'evening',17:'evening',20:'night',19:'evening',12:'mid_day',14:'mid_day',13:'mid_day',9:'morning',22:'night',11:'mid_day',18:'evening',16:'evening',10:'morning',21:'night',6:'morning',8:'morning',7:'morning',1:'wee_hours',2:'wee_hours',3:'wee_hours',0:'night',5:'morning',Differencebetweenenddateandstartdmerge4['diff']=dmerge4.apply(lambdax:x['enddate']-x['startdate'],Differencebetweenendhourandstartdmerge4['hourdiff']=dmerge4.apply(lambdax:x['enddate'].hour-.hour,Now,oneofthemostimportantvariablesinthedatasetisduration.Weareveryinterestedinunderstandinghowlongacustomerorsubscriberhasabicycle.Durationisdefinedas欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍thenumberofsecondsfromtakingabicyclefromadockandreturningittoadock.oftheinterestingquestionssuchasbreakdownofdurationbetweencustomerandsubscriberbylandmarkorbytimeofdayarerelevanttobothcustomerendusersandcivicasitaffectshowmanybikeswillbeavailableandwherewilltheybeneeded.Let’stransformdurationintominutesandthenalsodefineabinaryvariabletocomparetripsunderthirtyminutesandlesstotripsoverthirtyminutes.Durationisinsecondssolet’sconvertittodmerge4['durationminutes']=Convertdurationtoaninteger#roundandconverttodmerge4["duration_i"]=Durationasfloattypetotwodecimal#roundtotwodecimalspacesandkeeepasdmerge4["duration_f"]=We’llwriteafunctionthatmaps‘underthirty’toallvaluesunder30(minutes)and‘overthirty’toallvaluesover30(minutes)defifx<=30:return'in_thirty'elif31<=x:return'over_thirty'else:return'None'dmerge4["thirtymin"]=dmerge4['duration_i'].map(isitthirty) 欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍 Length:54,dtype:欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍DataScienceProgramminginPython-GrouandAggregatingDataGrouandqueryingNowwewillsliceanddiceourdatasetusinggrou,aggregationfunctions,summarystatistics,andvariousqueryingandindexingoperations.Toreallyunderstandthebikesharingdata,weneedtolookatnaturalgroussuchasstartinglandmark,subscriptiontype,andthetimeofdayofthestartofabicycletrip.Theworkdoneinthissectionwillsetupsomebeautifulandinformativeplotsinthenextsectionofthe ysis.Mostofthetablesbelowhaveplottingrepresentationsinthevisualizationsection.Grouandaggregatingorreducingdataandobtainingsummarymeasuresandstatisticsistheheartofdata ysisandworktogetherwiththevisualizationprocessincludingthoseofanexploratorynature.Thei tiveprocessofdatadiscoveryatbestleadstonewquestionsandinsightsbeingdiscoveredwhichinformandrefinethedatacarpentryprocess.Thisiswherethein ctivereplexcels.Thinkofitasacircularinquisitiveprocessofdatamunging,grouandaggregating,andvisualizing.WewillexploreusingSQLinspiredmethodsofgrouoperationsinPandasandtheuseofmult-indexesandhierarchicalindexing.importnumpyasnpimportpandasaspdfromdatetimeimportdatetime,%matplotlibinlineimportseabornassnsfromggplotimport*printpd. printnp. %load_extGrouandIt’stimetoexplorevariouswaysofgrouandaggregatingdata.Let’sreviewsometerminology.Dockcountreferstothenumberofdocksinstalled,subscriptiontypeiseithercustomerorsubscriber,andlandmarkreferstoanareacoveredbySanFrancisco,SanJose,RedwoodCity,orPaloAlto.Inthedatamungingsection,wecreatedacolumnindicatingwhetherthedurationofasegmentofabicycletripwasunderthirtyminutesoroverthirtyminutesasthisistheallottedtimebeforebeingchargedovertimefees.So,whatpercentageoftotaltripswereoverthirtyminutes?Whatpercentageoftotaltripswereoverthirty dtype:int64欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍Only6%oftotalrideshadadurationoverthirtyminutessoitlookslikepeopleareaboutreturningbikesontime.Or,werethey?Whenwelookatthestatisticsbasedonlandmarkandsubscribertype,wewillgetabetterpictureofwhoandwherehavelongerdurationtimes.HowdoesaveragedurationvarybytimeofdayfortripsbothunderthirtyminutesandoverthirtyHereisanotherwaytocheckonhowmanyobservationsoutofthetotalhadtripdurationsoverthirtyminutes.HowmanytripsbeganandendedatthesameOnly6878out144015observationsor4.8%wereroundtripssoitwouldseemthatpeoplearereallyusingthebicyclestogetfrompointAtopointBlen(dmerge4[dmerge4['startstation']==dmerge4['endstation']])Let’slookataveragedurationbysubscriptiontype.Customersaredefinedhashavingeitheratwenty-fourhourorthreedaypasswhileSubscribersaredefinedashavinganannualpass.Averagedurationsforsubscribersisalmosttenminuteswhileitissixtyminutesforcustomers.Perhapscustomersarerecreationalriderswhotakelongertripswhilesubscribersareshorterdistancecommuters.We’lllearnmorewhenwesliceanddicethedatalater.Whatisaveragedurationbysubscriptiondmerge4.groupby(['subscriptiontype']).agg({'duration_f':欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍Howmanyobservationsinthedatasetbelongtoeachsubscription dtype:int64Whatlandmarkoccursthemostfrequentlyamongsubscriptiontypeofdmerge4[dmerge4['subscriptiontype']SanSanMountainPaloRedwooddtype:So,we’velearnedthatthemajorityoftotalsubscribersareinSanFranciscoandtakeveryshorttrips.WhatarethetopfivestartHarryBridges za(FerryBuilding)EmbarcaderoatSansomeSanFranciscoCaltrain(Townsendat4th)Marketat4thPowellStreetName:duration,dtype:WhatlandmarkhasthehighesttotalThetablebelowindicatesthatSanFranciscohadthehighesttotaldurationbutkeepinmindthatthemajorityoftotalobservationsarealsoinSanFranciscosoofcoursethetotalwouldbehigher.Usingsummarystatisticswillgiveabetterideaofbicyclingbehaviorthanlookingattotalvalues.Although,forsomepurposes,itisgoodtoknowthetotalaswell.Forexample,bicyclewearandtearbasedontotaldurations.%%cachestation.pklstationdataWhatpercentageoftotalbicycledocksarealocatedineach欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍54%ofalldocksarelocatedinSanFranciscosothismaypartiallyexinwhydurationandbicycletripstakeninSanFranciscoarehigherandwhydurationsarehigherinPaloAltowhereonly6%ofthedocksarelocated.Whatarethedockcountsbylandmarkandstation欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍WhatarethetotalnumberofbicycletripsbyWhenlookingatnumberofbicycletripsbasedonlandmark,weseethat129,853or90%oftotalobservationsinthedatasetwereinSanFrancisco.6%oftotalobservationswereinSanJosewhileonly1%wereinPaloAlto.WhatareaveragedurationsbyLookingataveragedurationbylandmark,SanFranciscohasthelowestdurationfollowedbySanJosewhilePaloAltohadthehighestduration.WecanhypothesizethatthismaybepartlyduetofewerdocksinPaloAltoorgreaterdistancestotravelaswellasthesubscriptionprofileofPaloAltobicycleusers.Whatareaveragedurationanddockcountbylandmarkandtimeofdayforfirstsixmonthsofthebikesharetimelandmark=dmerge4.groupby(['timeofday','landmark']).agg({'dockcount':np.m\ean,'duration_f':np.mean,}).reset_index()欢迎加入欢迎加入 Python编程学习交WhereandatwhenwasthehighestaverageThehighestdockcountwasinRedwoodCityintheWhatarethetopfivelongestaveragedurationgroupedbytimeofdayandThehighestaveragedurationwasinPaloAltointhewee6duration_f,dtype:Whataretheaveragedockcountanddurationsbytimeofdayandlandmarkforthefirstsixmonthsofthebikesharesystem?Pandasproducesnicereadablemulti-indexnp.round(dmerge4.groupby(['timeofday','landmark']).agg({'dockcount':np.mean,'\duration_i':np.mean,}))欢迎加入 Python编程学习交 ,群里免费提供500+本Python书籍Grouandaggregatingtime-seriesLet’sobtainmeanvaluesbydayofthemonthbycreatingagroupedobjectfromthedaycomponentofthetimestampandapplyinganaggregationfunction.Wewanttoknowifthereishigherdemandortripdurationatcertaindaysofthemonth.day_means=dmerge4.groupby('day').aggregate(np.mean)欢迎加入欢迎加入 Python编程学习交WecanalsocreateagroupedobjectusingPandasgrouper.WecanactuallyspecifyaresamplingandagrouusingtheGrouper.So,inthiscaseIwantthefrequencytobemonthlyandgroupbylandmarkandsubscriptiontype.three_grouper=欢迎加入欢迎加入 Python编程学习交Byunstackingthedatacreatedinthegrouperobject,wenowhavedurationbycustomerandsubscriberbylandmark.Thisisanicewaytopresentthedataintabularformat.three_grouper_unstack=dmerge4.groupby([pd.Grouper(freq='M',key='startdate'),\three_grouper_unstack=dmerge4.groupby([pd.Grouper(freq='M',key='startdate'),"l\欢迎加入欢迎加入 Python编程学习交Averagedurationbymonthfacetedbygrouperl=dmerge4.groupby([pd.Grouper(freq='M',key='startdate'),'landmark']).me\Grouonavariablewithmanyvaluesisalsopossible.Inthiscase,wecanseesummarystatisticsoneverysingledurationasanexample.Wecanbeveryspecificandthelevelofgranularitycanbeverydetailed.Wecanpinpointtheaveragedurationforaveryspecificprofileordemographicwhichcanbeverypowerfulforadata-drivendecisionmakingincludingmarketingorcivicinfrastructuregoalsforthebikesharingsystem.欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍Thisnexttablegivesanindicationofmonthlydurationsbasedonstartinglandmarkendstation.Inotherwords,wegetanideaofhowlongittakestoreachbybicycleandthedifferencesbetweencustomersandsubscribersallinonetable.Wecanidentifywhichcustomersarekeethebicyclesforlongerperiodsoftime.Let’slookatthebreakdownofaveragedurationbybothlandmarkandsubscriptionWecanseesomecleardifferencesbetweencustomersandsubscribers.Inmostcases,subscribersaretakingshorttrips.欢迎加入 Python编程学习交 ,群里免费提供500+本Python书籍Let’slookateachlevelofdurationonanhourlyInthistable,wearelookingathourlydatabyhourlytry=欢迎加入欢迎加入 Python编程学习交GroupoperationsonadatetimeLet’smakeatableofaveragedockcountbyeachprecipitationlevelandsubscriptioni=pd.DataFrame(dmerge4.groupby(['precipitation_in','subscriptiontype']).dockco\i欢迎加入 Python编程学习交 ,群里免费提供500+本Python书籍WhatarethetotalnumberofobservationsineachoftheinitialsixOtherthanfirstmonthofoperation,thedistributionofobservationsdistributionisprettyevenlyspreadout.12dtype:int6491dtype:Let’screateamulti-levelindexwithoutgrouWe’lloftenwanttoperformqueriesonthefulldmerge4twolevels=dmerge4.set_index(['landmark','subscriptiontype'])欢迎加入欢迎加入 Python编程学习交Createsagroupbyobjectdataframeonwhichaggregateactionscanbeperformedandcangroupbythemulti-indexes.grouplevel0=dmerge4twolevels.groupby(level=0)#sameresultasgrouplevel1=dmerge4twolevels.groupby(level=1)Thelevelcanalsobespecifiedbydmerge4twolevels.sum(level='subscriptiontype')#sameasaboveCreatethelevelwithoutdropitasadmerge4twolevels.xs('Subscriber',欢迎加入欢迎加入 Python编程学习交QueryingBysettinguptheinitialquery,Wecanthenchangetheinputsforin ctivereportingofresults.Wecananswerveryspecificquestionsbyquerying.Let’screatedagroupedobject,applyaggregatefunctions,andthenquerytheInthistable,wehavetheaveragedurationforsubscriptiontypeofcustomergrouped1=dmerge4.groupby(['landmark','subscriptiontype'])group2=grouped1[['duration']].agg([np.mean,np.std])query1=group2.query('subscriptiontype=="Customer"')Returnadatasetfromamulti-indexdataframethatcontainsonlystartterminalnumber62.#subsetingandindexinganon-groupedmultidmerge4twolevels.query('startterminal==欢迎加入欢迎加入 Python编程学习交Queryamulti-indexdataframeforsubscriptiontypeofdmerge4twolevels.query('subscriptiontype==Whatistheaveragedurationindescendingorderbyendstationwhenthestartterminalisnumber62?Wecangetthistypeofgranulardatabyquerying.Bysettinguptheinitialquery,wecanthenchangetheinputsforin ctivereportingofresults.duration62=dmerge4twolevels.query('startterminal==62').groupby('endstation')\2nd2ndatBealeatCivicCenterBART(7thatEmbarcaderoatPowellatPost(UnionEmbarcaderoat za(MarketatGoldenGateatSanFranciscoCaltrain(TownsendatSouthVanNessatCommercialatMarketatTownsendatGrantAvenueatColumbusSanFranciscoCityDavisatBroadwayStatBatteryWashingtonatHarry za(Ferry欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍5that PowellStreet Clayat Postat Steuartat EmbarcaderoatBryant YerbaBuenaCenteroftheArts(3rd@Howard) Marketat SanFranciscoCaltrain2(330Townsend) EmbarcaderoatFolsom TemporaryTransbayTerminal(HowardatBeale) Spearat 2ndatSouth 2ndat Marketat Howardat Name:duration_f,dtype:MergingandLet’sconcatenatethemorningmonthlyandeveningmonthlycreatedinthetimeseriessectionintoadataframefortheplottingsection.concatenated=pd.concat([morning_monthly,evening_monthly],keys=['morning','e\Mergesamenamedcolumnfromtwodatasetsbutkeepbothcolumnsbutrenamethemwithasuffixmerge=pd.merge(morning_monthly,evening_monthly,how='outer',left_index=True,\r\ight_index=True,suffixes=['_morning','_evening'])欢迎加入欢迎加入 Python编程学习交Ad-hocdatasummaryorstringUsealambdafunctiontogetsummarystatisticsofaveragedurationby umaveragedurationwasinSanJose,thehighestaveragedurationwasinPaloAltoandthelowestwasinSanFrancisco.dmerge4.groupby("landmark")[['duration_f']].apply(lambdax:x.describe()).unstac\AnotheruseofthelambdafunctionistomapaForexamplesake,tocheckthelengthofa#applyalambdaf=lambdax:len(str(x))Hereisanotherstringoperationbywayofmapalambdafunction.Verydmerge4['endstation'].map(lambdax:x.startswith('Grant')).head()2013-08-29 2013-08-29 欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍2013-08-29 2013-08-29 2013-08-29 Name:endstation,dtype:Makeagroupedobjectwithoutmakingitthedmerge4.groupby('landmark',欢迎加入非Python编程学习交 ,群里免费提供500+本Python书籍DataScienceProgramminginPython-DataLet’smakesomevisualizationsaspartofthei tiveprocessofdatamunging,aggregating,andvisualizing.Visualizationshelpusunderstandthedatabetter.Atbest,exploratorydatavisualizationinspiresquestionsandinformsour ysiswhileidentifyingtrendsandpatternstofurtherlearnfromthedata.Wewillseethatdisyingsimilarinformationinavarietyofwaysandusingdifferenttypesandstylesofplotscanrevealevenmoreaboutthedata.%matplotlibimportnumpyasnpimportpandasaspdfromdatetimeimportdatetime,fromggplotimportimportseabornasprintpd. printnp. %load_extExploratorydataInpracticaldata ysis,wewanttomakeourplotsinthemostefficientandsuccinctwaypossiblefollowingani tivedata ysisprocess.Avisualizationwillformmorequestionsthatleadtofurthervisualizationsusingpandas,ggplot,andseaborn.Inexploratoryvisualization,grou&aggregatingdataandplottingarepartofthat tiveprocess.Wewanttolearnfromourdatawhatfurthervisualizationswillprovideinsightandhopefullyleadtomorestatisticalquestionsandmorevisualizations.MeanPlottingtime-seriesDailyaveragedurationforthefirstsixmonthsofoperationofthebikesharingInthisplot,wecanseethatmosttripswereundersixtyminutesandfluctuatedforthemostpartbetweenundertwentyminutesuptoaroundfiftyminutesdependingonthetimeofday.print(ggplot(aes(x='startdate',y='duration_i'),data=daily_means)+\geom_line())+geom_smooth()DailyaveragemeanSinceSanFranciscodoesn’thavetypicallyharshwinters,temperaturedidnotseemtohavemucheffectonlengthofbicycletripsprint(ggplot(aes(x='startdate',y='mean_temperature_f'),data=daily_means)+\MeantemperaturesfortheentireThisplotisincludedheretocomparetheabovegraphwithplottingtheentiredataset.Takingthedailymeaninthepreviousplotsallowsustoconsolidatemultipleobservationsonasingledaytogivealessnoisygraph.Inthisgraph,everysingleobservationisplotted.Takingthedailymeanisawaytoseethetrendmorecleanlyandalsowillallowforfurther ysisonadailydata.print(ggplot(aes(x='startdate',y='mean_temperature_f'),data=dmerge4)+\AveragedurationonamonthlybasisforthesixmonthThemonthlydurationtakesawaysomeofthenoiseindicatedbythedailydata.WedoactuallyseeadecliningtrendaswegofromAutumntoWinter.print(ggplot(aes(x='startdate',y='duration_i'),data=monthly_means)+\<brRememberthatweconcatenatedtwotimeseriesintervalsforthemorningandeveningcommute.Wecannowseeinthisgraphthattheaveragedurationbymonthismostlyhigherintheeveningthaninthemorning.KeeinmindthatAugustwasthefirstmonthofoperation,theplotalsoindicatesthatonaverage,thebicyclewasreturnedtoadockwithintheinitialthirtyminutefromstarttimefromSeptembertoFebruary.AveragedurationbymonthfacetedbymorningandeveningcommutingAveragedurationat4pmforthesixmonthWhat’sreallyniceaboutPandastimeseriesfunctionalityisthatwecanalsolookatplotsatspecifictimesaswell.Hereisaplotoftotaldurationat4pmfortheentiresixmonthperiod.Theplotindicatesthatauserhadabicycleforareallylongtimeat4pminOctoberbutmostlythetripdurationswereunderfiftyminutes.#totaldurationatparticulardmerge4.duration_i.at_time(time(16,four_pm_df=pd.DataFrame(dmerge4.duration_f.at_time(time(16,0))).reset_index()ggplot(four_pm_df,aes(x='startdate',y='duration_f'))+geom_line()#durationat4pmforeverydaytheentiresixmonthAveragedurationbytimeofDurationsareshorterduringthemorningandeveningcommutinghoursandslightlyhigherduringthemid-dayandatnight.Thedataalsoindicatesthatpeoplearekeebicyclesforalongerperiodoftimeaftermidnightuntilfouram.DodgedbarplotofaveragedurationbylandmarkandtimeofThisplotgivesareallynicevisualofaveragedurationbylandmarkandtimeofday.Acrosstheboard,morningdurationsareshortestwhichwouldmakesenseformorningcommuters.MorningcommutesarehighestinPaloAlto.Perhapsthereismoretrafficorgreaterdistancestotravel.Inaddition,weknowthattherearefewerbicycledocksinPaloAlto.Mid-daystarttimeshavelongertripdurationexceptforMountainViewwhereeveningbicycletripsarethelongest.DurationinSanFranciscoistheshortestandthisisalsotheareathatthehighestnumberofdocksinthedatasetasshowninthegrouandaggregatingsectionof sns.factorplot("landmark","duration_f","timeofday",DockcountsbylandmarkandItisevidentinthisplotthatdockcountswerelowestinPaloAltowhereaveragedurationswerealsothehighest.sns.factorplot("landmark","duration_f","dockcount",data=timelandmark,palette=\AveragedurationbylandmarkfacetedbyThisplotisnicebecauseitallowsustovisuallyseethedifferencesindurationbylandmarkbycategoricaltimeofday.sns.factorplot("landmark","duration_f",data=timelandmark,row="timeofday",margin_titles=True,aspect=3,size=2,palette="BuPu_d");AveragedurationforeachlandmarkfacetedbyPaloAltohadhighestdurationinalmosteverymonthexceptforFebruarywhenMountainViewhadthehighestaverageduration.sns.factorplot("landmark","duration_f",data=dmerge4,row="month",margin_titles=True,aspect=3,size=2,palette="Pas1");Plotofmean,minimum, umtemperaturebyThisplotindicatesplotsfor umsummaryMeanandminimumdurationbyhouroftheDurationislowestinthemorningandeveningcommutehoursandhighestintheeveningandweehours.WhataretheaveragedurationsbydayoftheInwhatmonthdidthehighestdurationoccurandwhatwasthesubscriptionThehighestdurationoccurredinDecemberforasubscriptiontypeofcustomerinRedwoodCityDodgedbarplotsbymonthofaveragedurationbylandmarkandsubscriptionInDecember,January,andFebruary,thehighestaveragedurationwasforacustomerinRedwoodCity.three_grouper_unstack_twice=MonthlyaveragedurationbyFromSeptemberuntilDecember,PaloAltohadthehighestaveragedurationbymonthwhileSanJoseandSanFranciscohadthelowest.AveragedurationbymonthincreasedinRedwoodCityfromSeptembertoFebruary.grouperl=dmerge4.groupby([pd.Grouper(freq='M',key='startdate'),\DodgedbarplotofmonthlyaveragedurationbyThisdodgedbarplotshowsthatdurationwashighestforRedwoodCityinFebruaryandhighestforPaloAltoinNovember.Ineverymonth,durationswerelowestforbothSanFranciscoandSanJose.Wecouldhypothesizethisisduetocommuting,trafficpatterns,anddemandforbicycles.Inanycase,knowingwheredurationsarehigherwillinfluencedemandanddockcount.Averagemonthlydurationfacetedbytimeofgrouperm=AveragedurationbyAveragedurationbyhourofthedayfortheentiresixmonthggplot(dmerge4,aes('hour','duration_f',color='landmark'))+geom_point(stat="i\BoxplotofmonthlyaveragedurationbySubscribersforthemostpartarebelowsixtyminutedurationswhilecustomershavehigherdurationsandthewidestrangeofdurationinRedwoodCityfollowedbyMountainthree_grouper=importseabornassns.factorplot("landmark","duration_f","subscriptiontype",three_grouper,\kind="box",palette="PRGn",aspect=2.25)DistributionofmeanItdoesn’tgettoowarmortoocoldinNorthernCaliforniabetweenAugustandAveragedurationandstandarddeviationforsubscriptiontypeofCustomerbyBarplotofaveragedurationbylandmarkandsubscriptiontype.Inthiscase,weonlywantedtocomparedurationbyonetypeofcustomer.dmerge4.groupby(['landmark','subscriptiontype'])[['duration_f']].agg([np.mean,n\p.std]).query('subscriptiontyp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论