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

Use R from Python

Use R from Python

airspace_nobo

July 27, 2019
Tweet

More Decks by airspace_nobo

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ w ໊લখ௶ୖਓ,PUTVCP5BLVUP w 5XJUUFS!BJSTQBDF@OPCP w ݱࡏम࢜೥ ༛༧िؒ  w

    ະདྷσʔλαΠΤϯςΟετ w ݚڀγϦϯυϦΧϧλΠϜγϦʔζ  w ࢖༻཰3 1ZUIPO ࡱӨऀ!LVSDLZ@Z
  2. QZQFSࣄ࢝Ί w QJQͰΠϯετʔϧ w SQZQFS3 Λ࣮ߦ͠ SΠϯελϯεʹ SίʔυΛ จࣈྻͱͯ͠༩͑Δ w

    ݁ՌΛදࣔ͢Δ࣌ʹ͸ QSJOUΛ࢖͏ r = pyper.R() r("a <- 5") print(r("a")) # ݁Ռ # try({a}) # [1] 5 r("b <- 10") print(r("a + b")) # ݁Ռ # try({a + b}) # [1] 15 pip install pyper
  3. w Sͱಉ༷ʹ ಡΈࠐΜͰ͍ͳ͍ QBDLBHFͷؔ਺͸࢖͑ͳ͍ w ؔ਺ΛಡΈࠐΊ͹ ύΠϓԋࢉࢠ΋ ࢖͑Δʂ r =

    pyper.R() print(r("glimpse(iris)")) # not find function "glimpse" r = pyper.R() r("library(tidyverse)") print(r("glimpse(iris)")) # try({glimpse(iris)}) print(r("iris %>% glimpse")) # try({iris %>% glimpse}) QZQFSؔ਺ಡΈࠐΈ
  4. QZQFS QZUIPOS w SQZQFS3 VTF@QBOEBTb5SVF` Ͱ  QBOEBT%BUB'SBNF͕࢖͑Δ w SBTTJHOؔ਺Ͱ

    EG QZUIPO Λ Q@EG S ʹׂΓ౰ͯΔ n = 300 x = np.random.poisson(10, n) y = np.random.normal(0, 5, n) # dataFrame df = pd.DataFrame({"X":x, "Y":y}) r = pyper.R(use_pandas='True') r.assign("p_df", df) print(r("summary(p_df)"))
  5. QZQFS SQZUIPO  w SHFUؔ਺͸ S಺ͷσʔλΛQZUIPOଆͰ ࢖༻Ͱ͖ΔΑ͏ʹ͢Δ w σʔλͷܕ͸ҎԼʹͳΔ ϕΫτϧOVNQZBSSBZ

    σʔλϑϨʔϜQBOEBT%BUB'SBNF r = pyper.R() r("rx <- rexp(300,1)") r("rdf <- data.frame(x = 1:200, y = rnorm(200))") px = r.get(“rx") pdf = r.get("rdf") plt.hist(px, bins=20) plt.ylabel("freq") plt.show()
  6. Ϣʔβࣗ࡞ؔ਺ w σʔλͷߦྻ਺Λදࣔ͢Δؔ਺ TIBQF@EBUB Λఆٛͨ͠εΫϦϓτϑΝ ΠϧΛ SͰಡΈࠐΉΑ͏ʹQZUIPOΛ௨ͯ͡΋ಡΈࠐΊΔ shape_data <- function(data){

    sprintf("σʔλ͸%dߦ, %dྻͰ͢!!",NROW(data), NCOL(data)) } r = pyper.R(use_pandas='True') r("source('~/Desktop/TokyoR/script/function.R', echo=FALSE)") print(r("shape_data(iris)"))
  7. Ϣʔβࣗ࡞ؔ਺ w ΋ͪΖΜQZUIPOଆ͔Β༩͑ͨσʔλʹରͯ͠΋ࣗ࡞ؔ਺Λద༻Մೳ σʔλ͕গ͠ҟͳΔͷͰ ྻʹͳΔ  w ΠϯελϯεΛॳظԽ͠ͳ͚Ε͹ ؔ਺͸อ࣋͞ΕΔʂ from

    sklearn.datasets import load_iris iris = load_iris() iris = pd.DataFrame(data=iris.data, columns=iris.feature_names) r.assign("p_iris", iris) print(r("shape_data(p_iris)"))
  8. ศརͳ࢖͍ํΛߟ͑Δ w SͰ͜Μͳؔ਺࡞ͬͯʜ w QZUIPOͰ΋͜Μͳؔ਺࡞Ε͹ʜ library(tidyverse); library(skimr) view_data <- function(data){

    print("use skim function"); skim(data) %>% kable print("use glimpse function"); glimpse(data) } def view_data(data): r = pyper.R(use_pandas='True') r("source('~/Desktop/TokyoR/script/function.R', echo=FALSE)") r.assign("p_data", data) print(r(“view_data(p_data)”)) view_data(iris)
  9. େ͖ͳ໰୊఺ w ద౰ͳσʔλϑϨʔϜΛ࡞੒ ͢Δؔ਺Λ࡞Δ w 3ͰͷEBUBGSBNF࡞੒࣌ؒ  1ZUIPO΁ͷσʔλҠߦ࣌ؒ w LBHHMFͱ͔Ͱ

    લॲཧ3Ͱॻ ͍ͯ σʔλͦͷ··ࣸͯ͠ ػցֶशʹҠߦ͢ΔΈ͍ͨͳ ͜ͱ͕Ͱ͖ͳ͍ fun <- function(len){ df1 <- tibble(aaa = 1:len, bbb = 10000:10000+len, ccc = 1000000:1000000+len, ddd = rbinom(len,1,0.5), eee = rnorm(len, 0, 100000), fff = rlnorm(len, 0, 100), ggg = stri_rand_strings(n=len, length=10), hhh = seq(as.Date("2018-03-07"), by="day", length.out=len)) return(df1) } MFO 3 1ZUIPO  TFD TFD  TFD TFD  TFD ແཧ
  10. ·ͱΊ w ԿΒ͔ͷཧ༝͕͋ͬͯ QZUIPO΋S΋࢖Θͳ͍ͱ͍͚ͳ͍࣌ SͰ࡞੒ͨ͠ ؔ਺Λ࢖͑Δͷ͸࣌ؒతʹ΋ อकతʹ΋ศར  w ࣗ෼Ͱ࢖͏෼ʹ͸໰୊ͳͦ͞͏͕ͩ

    ਓʹ࡞੒ͨ͠ίʔυΛ౉͢ͳͲͷ࣌ ͸ ஫ҙ͕ඞཁ  w ձࣾͰ1ZUIPO͔͠࢖͑ͳ͍ਓ͕ͨͪ ͬͦ͜Γ࢖͑Δ  w SFUJDVMBUFQBDLBHFͷํ͕ศར ݸਓͷײ૝