20140920 tokyo r43

39
前処理入門 Tokyo.R#43 2014-09-20 @kashitan

Upload: takashi-kitano

Post on 21-Apr-2017

2.997 views

Category:

Data & Analytics


2 download

TRANSCRIPT

Page 1: 20140920 tokyo r43

前処理入門!

Tokyo.R#43 2014-09-20 @kashitan

Page 2: 20140920 tokyo r43

> summary(kashitan)

• TwitterID : @kashitan

• お仕事 : 某通信会社

Page 3: 20140920 tokyo r43

セクシーになりたい

Page 4: 20140920 tokyo r43

現実は

Page 5: 20140920 tokyo r43

ひたすら前処理の日々

Page 6: 20140920 tokyo r43

前処理を楽にしたい

Page 8: 20140920 tokyo r43

という話は11年前もあった

2回でシリーズ終了

Page 9: 20140920 tokyo r43

ということで

Page 10: 20140920 tokyo r43

シリーズ復活よろしくお願いします

Page 11: 20140920 tokyo r43

Agenda1. caretパッケージ

• dummyVars()

• nearZeroVar()

• findLinearCombos()

• preProcess()

2. tidyrパッケージ

• gather()

• spread()

• separate()

• unite()

Page 12: 20140920 tokyo r43

caretによる前処理

Page 13: 20140920 tokyo r43

What’s caret?• 過去の勉強会発表資料参照

Page 14: 20140920 tokyo r43

dummyVars()• カテゴリカル変数からダミー変数を作成する

Species .setosa

Species .versicolor

Species .virginica

1 0 01 0 0

・・・0 1 00 1 0

・・・0 0 10 0 1

Species

setosasetosa・・・

versicolorversicolor・・・virginicavirginica

Page 15: 20140920 tokyo r43

dummyVars()• カテゴリカル変数からダミー変数を作成する

> library(caret)> data(iris)> noNames <- dummyVars(~., data=iris)> iris.dummy <- as.data.frame(predict(noNames, iris))> str(iris.dummy)'data.frame': 150 obs. of 7 variables:・・・ $ Species.setosa : num 1 1 1 1 1 1 1 1 1 1 ... $ Species.versicolor: num 0 0 0 0 0 0 0 0 0 0 ... $ Species.virginica : num 0 0 0 0 0 0 0 0 0 0 ...

Page 16: 20140920 tokyo r43

dummyVars()• カテゴリカル変数からダミー変数を作成する

> library(caret)> data(iris)> noNames <- dummyVars(~., data=iris)> iris.dummy <- as.data.frame(predict(noNames, iris))> str(iris.dummy)'data.frame': 150 obs. of 7 variables:・・・ $ Species.setosa : num 1 1 1 1 1 1 1 1 1 1 ... $ Species.versicolor: num 0 0 0 0 0 0 0 0 0 0 ... $ Species.virginica : num 0 0 0 0 0 0 0 0 0 0 ...

前回発表のdummiesの方が便利 !

!

!

!

Page 17: 20140920 tokyo r43

dummyVars()• 複数のカテゴリカル変数からダミー変数を作成する

Sex.Male: Age:Child

Sex.Male: Age:Adult ・・・

1 0 ・・・0 0 ・・・

・・・0 1 ・・・0 0 ・・・

・・・

Sex Age

Male ChildFemale Child・・・ ・・・Male Adult

Female Adult・・・ ・・・

Page 18: 20140920 tokyo r43

dummyVars()• 複数のカテゴリカル変数からダミー変数を作成する

> noNames <- dummyVars(~Sex:Age, data=titanic)> titanic.dummy <- as.data.frame(predict(noNames, titanic))'data.frame': 2201 obs. of 10 variables:・・・ $ Sex.Male:Age.Child : num 1 1 1 1 1 1 1 1 1 1 ... $ Sex.Female:Age.Child: num 0 0 0 0 0 0 0 0 0 0 ... $ Sex.Male:Age.Adult : num 0 0 0 0 0 0 0 0 0 0 ... $ Sex.Female:Age.Adult: num 0 0 0 0 0 0 0 0 0 0 ...

Page 19: 20140920 tokyo r43

nearZeroVar()• 分散が0に近い変数を検出する

peoe_vsa.2.1

0.000000

0.000000

0.000000

0.000000

0.000000

19.760620

0.000000

・・・

分散が0に近い変数は 予期しない挙動の原因になる

(学習用と検証用にデータを分割したり クロスバリデーションするときとか)

Page 20: 20140920 tokyo r43

nearZeroVar()• 分散が0に近い変数を検出する

> nearZeroVar(iris[, -5], saveMetrics = TRUE) freqRatio percentUnique zeroVar nzvSepal.Length 1.111111 23.33333 FALSE FALSESepal.Width 1.857143 15.33333 FALSE FALSEPetal.Length 1.000000 28.66667 FALSE FALSEPetal.Width 2.230769 14.66667 FALSE FALSE

FALSEなら問題なし

Page 21: 20140920 tokyo r43

nearZeroVar()• 分散が0に近い変数を検出する

> dim(bbbDescr)[1] 208 134> # nearZeroVarに該当する変数の列番号 > nzv <- nearZeroVar(bbbDescr)> nzv[1] 3 16 17 22 25 50 60> # nearZeroVarに該当する変数を除外> bbbDescr.nzv <- bbbDescr[, -nzv]> dim(bbbDescr.nzv)[1] 208 127

Page 22: 20140920 tokyo r43

findLinearCombos()• 線形従属する変数を検知する

V1 V2 V3 V4 V5 V6

1 1 0 1 0 0

1 1 0 0 1 0

1 1 0 0 0 1

1 0 1 1 0 0

1 0 1 0 1 0

1 0 1 0 0 1

線形従属する変数が データに含まれている場合

分析は失敗する (多重共線性がある)

Page 23: 20140920 tokyo r43

findLinearCombos()• 線形従属する変数を検知する

> dim(testData2)[1] 6 6> flc <- findLinearCombos(testData2)> flc$linearCombos$linearCombos[[1]][1] 3 1 2!$linearCombos[[2]][1] 6 1 4 5!$remove[1] 3 6!> dim(testData2[, -flc$remove])[1] 6 4

3列目は1列目と2列目の線形従属 V3 = V1-V2

6列目は1列目と4列目,5列目の線形従属 V6 = V1 - V4 - V5

Page 24: 20140920 tokyo r43

preProcess()• まとめて標準化する

Sepal .Length

Sepal .Width

Petal .Length

Petal .Width

5.1 3.5 1.4 0.24.9 3.0 1.4 0.24.7 3.2 1.3 0.24.6 3.1 1.5 0.25.0 3.6 1.4 0.25.4 3.9 1.7 0.44.6 3.4 1.4 0.3

・・・ ・・・ ・・・ ・・・

1変数ずつ scale()実行するのが

面倒

Page 25: 20140920 tokyo r43

preProcess()• まとめて標準化する

> preProc <- preProcess(iris[, -5])> iris.scale <- predict(preProc, iris[, -5])> apply(iris.scale,2,mean) Sepal.Length Sepal.Width Petal.Length Petal.Width -4.484318e-16 2.034094e-16 -2.895326e-17 -2.989362e-17 > apply(iris.scale,2,sd)Sepal.Length Sepal.Width Petal.Length Petal.Width 1 1 1 1

Page 26: 20140920 tokyo r43

caretまとめ• dummyVars()

• ダミー変数を作成する • nearZeroVar()

• 分散が0に近い変数を検知する • findLinearCombos()

• 線形従属する変数を検知する • preProcess()

• まとめて標準化する

Page 27: 20140920 tokyo r43

tidyrによる前処理

Page 28: 20140920 tokyo r43

What’s tidyr?• 神Hadley Wickhamがreshape2を再設計し、dplyr, magritterと共に使いやすいようにしたパッケージ

Page 29: 20140920 tokyo r43

gather()• reshape2のmelt()相当。横→縦変換

Sepal .Length

Sepal .Width

Petal .Length

Petal .Width Species

5.1 3.5 1.4 0.2 setosa・・・ ・・・ ・・・ ・・・ ・・・

Species variable valuesetosa Sepal.Length 5.1setosa Sepal.Width 3.5

setosa Petal.Length 1.4

setosa Petal.Width 0.2・・・ ・・・ ・・・

Page 30: 20140920 tokyo r43

gather()• reshape2のmelt()相当。横→縦変換

> iris %>% head(1) %>% name_rows() Sepal.Length Sepal.Width Petal.Length Petal.Width Species .rownames1 5.1 3.5 1.4 0.2 setosa 1> iris %>% head(1) %>% name_rows() %>% + gather(variable, value, -.rownames, -Species) Species .rownames variable value1 setosa 1 Sepal.Length 5.12 setosa 1 Sepal.Width 3.53 setosa 1 Petal.Length 1.44 setosa 1 Petal.Width 0.2

Page 31: 20140920 tokyo r43

spread()• reshape2のdcast()相当。縦→横変換

Sepal .Length

Sepal .Width

Petal .Length

Petal .Width Species

5.1 3.5 1.4 0.2 setosa・・・ ・・・ ・・・ ・・・ ・・・

Species variable valuesetosa Sepal.Length 5.1setosa Sepal.Width 3.5

setosa Petal.Length 1.4

setosa Petal.Width 0.2・・・ ・・・ ・・・

Page 32: 20140920 tokyo r43

spread()• reshape2のdcast()相当。縦→横変換

> iris %>% head(1) %>% name_rows() %>% + gather(variable, value, -.rownames, -Species) Species .rownames variable value1 setosa 1 Sepal.Length 5.12 setosa 1 Sepal.Width 3.53 setosa 1 Petal.Length 1.44 setosa 1 Petal.Width 0.2> iris %>% head(1) %>% name_rows() %>% + gather(variable, value, -.rownames, -Species) %>%+ spread(variable, value) Species .rownames Sepal.Length Sepal.Width Petal.Length Petal.Width1 setosa 1 5.1 3.5 1.4 0.2

Page 33: 20140920 tokyo r43

separate()• reshape2のcolsplit()相当。キー列を分割する

variable valueSepal.Length 5.1Sepal.Width 3.5Petal.Length 1.4Petal.Width 0.2

・・・ ・・・

part variable valueSepal Length 5.1Sepal Width 3.5Petal Length 1.4Petal Width 0.2・・・ ・・・ ・・・

デフォルトの区切り文字はアルファベット以外

Page 34: 20140920 tokyo r43

separate()• reshape2のcolsplit()相当。キー列を分割する

> iris %>% head(1) %>%+ gather(variable, value, -Species) %>%+ separate(variable, c("part", "variable")) Species part variable value1 setosa Sepal Length 5.12 setosa Sepal Width 3.53 setosa Petal Length 1.44 setosa Petal Width 0.2

Page 35: 20140920 tokyo r43

unite()• 列を結合する

Sp-part variablesetosa_Sepal Lengthsetosa_Sepal Widthsetosa_Petal Lengthsetosa_Petal Width

・・・ ・・・

Species Part variablesetosa Sepal Lengthsetosa Sepal Widthsetosa Petal Lengthsetosa Petal Width・・・ ・・・ ・・・

デフォルトの区切り文字は”_”

Page 36: 20140920 tokyo r43

unite()• 列を結合する

> iris %>% head(1) %>%+ gather(variable, value, -Species) %>%+ separate(variable, c("part", "variable")) %>%+ unite("Sp_Part", Species,part) Sp_Part variable value1 setosa_Sepal Length 5.12 setosa_Sepal Width 3.53 setosa_Petal Length 1.44 setosa_Petal Width 0.2

Page 37: 20140920 tokyo r43

tidyrまとめ• gather()

• 横→縦変換(reshape2のmelt) • spread()

• 縦→横変換(reshape2のdcast) • separate()

• 列の分割 • unite()

• 列の結合

Page 38: 20140920 tokyo r43

•・ GGaarrbbaaggee IInn,, GGaarrbbaaggee OOuutt..ゴミを入�れればゴミが出てくる

!

•・ 前処理重要!知識を共有させてください

Page 39: 20140920 tokyo r43