Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pythonista's Batting Eye(選球眼) #PyConJP 2018

Pythonista's Batting Eye(選球眼) #PyConJP 2018

PyCon JP 2018 Talk Session https://pycon.jp/2018/

Baseball Science, Web Application, Barrel Zone, and more...

Shinichi Nakagawa

September 17, 2018
Tweet

More Decks by Shinichi Nakagawa

Other Decks in Programming

Transcript

  1. Pythonista’s Batting Eye Shinichi NAKAGAWAʢ໺ٿΤϯδχΞʣ a.k.a. @shinyorke NEXT BASE Corp.

    Chief Technology Officer PyCon JP 2018 Talk Session 2018/9/17 Pythonistaͷબٿ؟ʢͤΜ͖Ύ͏͕Μʣ
  2. ʲ౴ʳΫϦεɾσʔϏε • Khris Davis,௨শʮKhrushʯ • Ξ੢஍۠ΞεϨνοΫεͷओ๒ • 3೥࿈ଓ40ຊྥଧҎ্(2016ʙ2018)
 ※ࠓγʔζϯ43ຊྥଧ(9/17 JSTݱࡏ)

    • ӈ౤ӈଧ,എ൪߸2 • ʮ࢛൪DHʯ,૸ྥͱकඋ͸μϝ • ਎௕180cm/86kgɺҙ֎ͱখ͍͞ ݩը૾ɿWikipedia
  3. ໺ٿΤϯδχΞͰ͢ • PyCon JPΛΩοΧέʹʮϗϯτʹ໺ٿͷਓʯ
 ʹͳͬͨʮ໺ٿͷΤϯδχΞɾݚڀऀʯͰ͢ • Shinichi NAKAGAWA(@shinyorke) • ʢגʣωΫετϕʔε

    ໺ٿΤϯδχΞ݉CTO • Python΋͘΋ࣗ͘शࣨʢ#rettypyʣΦʔΨφΠβʔ • ʲCareerʳITίϯαϧˠϦΫϧʔτˠRetty→΍͖͏ • ຊ৬͸αʔόʔαΠυΤϯδχΞɺࠓ͸ԿͰ΋԰ɺڕྉཧେ޷͖
  4. ʲCMʳωΫετϕʔε #ͱ͸ • Innovations For All Athletes
 (શͯͷΞεϦʔτʹٕज़ֵ৽Λ) • τϥοΫϚϯ(υοϓϥʔϨʔμʔ)


    ͔ΒಘΒΕͨ౤ٿσʔλΛղੳ͠,
 ෼ੳɾՄࢹԽΛߦ͏ϓϩμΫτʮBACSʢόοΫεʣʯΛٿஂɾબखʹఏڙ • ϓϩΞϚ໰Θͣɺ໺ٿνʔϜɾબखͷಈ࡞ղੳɾӡӦαϙʔτetc… • ͦͷଞϝσΟΞࣄۀͳͲͳͲɺৄ͘͠͸ https://nextbase.co.jp/ • ݱࡏΤϯδχΞืूதͰ͢ʂҰॹʹεϙʔπΛՊֶ͓ͯ͠΋͠Ζ͍ੈͷதΛ࡞Ζ͏ʂ ※BACS͸ʮBaseball Analytics and Coaching Systemʯͷུ
  5. ʲCMʳPython΋͘΋ࣗ͘शࣨ #rettypy • ࢲ͕Ҏલࡏ੶͍ͯͨ͠RettyͷΤϯδχΞ஥ؒͱڞʹ։࠵͍ͯ͠Δ΋͘΋͘ձ • झࢫ&Ϟοτʔ • ֤ࣗ,͓୊Λ࣋ࢀͯ͠ʮࣗश(΋͘΋͘)ʯ͢Δ • ൃදɾࡶஊΛ௨ͯ͡৘ใަ׵΍ֶͼΛಘΔ

    • ΠΠΰϋϯ(ϥϯνɾ͓΍ͭ)Λ༇͠Ή • ॳ৺ऀ͔ΒΤόϯδΣϦετ·Ͱଟछଟ༷ͳࢀՃϝϯόʔ • ࣍ճ͸ࠓि຤ʢ9/22౔ʣͰ͢ɺདྷͯͶ https://retty.connpass.com/event/100173/
  6. ໺ٿ෼ੳج൫ʮMortonʯ • PyCon JP 2014ʙ2017Ͱഓ͖ͬͯͨϊ΢ϋ΢Λू݁͠ɺ
 σʔλऩूʙ෼ੳʙՄࢹԽΛϦχϡʔΞϧͨ͠৽࣌୅ͷ໺ٿج൫ • ॳ୅(2014)ͷʮNo Ballʢ໺ٿʣʯɺ2017ʮBradfordʯͷػೳʹ
 Ճ͑ɺτϥοΩϯάσʔλͷ෼ੳΛ৽ͨʹ௥Ճ

    • ໊લ͸τϥοΩϯάσʔλͰ࠶ධՁ͞Εͨϝδϟʔ۶ࢦͷ
 ઌൃӈ࿹ʮνϟʔϦʔɾϞʔτϯʢݱΞετϩζʣʯ͔Βഈआ
 ˠৄ͘͠͸ʮϏοάσʔλɾϕʔεϘʔϧʯ͍ͬͯ͏ຊʹ͋Γ·͢

  7. PyCon JP 2014͔ΒͷਐԽ • Pythonɿ3.4→3.7 • Djangoɿ1.7→2.1 • Bootstrapϕʔεͷී௨ͷWebΞϓϦˠSPA •

    άϥϑϥΠϒϥϦ΋ચ࿅͞Εͨ(Chart.jsͳͲ) • ໺ٿσʔλɺ৽ͨʹʮτϥοΩϯάσʔλʯ͕ՃΘΔʂ
 ˠMLBެೝͷʮBaseball Savantʯͱ͍͏σʔληοτ͕ՃΘΓɺ
 ɹଧٿ଎౓΍֯౓ɺϘʔϧճస਺ͳͲ΋෼ੳͷର৅ʹ
  8. Nuxt UIʙCore UIͷNuxt.js൛ • BootstrapϕʔεͷAdminʢ؅ཧʣը໘ͷ༗໊ͳ
 ύοέʔδͱͯ͠ʮCore UIʯ͍ͬͯ͏ͷ͕͋Δ • Core UIࣗମ͸৭ΜͳFWʢAnguler,VueͳͲʣʹ


    ରԠ͍ͯ͠Δ,ͦͷதʹNuxt.js൛͕ • Nuxt UI͍ͬͯ͏ౕΛForkͯ͠ಠ࣮ࣗ૷͠·ͨ͠
 https://github.com/muhibbudins/nuxt-coreui
  9. SerializerͰࢦඪ஋ܭࢉ(งғؾ) from rest_framework import serializers from sabr.stats import Stats from

    mlb.models import Batter class BattingSerializer(serializers.HyperlinkedModelSerializer): ba = serializers.SerializerMethodField('batting_average') slg = serializers.SerializerMethodField('slugging') obp = serializers.SerializerMethodField('onbase') @classmethod def calc_single(cls, obj): return Stats.single(obj.h, obj.hr, obj.number_2b, obj.number_3b) @classmethod def calc_tb(cls, obj): return Stats.tb(cls.calc_single(obj), obj.hr, obj.number_2b, obj.number_3b) @classmethod def calc_avg(cls, obj): return Stats.avg(obj.h, obj.ab) @classmethod def calc_obp(cls, obj): return Stats.obp(obj.h, obj.bb, obj.hbp, obj.ab, obj.sf) @classmethod def calc_slugging(cls, obj): return Stats.slg(cls.calc_tb(obj), obj.ab) def batting_average(self, obj): return self.calc_avg(obj) def slugging(self, obj): return self.calc_slugging(obj) def onbase(self, obj): return self.calc_obp(obj) class Meta: model = Batting fields = ( # ※͜͜ʹSerializerͷcolumn͕ฒͿ )
  10. REST FrameworkͰྑ͔ͬͨ • ߦϨϕϧͷࢦඪܭࢉΛSerializerͰ׬݁Ͱ͖Δ
 ˠSerializerMethodFieldͰhookͰ͖Δ, ςετ͠΍͍͢ʢςετ͸ॻ͍ͯͳ͍ʣ • SerializerMethodField্Ͱࢦඪܭࢉ
 ˠҎલࣗ࡞ͨ͠ʮsabrʯͱ͍͏ύοέʔδͰඞཁͳܭࢉ͕Ͱ͖ͨ
 ɹ͍͍ͪͪSQLͰܭࢉͨ͠Γؔ਺࡞ͬͨΓ͢Δख͕ؒল͚ͨ

    • DB΁ͷΫΤϦͱSerializerʢ໭Γ஋ʣͷ໾ׂɾ੹೚Λ෼͚Δ
 ˠViewͰΫΤϦൃߦ,SerializerͰ໭Γ஋੔ܗͱ៉ྷʹ෼͚͔͚ͯΔ
 ɹ݁Ռ,ݟ௨͕͠ྑ͍ʢϝϯς͠΍͍͢ʣίʔυʹ
 ɹͱ͸͍͑ਏ͔ͬͨ(´ɾωɾʆ)
  11. Luigi + pandasͰ೔࣍όον • Baseball Savant https://baseballsavant.mlb.com/
 ͷσʔλ͸೔ʑߋ৽͞ΕΔʢࢼ߹ཌ೔ʹ͸ެ։ʣ • ͬͯฉ͘ͱ,΍ͬͺ৽͍͠σʔλͰ෼ੳɾղੳΛ


    ͨ͘͠ͳΔΑͶʁʢྫɿࡢ೔ͷOhtani-sanͬͯʁʣ • ͬͯࣄͰ, Luigi + pandasͰ೔࣍Ͱσʔλऔಘͱ
 DBʢMySQLʣ΁ͷߋ৽Λߦ͏ύΠϓϥΠϯΛ࡞ͬͨ,ͪΐͬͱਏ͔͚ͬͨͲ • ࣮͸ڈ೥,RettyͰಉ͡Α͏ͳ࢓ࣄΛ͍ͯͯͦ͠ͷܦݧ͕ੜ͖ͨ
 ༧໿ಋઢΛ͑͞͞ΔPython੡όοΫΤϯυΛLuigi+pandas+ScrapyͰ࡞ͬͨϋφγ
 https://qiita.com/shinichi-nakagawa-r/items/f91ee099e5bce7cd5a4f
  12. PyDataͰϓϩτλΠϐϯά • WebΞϓϦ(Django/Nuxt.js),όοΫΤϯυ(Luigi)͓Αͼσʔλߋ৽ɾલॲ ཧ͢΂ͯ, Jupyterͱpandas, BokehͰϓϩτλΠϐϯάͨ͠ • ࢼ͠ॻ͖ͨ͠Γσόοάͨ͠Γ͢Δͷʹ,ϒϥ΢βͰಈ͘؀ڥ͸
 ͱͯ΋΍Γ΍͍͢ʢؾܰʹ࡞ͬͯյͤΔʣ. •

    ιϑτ΢ΣΞͷʮՁ஋ʯ͸ৗʹTry and ErrorΛ܁Γฦ͠ͳ͕Βਫ਼៛Λ
 ্͛ΔϞϊ,͜͏͍ͬͨ࡞ۀ͸Jupyter࠷ڧ • ର৅σʔλΛʮࣂ͍ͳΒ͢ʯ·Ͱ΍Δͷ͕ϕετ.
 ͪΌΜͱ࡞Γ࢝Ί͔ͯΒਏΈΛຯΘ͏ΑΓίετֻ͕͔Βແͯ͘ྑ͍.
  13. ࠓճ׆༂ͨ͠Ϟϊ • Bokehʢάϥϑඳըʣ • ࢄ෍ਤ΍ԁάϥϑ • ׳Εͳ͍JSͰඳ͘લʹ
 ଘ෼ʹ࣮ݧ • pandasʢσʔλॲཧʣ

    • CSVΛॲཧͨ͠Γ • DB͔Β௚઀σʔλΛҾ͍ͨΓ • ܽଛ஋ॲཧͱ͔େ׆༂ • ͜ΕΒΛJupyterͰ͍͍ײ͡ʹॲཧ
  14. ͦͷଞͷͩ͜ΘΓϙΠϯτ • Sean LahmanʢηΠόʔσʔλʣDBߏங • PyCon JP 2014Ͱ࡞ͬͨϚΠάϨʔγϣϯπʔϧʢࣗ࡞ʣΛ࢖ͬͯܭࢉ • εΩʔϚ͕มΘͬͨ෼Λ৽نͰ։ൃʢେͯ͠ਏ͘ͳ͔ͬͨʣ

    • Docker • Ϋϥ΢υαʔϏεͰͷӡ༻͓Αͼ,ଞϝϯόʔ΁ͷಈ͘΋ͷڞ༗ • ϗϯτ͸K8SԽ͔͕ͨͬͨؒ͠ʹ߹Θͣorz #rettypy Ͱ΍Δͱࢥ͏ • ։ൃɾςετ͸Ұ؏ͯࣗ͠෼ͷPC্ɺPyCharmΛ׆༻ͯ͠΍Γ·ͨ͠
  15. ਏ͍ʢਏ͘ͳ͍ʣϥϯΩϯά ໊લ ଧ ਏ͍ʢࡾৼʣ (JBODBSMP 4UBOUPO ӈ  +BWJFS#BF[ ӈ

     1BVM (PMETDINJEU ӈ  +PFZ(BMMP ࠨ  5SFWPS4UPSZ ӈ  ໊લ ଧ ਏ͘ͳ͍ ʢ҆ଧʣ +PTF"MUVWF ӈ  5SFWPS4UPSZ ӈ  .BSDFMM 0[VOB ӈ  1BVM (PMETDINJEU ӈ  ,FWJO1JMMBS ӈ 
  16. બ୒ͱܾஅ • ΤϯδχΞʹ͸ʮબ୒ʯͱʮܾஅʯ͕ৗʹ͋Δ • DjangoͰ࡞Δ?Flask??ͦΕͱ΋Rails??? • SPAͬͯVue.js͕͍͍ͷʁͦΕͱ΋Angular? • ֮͑ΔͳΒPythonͳͷ͔JavaScript •

    ৗʹ͘ΔϘʔϧʹରͯ͠ʮৼΔʯ͔ʮৼΒͳ͍ʯ͔
 બ୒͕ഭΒΕΔ໺ٿબखͩͬͯʮબ୒ʯͱʮܾஅʯͷ܁Γฦ͠
 ˞ਏ͍εϥΠμʔ͸ࣺͯΔࣄ΋ඞཁ
  17. ʮܾஅʯΛڪΕͳ͍ • ʮΠγϡʔ͔Β͸͡ΊΔʯ • ࡞ΔϞϊɾ෼ੳ͢ΔίτΛʮݴޠԽʯ͢ΔɺԾઆɾςʔϚࠐͰ • ޙड़͢Δʮܾஅ͢Δʯϧʔϧʹͦͷ··ܨ͕Δ • ϞϊΰτΛʮܾஅ͢ΔʯϧʔϧΛࣗ෼ʹ࡞Δ •

    ʮ໎ͬͨΒ˓˓ʯͷʮ˓˓ʯΛܾΊΔ • ࢲͷ৔߹ɺΤϯδχΞϦϯάͩͱ
 ʮ໎ͬͨΒPythonʯͱ͍͏ϧʔϧΛܾΊ͍ͯΔ • ໺ٿͰݴ͏ͱ͜ΖͷʮϠϚΛுΔʯΈ͍ͨͳϞϊ • ʮܾஅϧʔϧʯͷ֎ʹ͋Δ΋ͷ͸ݟಀ͢ࣄΛڪΕͳ͍ʢεϥΠμʔΛݟૹΔ༻ʹʣ
  18. ूத͢Δ • ʮબ୒ʯʮܾஅʯΛਖ਼͘͠ճ͢ʹ͸ʮूதʯ΋େ੾ • ʮબ୒ʯʮܾஅʯͨ͠ࣄΛ΍Γ͖ΔҝͷϦιʔεΛʮूதʯ͢Δ • ৭ʑ͋Ε͜Εͱ΍Γ͗͢ͳ͍ • ྲྀߦΓ΋ͷʹඈͼͭ͘ •

    ຖ೔ɾຖिͷ༻ʹษڧձ΍Πϕϯτʹߦ͘ • ूத͢Δ͸ͣͷϦιʔεʢ࣌ؒɾ͓ۚʣ͕ൃࢄ͢ΔΑ͏ͳࣄ͸NG • ࣌ؒతʹʮूதʯͰ͖ΔΑ͏ͳʮश׳ʯ࡞ΓʢྫɿSNSઈͪʣ