20140920 tokyo r43

Preview:

DESCRIPTION

TokyoR#43

Citation preview

前処理入門!

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

> summary(kashitan)

• TwitterID : @kashitan

• お仕事 : 某通信会社

セクシーになりたい

現実は

ひたすら前処理の日々

前処理を楽にしたい

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

2回でシリーズ終了

ということで

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

Agenda1. caretパッケージ

• dummyVars()

• nearZeroVar()

• findLinearCombos()

• preProcess()

2. tidyrパッケージ

• gather()

• spread()

• separate()

• unite()

caretによる前処理

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

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

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 ...

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の方が便利 !

!

!

!

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・・・ ・・・

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 ...

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

peoe_vsa.2.1

0.000000

0.000000

0.000000

0.000000

0.000000

19.760620

0.000000

・・・

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

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

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なら問題なし

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

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

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

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

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

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()実行するのが

面倒

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

caretまとめ• dummyVars()

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

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

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

• まとめて標準化する

tidyrによる前処理

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

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・・・ ・・・ ・・・

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

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・・・ ・・・ ・・・

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

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・・・ ・・・ ・・・

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

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

unite()• 列を結合する

Sp-part variablesetosa_Sepal Lengthsetosa_Sepal Widthsetosa_Petal Lengthsetosa_Petal Width

・・・ ・・・

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

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

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

tidyrまとめ• gather()

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

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

• 列の分割 • unite()

• 列の結合

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

!

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

Recommended