ํ์ฌ sklearn์๋ ๊ณ์ธตํ๋ ๊ทธ๋ฃน kfold ๊ธฐ๋ฅ์ด ์์ต๋๋ค. ๊ณ์ธตํ๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ ๊ทธ๋ฃน kfold๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ๋ ๋ค ์์ผ๋ฉด ์ข์ ๊ฒ์ ๋๋ค.
์ฐ๋ฆฌ๊ฐ ๊ทธ๊ฒ์ ๊ฐ๊ธฐ๋ก ๊ฒฐ์ ํ๋ค๋ฉด ๊ทธ๊ฒ์ ๊ตฌํํ๊ณ ์ถ์ต๋๋ค.
@TomDLT @NicolasHug ์ด๋ป๊ฒ ์๊ฐํ์ธ์?
์ด๋ก ์ ์ผ๋ก๋ ํฅ๋ฏธ๋ก์ธ ์ ์์ง๋ง ์ค์ ๋ก ์ผ๋ง๋ ์ ์ฉํ ์ง ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ์ฐ๋ฆฌ๋ ํ์คํ ๋ฌธ์ ๋ฅผ ์ด์ด๋๊ณ ์ผ๋ง๋ ๋ง์ ์ฌ๋๋ค์ด ์ด ๊ธฐ๋ฅ์ ์์ฒญํ๋์ง ํ์ธํ ์ ์์ต๋๋ค.
๊ฐ ๊ทธ๋ฃน์ด ๋จ์ผ ํด๋์ค์ ์๋ค๊ณ ๊ฐ์ ํฉ๋๊น?
์ฐธ์กฐ #9413
@jnothman ์, ๋น์ทํ ๊ฒ์ ์ผ๋์ ๋์์ต๋๋ค. ๊ทธ๋ฌ๋ pull ์์ฒญ์ด ์์ง ์ด๋ ค ์์์ ์ ์ ์์ต๋๋ค. ๋๋ ๊ทธ๋ฃน์ด ํด๋์์ ๋ฐ๋ณต๋์ง ์์ ๊ฒ์์ ์๋ฏธํ์ต๋๋ค. ๊ทธ๋ฃน์ผ๋ก ID๊ฐ ์๋ ๊ฒฝ์ฐ ๋์ผํ ID๊ฐ ์ฌ๋ฌ ํด๋์์ ๋ฐ์ํ์ง ์์ต๋๋ค.
RFECV ์ฌ์ฉ๊ณผ ๊ด๋ จ์ด ์์์ ์ดํดํฉ๋๋ค.
ํ์ฌ ๊ธฐ๋ณธ์ ์ผ๋ก StratifiedKFold cv๋ฅผ ์ฌ์ฉํฉ๋๋ค. ๊ทธ๊ฒ์ fit()์ ๋ํ ๊ทธ๋ฃน์ ์ทจํฉ๋๋ค=
๊ทธ๋ฌ๋ fit()์ ์คํํ ๋ ๊ทธ๋ฃน์ด ์กด์ค๋์ง ์๋ ๊ฒ์ผ๋ก ๋ณด์
๋๋ค. ๊ฒฝ๊ณ ์์(๋ฒ๊ทธ๋ก ๊ฐ์ฃผ๋ ์ ์์).
๊ทธ๋ฃนํ ๋ฐ ๊ณ์ธตํ๋ ๋ ์ฝ๋ ๊ฐ ์ข
์์ฑ์ด ์๋ ๋ถ๊ท ํํ ๋ฐ์ดํฐ ์ธํธ์ ์ ์ฉํฉ๋๋ค.
(์ ์ ๊ฒฝ์ฐ ๊ฐ์ ๊ฐ์ธ์ด ์ฌ๋ฌ ๊ธฐ๋ก์ ๊ฐ์ง๊ณ ์์ง๋ง ๋ถํ ์์ ๋นํด ์ฌ์ ํ ๋ง์ ์์ ๊ทธ๋ฃน=์ฌ๋์ด ์์ต๋๋ค. ๋ถํ ์).
๊ทธ๋์: +1!
์ด๊ฒ์ ํ์คํ ์ ์ฉํ ๊ฒ์ ๋๋ค. ์๋ฅผ ๋ค์ด, ๋ถ๊ท ํ์ด ์ฌํ ์๊ณ์ด ์๋ฃ ๋ฐ์ดํฐ๋ก ์์ ํ์ฌ ํ์๋ฅผ ๋ถ๋ฆฌํ์ง๋ง ๊ฐ ์ ํ์์ ๋ถ๊ท ํ ํด๋์ค์ (๋๋ต) ๊ท ํ์ ์ ์งํฉ๋๋ค.
๋ํ StratifiedKFold๋ ๊ทธ๋ฃน์ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํ์ง๋ง ๊ทธ๋ฃน์ ๋ฐ๋ผ ๊ทธ๋ฃนํํ์ง ์์ผ๋ฏ๋ก ํ๋๊ทธ๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค.
์ด ๊ธฐ๋ฅ์ ๋ ๋ค๋ฅธ ์ข์ ์ฌ์ฉ์ ์ผ๋ฐ์ ์ผ๋ก ๋งค์ฐ ๋ถ๊ท ํํ ์ฌ๋ฌด ๋ฐ์ดํฐ์
๋๋ค. ์ ๊ฒฝ์ฐ์๋ ๋์ผํ ์ํฐํฐ(๋ค๋ฅธ ์์ )์ ๋ํ ์ฌ๋ฌ ๋ ์ฝ๋๊ฐ ์๋ ๋งค์ฐ ๋ถ๊ท ํํ ๋ฐ์ดํฐ ์ธํธ๊ฐ ์์ต๋๋ค. ์ฐ๋ฆฌ๋ ๋์ถ์ ํผํ๊ธฐ ์ํด GroupKFold
๋ฅผ ํ๊ณ ์ถ์ง๋ง ๋์ ๋ถ๊ท ํ์ผ๋ก ์ธํด ์์ฑ์ด ๊ฑฐ์ ๋๋ ์ ํ ์๋ ๊ทธ๋ฃน์ผ๋ก ๋๋ ์ ์๊ธฐ ๋๋ฌธ์ ๋ํ ๊ณ์ธตํํฉ๋๋ค.
๋ํ #14524๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
Stratified GroupShuffleSplit ๋ฐ GroupKFold์ ๋ ๋ค๋ฅธ ์ฌ์ฉ ์ฌ๋ก๋ ์๋ฌผํ์ "๋ฐ๋ณต ์ธก์ " ์ค๊ณ๋ก, ํผํ์ ๋๋ ๋ค๋ฅธ ์์ ์๋ฌผํ์ ๋จ์๋น ์ฌ๋ฌ ์ํ์ด ์์ต๋๋ค. ๋ํ ์๋ฌผํ์ ๋ง์ ์ค์ ๋ฐ์ดํฐ ์ธํธ์๋ ๊ณ๊ธ ๋ถ๊ท ํ์ด ์์ต๋๋ค. ๊ฐ ์ํ ๊ทธ๋ฃน์๋ ๋์ผํ ํด๋์ค๊ฐ ์์ต๋๋ค. ๋ฐ๋ผ์ ๊ทธ๋ฃน์ ๊ณ์ธตํํ๊ณ ํจ๊ป ์ ์งํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
์ค๋ช
ํ์ฌ sklearn์๋ ๊ณ์ธตํ๋ ๊ทธ๋ฃน kfold ๊ธฐ๋ฅ์ด ์์ต๋๋ค. ๊ณ์ธตํ๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ ๊ทธ๋ฃน kfold๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ๋ ๋ค ์์ผ๋ฉด ์ข์ ๊ฒ์ ๋๋ค.
์ฐ๋ฆฌ๊ฐ ๊ทธ๊ฒ์ ๊ฐ๊ธฐ๋ก ๊ฒฐ์ ํ๋ค๋ฉด ๊ทธ๊ฒ์ ๊ตฌํํ๊ณ ์ถ์ต๋๋ค.
์๋ ํ์ธ์, ์ํ ML์ ๊ฝค ์ ์ฉํ ๊ฒ ๊ฐ์ต๋๋ค. ์ด๋ฏธ ๊ตฌํ๋์ด ์์ต๋๊น?
@amueller ์ฌ๋๋ค์ด ์ด๊ฒ์ ๊ด์ฌ์ด ์๋ ๊ฒ์ ๊ฐ์ํ ๋ ์ด๊ฒ์ ๊ตฌํํด์ผ ํ๋ค๊ณ ์๊ฐํ์ญ๋๊น?
์ ๋ ๋งค์ฐ ๊ด์ฌ์ด ๋ง์ต๋๋ค... ๊ฐ ์ํ์ ๋ํด ์ฌ๋ฌ ๊ฐ์ ๋ฐ๋ณต ์ธก์ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ ๋ถ๊ดํ์์ ์ ๋ง ์ ์ฉํ ๊ฒ์
๋๋ค. ๊ต์ฐจ ๊ฒ์ฆ ์ค์ ๋์ผํ ์ ๊ธฐ ์ํ๋ฅผ ์ ์งํด์ผ ํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ถ๋ฅํ๋ ค๋ ๋ถ๊ท ํ ํด๋์ค๊ฐ ์ฌ๋ฌ ๊ฐ ์๋ ๊ฒฝ์ฐ ๊ณ์ธตํ ๊ธฐ๋ฅ๋ ์ฌ์ฉํ๊ณ ์ถ์ ๊ฒ์
๋๋ค. ๊ทธ๋์ ๋๋ ํํ! ๊ฐ๋ฐ์ ์ฐธ์ฌํ๊ธฐ์๋ ๋ฏธํกํ์ง๋ง ์ฐธ์ฌํ์ค ๋ถ๋ค์ ์ํด ์์ฌํ๊ณ ์ฌ์ฉํ ์ ์์ด์ ์ฃ์กํฉ๋๋ค :-)
๋ชจ๋ ํ์ ์ํด ์์ง์๊ฐ๋ฝ์ ์น์ผ์ธ์๋๋ค. ๊ฐ์ฌ!
StratifiedGroupKFold
์ ๋ํ ์์
์ด ์๋๋์์ผ๋ฏ๋ก ์ด ์ค๋ ๋์์ ์ฐธ์กฐ๋ ๋ฌธ์ ๋ฐ PR์ ์ดํด๋ณด์ญ์์ค. ๋๋ ์ด๋ฏธ ํ
์คํธ๊ฐ ํ์ํ StratifiedGroupShuffleSplit
#15239๋ฅผ ์ํํ์ง๋ง ์ด๋ฏธ ๋ด ์์
์ ๊ฝค ๋ง์ด ์ฌ์ฉํ์ต๋๋ค.
์ฐ๋ฆฌ๊ฐ ๊ตฌํํด์ผ ํ๋ค๊ณ ์๊ฐํ์ง๋ง, ์ฐ๋ฆฌ๊ฐ ์ค์ ๋ก ์ํ๋ ๊ฒ์ด ๋ฌด์์ธ์ง ์์ง ๋ชจ๋ฅธ๋ค๊ณ ์๊ฐํฉ๋๋ค. @hermidalc ๋ ๋์ผํ ๊ทธ๋ฃน์ ๊ตฌ์ฑ์์ด ๋์ผํ ํด๋์ค์ ์์ด์ผ ํ๋ค๋ ์ ํ์ด ์์ต๋๋ค. ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ๋ ์๋์์์?
๊ด์ฌ ์๋ ์ฌ๋๋ค์ด ์์ ์ ์ฌ์ฉ ์ฌ๋ก์ ์ด๋ก๋ถํฐ ์ง์ ์ํ๋ ๊ฒ์ด ๋ฌด์์ธ์ง ์ค๋ช ํ ์ ์๋ค๋ฉด ์ข์ ๊ฒ์ ๋๋ค.
#15239 #14524 ๋ฐ #9413์ด ์๋๋ฐ ๋ชจ๋ ๋ค๋ฅธ ์๋ฏธ๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฒ์ผ๋ก ๊ธฐ์ตํฉ๋๋ค.
@amueller ๋ ์ ์ ์ผ๋ก ๋์ํฉ๋๋ค. ์ ๋ ์ค๋ ์ฌ์ฉ ๊ฐ๋ฅํ ์ฌ๋ฌ ๋ฒ์ (#15239 #14524 ๋ฐ #9413) ์ฌ์ด์์ ๋ญ๊ฐ๋ฅผ ์ฐพ๋ ๋ฐ ๋ช ์๊ฐ์ ๋ณด๋์ง๋ง ์ด ์ค ์ด๋ ๊ฒ์ด ์ ํ์์ ๋ง๋์ง ์ ๋ง ์ดํดํ ์ ์์์ต๋๋ค. ๋์์ด ๋ ์ ์๋ค๋ฉด ์ฌ๊ธฐ ๋ด ์ฌ์ฉ ์ฌ๋ก๊ฐ ์์ต๋๋ค.
1000๊ฐ์ ์ํ์ด ์์ต๋๋ค. ๊ฐ ์ํ์ NIR ๋ถ๊ด๊ณ๋ก 3๋ฒ ์ธก์ ๋์์ผ๋ฏ๋ก ๊ฐ ์ํ์๋ 3๋ฒ์ ๋ณต์ ๊ฐ ํฌํจ๋์ด ๊ณ์ ํจ๊ป ์ ์งํ๊ณ ์ถ์ต๋๋ค...
์ด 1000๊ฐ์ ์ํ์ ๊ฐ๊ฐ ์ํ ์๊ฐ ๋งค์ฐ ๋ค๋ฅธ 6๊ฐ์ง ํด๋์ค์ ์ํฉ๋๋ค.
ํด๋์ค 1: 400๊ฐ ์ํ
ํด๋์ค 2: 300๊ฐ ์ํ
ํด๋์ค 3: 100๊ฐ ์ํ
ํด๋์ค 4: 100๊ฐ ์ํ
ํด๋์ค 5: 70๊ฐ ์ํ
ํด๋์ค 6: 30๊ฐ ์ํ
๊ฐ ํด๋์ค์ ๋ํ ๋ถ๋ฅ๊ธฐ๋ฅผ ๋ง๋ค๊ณ ์ถ์ต๋๋ค. ๋ฐ๋ผ์ ํด๋์ค 1 ๋ ๋ค๋ฅธ ๋ชจ๋ ํด๋์ค, ํด๋์ค 2 ๋ ๋ค๋ฅธ ๋ชจ๋ ํด๋์ค ๋ฑ์
๋๋ค.
๋ด ๋ถ๋ฅ๊ธฐ ๊ฐ๊ฐ์ ์ ํ๋๋ฅผ ์ต๋ํํ๋ ค๋ฉด ๊ฐ ์ ๊ธฐ์ ํํ๋ 6๊ฐ ํด๋์ค์ ์ํ์ ๊ฐ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค. ๋ด ํด๋์ค๊ฐ ๊ทธ๋ ๊ฒ ๋ค๋ฅด์ง ์๊ธฐ ๋๋ฌธ์ ํญ์ 6๊ฐ ํด๋์ค๊ฐ ํํ๋๋๋ก ์ ํํ ๊ฒฝ๊ณ๋ฅผ ๋ง๋๋ ๋ฐ ์ค์ ๋ก ๋์์ด ๋๊ธฐ ๋๋ฌธ์
๋๋ค. ๊ฐ ์ ๊ธฐ์์.
์ด๊ฒ์ด ๋ด๊ฐ ๊ณ์ธตํ๋(ํญ์ ๊ฐ ํด๋์ 6๊ฐ์ ํด๋์ค๊ฐ ํ์๋จ) ๊ทธ๋ฃน(๊ฐ ์ํ์ 3๊ฐ์ ๋ฐ๋ณต ์ธก์ ๊ฐ์ ํญ์ ํจ๊ป ์ ์ง) kfold๊ฐ ๋ด๊ฐ ์ฌ๊ธฐ์ ์ฐพ๊ณ ์๋ ๊ฒ๊ณผ ๋งค์ฐ ์ ์ฌํ๋ค๊ณ ๋ฏฟ๋ ์ด์ ์
๋๋ค.
์ด๋ค ์๊ฒฌ์ด ์์ต๋๊น?
๋ด ์ฌ์ฉ ์ฌ๋ก์ ๋ด๊ฐ StratifiedGroupShuffleSplit
๋ฅผ ์ด ์ด์ ๋ https://en.wikipedia.org/wiki/Repeated_measures_design ๋ฐ๋ณต ์ธก์ ๋์์ธ์ ์ง์ํ๊ธฐ ์ํ ๊ฒ์
๋๋ค. ๋ด ์ฌ์ฉ ์ฌ๋ก์์ ๋์ผํ ๊ทธ๋ฃน์ ๊ตฌ์ฑ์์ ๋์ผํ ํด๋์ค์ ์์ด์ผ ํฉ๋๋ค.
@fcoppey ์ฌ๋ฌ๋ถ, ๊ทธ๋ฃน ๋ด์ ์ํ์ ํญ์ ๊ฐ์ ํด๋์ค๋ฅผ ๊ฐ์ง๊ณ ์์ฃ ?
@hermidalc ๋๋ ์ฉ์ด์ ์ต์ํ์ง ์์ง๋ง wikipedia์์ "๋ฐ๋ณต ์ธก์ ๋์์ธ"์ "ํฌ๋ก์ค ์ค๋ฒ ์ํ์๋ ๋ฐ๋ณต ์ธก์ ๋์์ธ์ด ์์ต๋๋ค. ๊ฐ ํ์๋ ๋ ๊ฐ์ง ์ด์์ ์ผ๋ จ์ ์น๋ฃ์ ๋ฐฐ์ ๋๋ฉฐ, ๊ทธ ์ค ํ๋๋ ํ์ค ์น๋ฃ ๋๋ ์์ฝ์ด ๋ ์ ์์ต๋๋ค."
์ด๊ฒ์ ML ์ค์ ๊ณผ ๊ด๋ จํ์ฌ ์ธก์ ์ ํตํด ๊ฐ์ธ์ด ๋ฐฉ๊ธ ์น๋ฃ๋ฅผ ๋ฐ์๋์ง ๋๋ ์์ฝ์ ๋ฐ์๋์ง ์์ธกํ๊ฑฐ๋ ํด๋น ์น๋ฃ์ ๋ฐ๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์์ธกํ๋ ค๊ณ ํ ์ ์์ต๋๋ค.
์ด๋ ์ชฝ์ด๋ ๊ฐ์ ๊ฐ์ธ์ ํด๋์ค๊ฐ ๋ฐ๋ ์ ์์ง ์์ต๋๊น?
์ด๋ฆ์ ๊ด๊ณ์์ด ๋ ์ฌ๋ ๋ชจ๋ ๋์ผํ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฒ์ฒ๋ผ ๋ค๋ฆฌ์ง๋ง ์ ๋ ๊ต์ฐจ ์ฐ๊ตฌ์์ ์ค๋ช ํ ๊ฒ๊ณผ ์ ์ฌํ ์ฌ๋ก์ ๋ํด ์๊ฐํ๊ณ ์์์ต๋๋ค. ์๋๋ฉด ์กฐ๊ธ ๋ ๊ฐ๋จํ ์๋ ์์ต๋๋ค. ์๊ฐ์ด ์ง๋๋ฉด์ ํ์๊ฐ ์ํ๊ฒ ํ๊ฑฐ๋(๋๋ ๋ซ๊ฒ) ํ์์ ๊ฒฐ๊ณผ๊ฐ ๋ฐ๋ ์ ์์ต๋๋ค.
์ค์ ๋ก ๋งํฌํ ์ํคํผ๋์ ๊ธฐ์ฌ์๋ "์ข
๋จ์ ๋ถ์ - ๋ฐ๋ณต ์ธก์ ์ค๊ณ๋ฅผ ํตํด ์ฐ๊ตฌ์์ด ์ฅ๋จ๊ธฐ ์ํฉ ๋ชจ๋์์ ์ฐธ๊ฐ์๊ฐ ์๊ฐ์ด ์ง๋จ์ ๋ฐ๋ผ ์ด๋ป๊ฒ ๋ณํํ๋์ง ๋ชจ๋ํฐ๋งํ ์ ์์ต๋๋ค."๋ผ๊ณ ๋ช
์์ ์ผ๋ก ๋ช
์๋์ด ์์ผ๋ฏ๋ก ํด๋์ค ๋ณ๊ฒฝ์ด ํฌํจ๋์ด ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
์ธก์ ์ด ๋์ผํ ์กฐ๊ฑด์์ ์ํ๋๋ค๋ ๊ฒ์ ์๋ฏธํ๋ ๋ค๋ฅธ ๋จ์ด๊ฐ ์๋ค๋ฉด ๊ทธ ๋จ์ด๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๊น?
@amueller ์, ๋ง์ต๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ์ด ์ฌ์ฉ ์ฌ๋ก๊ฐ ์๋ ์ด ๋์์ธ์ ์ฌ์ฉ ์ฌ๋ก์์ ๋งํ๊ณ ์ ํ๋ ๋ถ๋ถ์ ์์์ ์๋ชป ์ผ๋ค๋ ๊ฒ์ ๊นจ๋ฌ์์ต๋๋ค.
๋ฐ๋ณต ์ธก์ ์ค๊ณ์๋ ๋งค์ฐ ์ ๊ตํ ์ ํ์ด ๋ง์ด ์์ ์ ์์ง๋ง ๋ ์ ํ์์ ๊ทธ๋ฃน ๋ด StratifiedGroupShuffleSplit
๊ฐ ํ์ํ์ต๋๋ค. ๋์ผํ ํด๋์ค ์ ํ์ด ์ ์ง๋ฉ๋๋ค(์น๋ฃ ๋ฐ์์ ์์ธกํ ๋ ์น๋ฃ ์ ํ์ ์ธ๋ก ์ํ๋ง, ๋ค์ค ์ ์ฒ๋ฆฌ ์น๋ฃ ๋ฐ์์ ์์ธกํ ๋ ๋ค๋ฅธ ์ ์ฒด ์์น์์ ํผํ์๋น ์ํ).
์ฆ์ ์๋ํ๋ ๋ฌด์ธ๊ฐ๊ฐ ํ์ํ๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ์ฌ๋๋ค์ด ์ฌ์ฉํ ์ ์๋๋ก ๊ณต๊ฐํ๊ณ sklearn์์ ๋ฌด์ธ๊ฐ๋ฅผ ์์ํ๊ณ ์ถ์์ต๋๋ค. ๊ฒ๋ค๊ฐ ๋ด๊ฐ ์๋ชป ์๊ณ ์์ง ์๋ค๋ฉด ๊ทธ๋ฃน ํด๋์ค ๋ ์ด๋ธ ๋ด์์ ๊ณ์ธตํ ๋ ผ๋ฆฌ๋ฅผ ์ค๊ณํ๋ ๊ฒ์ด ๋ ๋ณต์กํ ์ ์์ต๋๋ค.
@amueller ํญ์ ๊ทธ๋ ์ต๋๋ค. ๊ทธ๋ค์ ์์ธก์ ์ฅ์น์ ๋ด๋ณ์ฑ์ ํฌํจํ๊ธฐ ์ํด ๋์ผํ ์ธก์ ์ ๋ณต์ ์ ๋๋ค.
@hermidalc ๋ค, ์ด ๊ฒฝ์ฐ๊ฐ ํจ์ฌ ์ฝ์ต๋๋ค. ์ผ๋ฐ์ ์ธ ์๊ตฌ ์ฌํญ์ด๋ผ๋ฉด ๊ธฐ๊บผ์ด ์ถ๊ฐํ๊ฒ ์ต๋๋ค. ์ฐ๋ฆฌ๋ ์ด๋ฆ์์ ๊ทธ๊ฒ์ด ํ๋ ์ผ์ ์ด๋ ์ ๋ ๋ช ํํ๊ฒ ํด์ผ ํ๊ณ ์ด ๋ ๋ฒ์ ์ด ๊ฐ์ ํด๋์ค์ ์์ด์ผ ํ๋์ง์ ๋ํด ์๊ฐํด์ผ ํฉ๋๋ค.
StratifiedKFold
๊ฐ ์ด๊ฒ์ ํ๋๋ก ๋ง๋๋ ๊ฒ์ ์์ฃผ ์ฌ์ธ ๊ฒ์
๋๋ค. ๋ ๊ฐ์ง ์ต์
์ด ์์ต๋๋ค. ๊ฐ ์ ๊ธฐ์ ๋น์ทํ ์์ ์ํ์ด ํฌํจ๋์ด ์๋์ง ํ์ธํ๊ฑฐ๋ ๊ฐ ์ ๊ธฐ์ ๋น์ทํ ์์ ๊ทธ๋ฃน์ด ํฌํจ๋์ด ์๋์ง ํ์ธํฉ๋๋ค.
๋ ๋ฒ์งธ ๊ฒ์ ๊ฐ๋จํ๊ฒ ์ํํ ์ ์์ต๋๋ค(๊ฐ ๊ทธ๋ฃน์ด ๋จ์ผ ํฌ์ธํธ์ธ ๊ฒ์ฒ๋ผ ๊ฐ์ฅํ๊ณ StratifiedKFold
์ ์ ๋ฌ). ๊ทธ๊ฒ์ด ๋น์ ์ด PR์์ ํ๋ ์ผ์
๋๋ค.
GroupKFold ๋๋ ๊ฐ์ฅ ์์ ์ ๊ธฐ๋ฅผ ๋จผ์ ์ถ๊ฐํ์ฌ ๋ ๊ฐ์ง๋ฅผ ๊ฒฝํ์ ์ผ๋ก ๊ตํํ๋ค๊ณ ์๊ฐํฉ๋๋ค. ์ด๊ฒ์ด ๊ณ์ธตํ๋ ๊ฒฝ์ฐ๋ก ์ด๋ป๊ฒ ๋ณํ๋ ์ง ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ๋ฐ๋ผ์ ๊ทํ์ ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉํ๊ฒ ๋์ด ๊ธฐ์ฉ๋๋ค.
๋์ผํ PR์ GroupStratifiedKFold๋ ์ถ๊ฐํด์ผ ํฉ๋๊น? ์๋๋ฉด ๋์ค์ ์ํด ๋จ๊ฒจ๋๊น์?
๋ค๋ฅธ PR์ ์ฝ๊ฐ ๋ค๋ฅธ ๋ชฉํ๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค. ๋๊ตฐ๊ฐ ๋ค๋ฅธ ์ฌ์ฉ ์ฌ๋ก๊ฐ ๋ฌด์์ธ์ง ์ธ ์ ์๋ค๋ฉด ์ข์ ๊ฒ์
๋๋ค(์ง๊ธ์ ์๊ฐ์ด ์์ ๊ฒ์
๋๋ค).
๋ชจ๋ ์ํ์ ๋์ผํ ํด๋์ค๊ฐ ์๋ ๊ทธ๋ฃน ์ ์ฝ ์กฐ๊ฑด์ ๋ณ๋๋ก ์ฒ๋ฆฌํ๋ ค๋ฉด +1์ ๋๋ค.
@hermidalc ๋ค, ์ด ๊ฒฝ์ฐ๊ฐ ํจ์ฌ ์ฝ์ต๋๋ค. ์ผ๋ฐ์ ์ธ ์๊ตฌ ์ฌํญ์ด๋ผ๋ฉด ๊ธฐ๊บผ์ด ์ถ๊ฐํ๊ฒ ์ต๋๋ค. ์ฐ๋ฆฌ๋ ์ด๋ฆ์์ ๊ทธ๊ฒ์ด ํ๋ ์ผ์ ์ด๋ ์ ๋ ๋ช ํํ๊ฒ ํด์ผ ํ๊ณ ์ด ๋ ๋ฒ์ ์ด ๊ฐ์ ํด๋์ค์ ์์ด์ผ ํ๋์ง์ ๋ํด ์๊ฐํด์ผ ํฉ๋๋ค.
๋๋ ์ด๊ฒ์ ์์ ํ ์ดํดํ์ง ๋ชปํ๊ณ ์์ต๋๋ค. StratifiedGroupShuffleSplit
๋ฐ StratifiedGroupKFold
๊ฐ ๊ทธ๋ฃน์ ๊ตฌ์ฑ์์ด ๋ค๋ฅธ ํด๋์ค์ ์ํ ์ ์๋ ๊ฒฝ์ฐ ์ฌ์ฉ์๊ฐ ๋ชจ๋ ๊ทธ๋ฃน ๊ตฌ์ฑ์์ ๋ค์์ผ๋ก ์ง์ ํ ๋ ์ ํํ ๋์ผํ ๋ถํ ๋์์ ๊ฐ์ ธ์ผ ํฉ๋๋ค. ๊ฐ์ ํด๋์ค์. ๋์ค์ ๋ด๋ถ๋ฅผ ๊ฐ์ ํ ์ ์๊ณ ๊ธฐ์กด ๋์์ด ๋์ผํ๊ฒ ๋๋ ๊ฒฝ์ฐ๋ ์ธ์ ์
๋๊น?
๋ ๋ฒ์งธ ๊ฒ์ ๊ฐ๋จํ๊ฒ ์ํํ ์ ์์ต๋๋ค(๊ฐ ๊ทธ๋ฃน์ด ๋จ์ผ ํฌ์ธํธ์ธ ๊ฒ์ฒ๋ผ ๊ฐ์ฅํ๊ณ
StratifiedKFold
์ ์ ๋ฌ). ๊ทธ๊ฒ์ด ๋น์ ์ด PR์์ ํ๋ ์ผ์ ๋๋ค.GroupKFold ๋๋ ๊ฐ์ฅ ์์ ์ ๊ธฐ๋ฅผ ๋จผ์ ์ถ๊ฐํ์ฌ ๋ ๊ฐ์ง๋ฅผ ๊ฒฝํ์ ์ผ๋ก ๊ตํํ๋ค๊ณ ์๊ฐํฉ๋๋ค. ์ด๊ฒ์ด ๊ณ์ธตํ๋ ๊ฒฝ์ฐ๋ก ์ด๋ป๊ฒ ๋ณํ๋ ์ง ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ๋ฐ๋ผ์ ๊ทํ์ ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉํ๊ฒ ๋์ด ๊ธฐ์ฉ๋๋ค.
๋์ผํ PR์ GroupStratifiedKFold๋ ์ถ๊ฐํด์ผ ํฉ๋๊น? ์๋๋ฉด ๋์ค์ ์ํด ๋จ๊ฒจ๋๊น์?
๋ค๋ฅธ PR์ ์ฝ๊ฐ ๋ค๋ฅธ ๋ชฉํ๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค. ๋๊ตฐ๊ฐ ๋ค๋ฅธ ์ฌ์ฉ ์ฌ๋ก๊ฐ ๋ฌด์์ธ์ง ์ธ ์ ์๋ค๋ฉด ์ข์ ๊ฒ์ ๋๋ค(์ง๊ธ์ ์๊ฐ์ด ์์ ๊ฒ์ ๋๋ค).
๋ด๊ฐ ์ฌ์ฉํ "๊ฐ ๊ทธ๋ฃน ๋จ์ผ ์ํ" ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉํ์ฌ StatifiedGroupKFold
๋ฅผ ์ถ๊ฐํฉ๋๋ค.
๊ด์ฌ ์๋ ์ฌ๋๋ค์ด ์์ ์ ์ฌ์ฉ ์ฌ๋ก์ ์ด๋ก๋ถํฐ ์ง์ ์ํ๋ ๊ฒ์ด ๋ฌด์์ธ์ง ์ค๋ช ํ ์ ์๋ค๋ฉด ์ข์ ๊ฒ์ ๋๋ค.
์ธก์ ์ ๋ฐ๋ณตํด์ผ ํ๋ ๊ฒฝ์ฐ ์ํ ๋ฐ ์๋ฌผํ์์ ๋งค์ฐ ์ผ๋ฐ์ ์ธ ์ฌ์ฉ ์ฌ๋ก์
๋๋ค.
์: MR ์ด๋ฏธ์ง์์ ์์ธ ํ์ด๋จธ๋ณ(AD) ๋ ๊ฑด๊ฐํ ๋์กฐ๊ตฐ๊ณผ ๊ฐ์ ์ง๋ณ์ ๋ถ๋ฅํ๋ ค๊ณ ํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค. ๋์ผํ ์ฃผ์ ์ ๋ํด ์ฌ๋ฌ ์ค์บ์ด ์์ ์ ์์ต๋๋ค(์ถ์ ์ธ์
๋๋ ์ข
๋จ ๋ฐ์ดํฐ์์). ์ด 1000๋ช
์ ํผํ์๊ฐ ์๊ณ ๊ทธ ์ค 200๋ช
์ด AD(๋ถ๊ท ํ ํด๋์ค)๋ก ์ง๋จ๋๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค. ๋๋ถ๋ถ์ ํผ์ฌ์ฒด๋ ํ ๋ฒ ์ค์บํ์ง๋ง ์ผ๋ถ๋ 2~3๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ถ๋ฅ๊ธฐ๋ฅผ ํ๋ จ/ํ
์คํธํ ๋ ๋ฐ์ดํฐ ๋์ถ์ ๋ฐฉ์งํ๊ธฐ ์ํด ๋์ผํ ์ฃผ์ ์ ์ด๋ฏธ์ง๊ฐ ํญ์ ๋์ผํ ์ ํ์ ์๋์ง ํ์ธํ๋ ค๊ณ ํฉ๋๋ค.
์ด๋ฅผ ์ํด StratifiedGroupKFold๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ฐ์ฅ ์ข์ต๋๋ค. ํด๋์ค ๋ถ๊ท ํ์ ์ค๋ช
ํ๊ธฐ ์ํด ๊ณ์ธตํํ์ง๋ง ์ฃผ์ ๊ฐ ๋ค๋ฅธ ํด๋์ ๋ํ๋์ง ์์์ผ ํ๋ค๋ ๊ทธ๋ฃน ์ ์ฝ ์กฐ๊ฑด์ด ์์ต๋๋ค.
์ฃผ์: ๋ฐ๋ณต ๊ฐ๋ฅํ๊ฒ ๋ง๋๋ ๊ฒ์ด ์ข์ต๋๋ค.
์๋๋ kaggle-kernel ์์ ์๊ฐ์ ๋ฐ์ ๊ตฌํ ์์ ๋๋ค.
import numpy as np
from collections import Counter, defaultdict
from sklearn.utils import check_random_state
class RepeatedStratifiedGroupKFold():
def __init__(self, n_splits=5, n_repeats=1, random_state=None):
self.n_splits = n_splits
self.n_repeats = n_repeats
self.random_state = random_state
# Implementation based on this kaggle kernel:
# https://www.kaggle.com/jakubwasikowski/stratified-group-k-fold-cross-validation
def split(self, X, y=None, groups=None):
k = self.n_splits
def eval_y_counts_per_fold(y_counts, fold):
y_counts_per_fold[fold] += y_counts
std_per_label = []
for label in range(labels_num):
label_std = np.std(
[y_counts_per_fold[i][label] / y_distr[label] for i in range(k)]
)
std_per_label.append(label_std)
y_counts_per_fold[fold] -= y_counts
return np.mean(std_per_label)
rnd = check_random_state(self.random_state)
for repeat in range(self.n_repeats):
labels_num = np.max(y) + 1
y_counts_per_group = defaultdict(lambda: np.zeros(labels_num))
y_distr = Counter()
for label, g in zip(y, groups):
y_counts_per_group[g][label] += 1
y_distr[label] += 1
y_counts_per_fold = defaultdict(lambda: np.zeros(labels_num))
groups_per_fold = defaultdict(set)
groups_and_y_counts = list(y_counts_per_group.items())
rnd.shuffle(groups_and_y_counts)
for g, y_counts in sorted(groups_and_y_counts, key=lambda x: -np.std(x[1])):
best_fold = None
min_eval = None
for i in range(k):
fold_eval = eval_y_counts_per_fold(y_counts, i)
if min_eval is None or fold_eval < min_eval:
min_eval = fold_eval
best_fold = i
y_counts_per_fold[best_fold] += y_counts
groups_per_fold[best_fold].add(g)
all_groups = set(groups)
for i in range(k):
train_groups = all_groups - groups_per_fold[i]
test_groups = groups_per_fold[i]
train_indices = [i for i, g in enumerate(groups) if g in train_groups]
test_indices = [i for i, g in enumerate(groups) if g in test_groups]
yield train_indices, test_indices
RepeatedStratifiedKFold
(๊ฐ์ ๊ทธ๋ฃน์ ์ํ์ด ๋ ํด๋์ ๋ํ๋ ์ ์์)์ RepeatedStratifiedGroupKFold
๋น๊ต:
import matplotlib.pyplot as plt
from sklearn import model_selection
def plot_cv_indices(cv, X, y, group, ax, n_splits, lw=10):
for ii, (tr, tt) in enumerate(cv.split(X=X, y=y, groups=group)):
indices = np.array([np.nan] * len(X))
indices[tt] = 1
indices[tr] = 0
ax.scatter(range(len(indices)), [ii + .5] * len(indices),
c=indices, marker='_', lw=lw, cmap=plt.cm.coolwarm,
vmin=-.2, vmax=1.2)
ax.scatter(range(len(X)), [ii + 1.5] * len(X), c=y, marker='_',
lw=lw, cmap=plt.cm.Paired)
ax.scatter(range(len(X)), [ii + 2.5] * len(X), c=group, marker='_',
lw=lw, cmap=plt.cm.tab20c)
yticklabels = list(range(n_splits)) + ['class', 'group']
ax.set(yticks=np.arange(n_splits+2) + .5, yticklabels=yticklabels,
xlabel='Sample index', ylabel="CV iteration",
ylim=[n_splits+2.2, -.2], xlim=[0, 100])
ax.set_title('{}'.format(type(cv).__name__), fontsize=15)
# demonstration
np.random.seed(1338)
n_splits = 4
n_repeats=5
# Generate the class/group data
n_points = 100
X = np.random.randn(100, 10)
percentiles_classes = [.4, .6]
y = np.hstack([[ii] * int(100 * perc) for ii, perc in enumerate(percentiles_classes)])
# Evenly spaced groups
g = np.hstack([[ii] * 5 for ii in range(20)])
fig, ax = plt.subplots(1,2, figsize=(14,4))
cv_nogrp = model_selection.RepeatedStratifiedKFold(n_splits=n_splits,
n_repeats=n_repeats,
random_state=1338)
cv_grp = RepeatedStratifiedGroupKFold(n_splits=n_splits,
n_repeats=n_repeats,
random_state=1338)
plot_cv_indices(cv_nogrp, X, y, g, ax[0], n_splits * n_repeats)
plot_cv_indices(cv_grp, X, y, g, ax[1], n_splits * n_repeats)
plt.show()
stratifiedGroupKfold์ ๊ฒฝ์ฐ +1์ ๋๋ค. samrt ์๊ณ์์ ์ผ์๋ฅผ ๊ฐ์ ธ์ ๋ ธ์ธ์ ๋์์ ๊ฐ์งํ๋ ค๊ณ ํฉ๋๋ค. ์ฐ๋ฆฌ๋ ๊ฐ์ ๋ฐ์ดํฐ๊ฐ ๋ง์ง ์๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ๋ฑ๊ธ์ ๊ฐ์ง ๋ค๋ฅธ ์๊ณ๋ก ์๋ฎฌ๋ ์ด์ ์ ํฉ๋๋ค. ๋ํ ๋ฐ์ดํฐ๋ฅผ ํ๋ จํ๊ธฐ ์ ์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ฐํฉ๋๋ค. ๊ฐ ๋ฐ์ดํฐ ํฌ์ธํธ์์ 9๊ฐ์ ํฌ์ธํธ๋ฅผ ๋ง๋ค๊ณ ์ด๊ฒ์ด ๊ทธ๋ฃน์ ๋๋ค. ๊ทธ๋ฃน์ด ์ค๋ช ๋ ๋๋ก ํ๋ จ๊ณผ ํ ์คํธ๋ฅผ ๋ชจ๋ ์ํํ์ง ์๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
StratifiedGroupKFold๋ ์ฌ์ฉํ ์ ์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค. ์ ๋ ๊ธ์ต ์๊ธฐ๋ฅผ ์์ธกํ๊ธฐ ์ํ ๋ฐ์ดํฐ ์ธํธ๋ฅผ ๋ณด๊ณ ์์ต๋๋ค. ๊ฐ ์๊ธฐ ์ , ํ, ๋์์ ์์ฒด ๊ทธ๋ฃน์ ๋๋ค. ํ๋ จ ๋ฐ ๊ต์ฐจ ๊ฒ์ฆ ์ค์ ๊ฐ ๊ทธ๋ฃน์ ๊ตฌ์ฑ์์ ํด๋ ์ฌ์ด์์ ๋์ถ๋์ด์๋ ์ ๋ฉ๋๋ค.
์ด์จ๋ ๋ค์ค ๋ ์ด๋ธ ์๋๋ฆฌ์ค(Multilabel_
๊ณ์ธตํ ๊ทธ๋ฃนKfold)?
์ด๊ฒ์ ๋ํด +1. ์คํธ์ ๋ํ ์ฌ์ฉ์ ๊ณ์ ์ ๋ถ์ํ๊ณ ์์ผ๋ฏ๋ก ์ฌ์ฉ์๋ณ๋ก ๊ทธ๋ฃนํํ๊ณ ์ถ์ง๋ง ์คํธ์ ์๋์ ์ผ๋ก ๋ฐ์๋ฅ ์ด ๋ฎ๊ธฐ ๋๋ฌธ์ ๊ณ์ธตํํ๊ธฐ๋ ํฉ๋๋ค. ์ฐ๋ฆฌ์ ์ฌ์ฉ ์ฌ๋ก์์๋ ํ ๋ฒ ์คํธ์ ๋ณด๋ธ ์ฌ์ฉ์๋ ๋ชจ๋ ๋ฐ์ดํฐ์์ ์คํธ ๋ฐ์ก์๋ก ํ์๋๋ฏ๋ก ๊ทธ๋ฃน ๊ตฌ์ฑ์์ ํญ์ ๋์ผํ ๋ ์ด๋ธ์ ๊ฐ๊ฒ ๋ฉ๋๋ค.
๋ฌธ์๋ฅผ ๊ตฌ์ฑํ๊ธฐ ์ํ ๊ณ ์ ์ ์ธ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ์ ๊ณตํด ์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค.
@philip-iv!
StratifiedGroupShuffleSplit
์ ๋์ผํ PR #15239์ StratifiedGroupKFold
๊ตฌํ์ ์ถ๊ฐํ์ต๋๋ค.
PR์์ ๋ณผ ์ ์๋ฏ์ด ๋ ๋ค์ ๋ํ ๋
ผ๋ฆฌ๋ https://github.com/scikit-learn/scikit-learn/issues/13621#issuecomment -557802602๋ณด๋ค ํจ์ฌ ๊ฐ๋จํฉ๋๋ค. ๊ณ ์ ํ ๊ทธ๋ฃน ์ ๋ณด๋ฅผ ์ ๋ฌํ์ฌ ๊ธฐ์กด StratifiedKFold
๋ฐ StratifiedShuffleSplit
์ฝ๋๋ฅผ ํ์ฉํ ์ ์๋๋ก ๊ฐ ํด๋์ค(์ํ์ ๋ฐฑ๋ถ์จ ์๋). ๊ทธ๋ฌ๋ ๋ ๊ตฌํ ๋ชจ๋ ๊ฐ ๊ทธ๋ฃน์ ์ํ์ด ๋์ผํ ํด๋์ ํจ๊ป ์ ์ง๋๋ ํด๋๋ฅผ ์์ฑํฉ๋๋ค.
https://github.com/scikit-learn/scikit-learn/issues/13621#issuecomment -557802602๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ ์ ๊ตํ ๋ฐฉ๋ฒ์ ํฌํํ๊ฒ ์ง๋ง
๋ค์์ @mrunibe ์์ ์ ๊ณตํ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ StratifiedGroupKFold
๋ฐ RepeatedStratifiedGroupKFold
์ ๋ณธ๊ฒฉ์ ์ธ ๋ฒ์ ์
๋๋ค. ์ด ์ฝ๋๋ ๋ช ๊ฐ์ง๋ฅผ ๋์ฑ ๋จ์ํํ๊ณ ๋ณ๊ฒฝํ์ต๋๋ค. ์ด ํด๋์ค๋ ๋ํ ๋์ผํ ์ ํ์ ๋ค๋ฅธ sklearn CV ํด๋์ค๊ฐ ์ํ๋๋ ๋ฐฉ์์ ์ค๊ณ๋ฅผ ๋ฐ๋ฆ
๋๋ค.
class StratifiedGroupKFold(_BaseKFold):
"""Stratified K-Folds iterator variant with non-overlapping groups.
This cross-validation object is a variation of StratifiedKFold that returns
stratified folds with non-overlapping groups. The folds are made by
preserving the percentage of samples for each class.
The same group will not appear in two different folds (the number of
distinct groups has to be at least equal to the number of folds).
The difference between GroupKFold and StratifiedGroupKFold is that
the former attempts to create balanced folds such that the number of
distinct groups is approximately the same in each fold, whereas
StratifiedGroupKFold attempts to create folds which preserve the
percentage of samples for each class.
Read more in the :ref:`User Guide <cross_validation>`.
Parameters
----------
n_splits : int, default=5
Number of folds. Must be at least 2.
shuffle : bool, default=False
Whether to shuffle each class's samples before splitting into batches.
Note that the samples within each split will not be shuffled.
random_state : int or RandomState instance, default=None
When `shuffle` is True, `random_state` affects the ordering of the
indices, which controls the randomness of each fold for each class.
Otherwise, leave `random_state` as `None`.
Pass an int for reproducible output across multiple function calls.
See :term:`Glossary <random_state>`.
Examples
--------
>>> import numpy as np
>>> from sklearn.model_selection import StratifiedGroupKFold
>>> X = np.ones((17, 2))
>>> y = np.array([0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> groups = np.array([1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8])
>>> cv = StratifiedGroupKFold(n_splits=3)
>>> for train_idxs, test_idxs in cv.split(X, y, groups):
... print("TRAIN:", groups[train_idxs])
... print(" ", y[train_idxs])
... print(" TEST:", groups[test_idxs])
... print(" ", y[test_idxs])
TRAIN: [2 2 4 5 5 5 5 6 6 7]
[1 1 1 0 0 0 0 0 0 0]
TEST: [1 1 3 3 3 8 8]
[0 0 1 1 1 0 0]
TRAIN: [1 1 3 3 3 4 5 5 5 5 8 8]
[0 0 1 1 1 1 0 0 0 0 0 0]
TEST: [2 2 6 6 7]
[1 1 0 0 0]
TRAIN: [1 1 2 2 3 3 3 6 6 7 8 8]
[0 0 1 1 1 1 1 0 0 0 0 0]
TEST: [4 5 5 5 5]
[1 0 0 0 0]
See also
--------
StratifiedKFold: Takes class information into account to build folds which
retain class distributions (for binary or multiclass classification
tasks).
GroupKFold: K-fold iterator variant with non-overlapping groups.
"""
def __init__(self, n_splits=5, shuffle=False, random_state=None):
super().__init__(n_splits=n_splits, shuffle=shuffle,
random_state=random_state)
# Implementation based on this kaggle kernel:
# https://www.kaggle.com/jakubwasikowski/stratified-group-k-fold-cross-validation
def _iter_test_indices(self, X, y, groups):
labels_num = np.max(y) + 1
y_counts_per_group = defaultdict(lambda: np.zeros(labels_num))
y_distr = Counter()
for label, group in zip(y, groups):
y_counts_per_group[group][label] += 1
y_distr[label] += 1
y_counts_per_fold = defaultdict(lambda: np.zeros(labels_num))
groups_per_fold = defaultdict(set)
groups_and_y_counts = list(y_counts_per_group.items())
rng = check_random_state(self.random_state)
if self.shuffle:
rng.shuffle(groups_and_y_counts)
for group, y_counts in sorted(groups_and_y_counts,
key=lambda x: -np.std(x[1])):
best_fold = None
min_eval = None
for i in range(self.n_splits):
y_counts_per_fold[i] += y_counts
std_per_label = []
for label in range(labels_num):
std_per_label.append(np.std(
[y_counts_per_fold[j][label] / y_distr[label]
for j in range(self.n_splits)]))
y_counts_per_fold[i] -= y_counts
fold_eval = np.mean(std_per_label)
if min_eval is None or fold_eval < min_eval:
min_eval = fold_eval
best_fold = i
y_counts_per_fold[best_fold] += y_counts
groups_per_fold[best_fold].add(group)
for i in range(self.n_splits):
test_indices = [idx for idx, group in enumerate(groups)
if group in groups_per_fold[i]]
yield test_indices
class RepeatedStratifiedGroupKFold(_RepeatedSplits):
"""Repeated Stratified K-Fold cross validator.
Repeats Stratified K-Fold with non-overlapping groups n times with
different randomization in each repetition.
Read more in the :ref:`User Guide <cross_validation>`.
Parameters
----------
n_splits : int, default=5
Number of folds. Must be at least 2.
n_repeats : int, default=10
Number of times cross-validator needs to be repeated.
random_state : int or RandomState instance, default=None
Controls the generation of the random states for each repetition.
Pass an int for reproducible output across multiple function calls.
See :term:`Glossary <random_state>`.
Examples
--------
>>> import numpy as np
>>> from sklearn.model_selection import RepeatedStratifiedGroupKFold
>>> X = np.ones((17, 2))
>>> y = np.array([0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> groups = np.array([1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8])
>>> cv = RepeatedStratifiedGroupKFold(n_splits=2, n_repeats=2,
... random_state=36851234)
>>> for train_index, test_index in cv.split(X, y, groups):
... print("TRAIN:", groups[train_idxs])
... print(" ", y[train_idxs])
... print(" TEST:", groups[test_idxs])
... print(" ", y[test_idxs])
TRAIN: [2 2 4 5 5 5 5 8 8]
[1 1 1 0 0 0 0 0 0]
TEST: [1 1 3 3 3 6 6 7]
[0 0 1 1 1 0 0 0]
TRAIN: [1 1 3 3 3 6 6 7]
[0 0 1 1 1 0 0 0]
TEST: [2 2 4 5 5 5 5 8 8]
[1 1 1 0 0 0 0 0 0]
TRAIN: [3 3 3 4 7 8 8]
[1 1 1 1 0 0 0]
TEST: [1 1 2 2 5 5 5 5 6 6]
[0 0 1 1 0 0 0 0 0 0]
TRAIN: [1 1 2 2 5 5 5 5 6 6]
[0 0 1 1 0 0 0 0 0 0]
TEST: [3 3 3 4 7 8 8]
[1 1 1 1 0 0 0]
Notes
-----
Randomized CV splitters may return different results for each call of
split. You can make the results identical by setting `random_state`
to an integer.
See also
--------
RepeatedStratifiedKFold: Repeats Stratified K-Fold n times.
"""
def __init__(self, n_splits=5, n_repeats=10, random_state=None):
super().__init__(StratifiedGroupKFold, n_splits=n_splits,
n_repeats=n_repeats, random_state=random_state)
@hermidalc ๋๋๋ก ์ด๊ฒ์ ๋๋์๋ณด๋ฉด์ ์ฐ๋ฆฌ๊ฐ ๋ฌด์์ ํด๊ฒฐํ๋์ง ์๋นํ ํผ๋์ค๋ฝ์ต๋๋ค. (์ํ๊น๊ฒ๋ ์ ์๊ฐ์ ์์ ๊ฐ์ง ์์ต๋๋ค!) scikit-learn์ ํฌํจ์ํฌ ๊ฒ์ ๊ถ์ฅํ ๋งํ ์์ด๋์ด๋ฅผ ์๋ ค์ฃผ์ค ์ ์์ต๋๊น?
@hermidalc ๋๋๋ก ์ด๊ฒ์ ๋๋์๋ณด๋ฉด์ ์ฐ๋ฆฌ๊ฐ ๋ฌด์์ ํด๊ฒฐํ๋์ง ์๋นํ ํผ๋์ค๋ฝ์ต๋๋ค. (์ํ๊น๊ฒ๋ ์ ์๊ฐ์ ์์ ๊ฐ์ง ์์ต๋๋ค!) scikit-learn์ ํฌํจ์ํฌ ๊ฒ์ ๊ถ์ฅํ ๋งํ ์์ด๋์ด๋ฅผ ์๋ ค์ฃผ์ค ์ ์์ต๋๊น?
#15239์์ ํ๋ ๊ฒ๋ณด๋ค ๋ ๋์ ๊ตฌํ์ ํ๊ณ ์ถ์์ต๋๋ค. ํด๋น PR์ ๊ตฌํ์ ์๋ํ์ง๋ง ๋ ผ๋ฆฌ๋ฅผ ๊ฐ๋จํ๊ฒ ๋ง๋ค๊ธฐ ์ํด ๊ทธ๋ฃน์ ๊ณ์ธตํํ์ง๋ง ์ด๊ฒ์ด ์ด์์ ์ด์ง๋ ์์ต๋๋ค.
๊ทธ๋์ ์์์ ๋ด๊ฐ ํ ๊ฒ์(jakubwasikowski์ @mrunibe ๋ฐ kaggle ๋๋ถ์) ์ํ์ ๊ณ์ธตํํ๋ StratifiedGroupKFold
์ ๋ ๋์ ๊ตฌํ์
๋๋ค. ๋ ๋์ StratifiedGroupShuffleSplit
๋ฅผ ์ํํ๊ธฐ ์ํด ๋์ผํ ๋
ผ๋ฆฌ๋ฅผ ์ด์ํ๊ณ ์ถ์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ์ค๋น๊ฐ ๋ฉ๋๋ค. ์ด์ ๊ตฌํ์ ๋์ฒดํ๊ธฐ ์ํด ์ ์ฝ๋๋ฅผ #15239์ ๋ฃ์ต๋๋ค.
๋ฏธ์์ฑ์ธ PR์ ๋ํด ์ฌ๊ณผ๋๋ฆฝ๋๋ค. ๋ฐ์ฌ ํ์๋ฅผ ๋ฐ๋ ์ค์ด๋ฏ๋ก ์๊ฐ์ด ์์ต๋๋ค!
๊ตฌํ์ ์ ๊ณต ํด์ฃผ์ @hermidalc ์ @mrunibe ์๊ฒ ๊ฐ์ฌ๋๋ฆฝ๋๋ค. ๋ํ ํด๋์ค ๋ถ๊ท ํ์ด ์ฌํ๊ณ ์ฃผ์ ๋น ์ํ ์๊ฐ ๋งค์ฐ ๋ค์ํ ์๋ฃ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํ StratifiedGroupKFold
๋ฐฉ๋ฒ์ ์ฐพ๊ณ ์์ต๋๋ค. GroupKFold
์์ฒด์ ์ผ๋ก ํ๋์ ํด๋์ค๋ง ํฌํจํ๋ ํ๋ จ ๋ฐ์ดํฐ ํ์ ์งํฉ์ ๋ง๋ญ๋๋ค.
๋ ๋์ StratifiedGroupShuffleSplit์ ์ํํ๊ธฐ ์ํด ๋์ผํ ๋ ผ๋ฆฌ๋ฅผ ์ด์ํ๊ณ ์ถ์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ์ค๋น๊ฐ ๋ฉ๋๋ค.
StratifiedGroupShuffleSplit
StratifiedGroupKFold
๋ณํฉ์ ํ์คํ ๊ณ ๋ คํ ์ ์์ต๋๋ค.
๋ฏธ์์ฑ์ธ PR์ ๋ํด ์ฌ๊ณผ๋๋ฆฝ๋๋ค. ๋ฐ์ฌ ํ์๋ฅผ ๋ฐ๋ ์ค์ด๋ฏ๋ก ์๊ฐ์ด ์์ต๋๋ค!
์๋ฃ ์ง์์ ์ํ์๋ฉด ์ ํฌ์๊ฒ ์๋ ค์ฃผ์ญ์์ค!
๊ทธ๋ฆฌ๊ณ ๋ฐ์ฌ ๊ณผ์ ์ ํ์ด์ ๋น๋๋ค
๋ค์์ @mrunibe ์์ ์ ๊ณตํ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ
StratifiedGroupKFold
๋ฐRepeatedStratifiedGroupKFold
์ ๋ณธ๊ฒฉ์ ์ธ ๋ฒ์ ์ ๋๋ค. ์ด ์ฝ๋๋ ๋ช ๊ฐ์ง๋ฅผ ๋์ฑ ๋จ์ํํ๊ณ ๋ณ๊ฒฝํ์ต๋๋ค. ์ด ํด๋์ค๋ ๋ํ ๋์ผํ ์ ํ์ ๋ค๋ฅธ sklearn CV ํด๋์ค๊ฐ ์ํ๋๋ ๋ฐฉ์์ ์ค๊ณ๋ฅผ ๋ฐ๋ฆ ๋๋ค.
์ด๊ฒ์ ์๋ํ๋ ๊ฒ์ด ๊ฐ๋ฅํฉ๋๊น? ๋ค์ํ ์ข ์์ฑ ์ค ์ผ๋ถ๋ฅผ ์๋ผ๋ด์ด ๋ถ์ฌ๋ฃ์ผ๋ ค๊ณ ํ์ง๋ง ๋์ด ์์์ต๋๋ค. ๋ด ํ๋ก์ ํธ์์ ์ด ์์ ์ ์๋ํด ๋ณด๊ณ ์ถ์ต๋๋ค. ์ง๊ธ ๊ฐ๋ฅํ ๋ฐฉ๋ฒ์ด ์๋์ง ํ์ธํ๋ ค๊ณ ํฉ๋๋ค.
@hermidalc ๋ฐ์ฌ ๊ณผ์ ์ด ์ฑ๊ณต์ ์ด๊ธฐ๋ฅผ ๋ฐ๋๋๋ค!
Geosciences์์ ์ ๋ฐ์ฌ ํ์ ์์
์๋ ๊ทธ๋ฃน ์ ์ด์ ํจ๊ป ์ด ๊ณ์ธตํ ๊ธฐ๋ฅ์ด ํ์ํ๊ธฐ ๋๋ฌธ์ ์ด ๊ตฌํ์ด ์๋ฃ๋๋ ๊ฒ์ ๋ณด๊ธฐ๋ฅผ ๊ณ ๋ํ๊ณ ์์ต๋๋ค. ๋ด ํ๋ก์ ํธ์์ ์๋์ผ๋ก ๋ถํ ํ๋ ์์ด๋์ด๋ฅผ ๊ตฌํํ๋ ๋ฐ ๋ช ์๊ฐ์ ๋ณด๋์ต๋๋ค. ํ์ง๋ง ๊ฐ์ ์ด์ ๋ก ๊ทธ๋ง๋๋๋ฐ...๋ฐ์ฌ๊ณผ์ . ๊ทธ๋์ ๋ฐ์ฌ ๊ณผ์ ์ด ์ด๋ป๊ฒ ์ฌ๋์ ์๊ฐ์ ๊ดด๋กญํ ์ ์๋์ง ์์ ํ ์ดํดํ ์ ์์ต๋๋ค. LOL ์๋ ฅ์ด ์์ต๋๋ค. ์ง๊ธ์ GroupShuffleSplit์ ๋์์ผ๋ก ์ฌ์ฉํฉ๋๋ค.
๊ฑด๋ฐฐ
@bfeeny @dispink ์์์ ์์ฑํ ๋ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋งค์ฐ ์ฝ์ต๋๋ค. ๋ค์์ ์ฌ์ฉํ์ฌ split.py
์ ๊ฐ์ ํ์ผ์ ๋ง๋ญ๋๋ค. ๊ทธ๋ฐ ๋ค์ ์คํฌ๋ฆฝํธ๊ฐ split.py
์ ๋์ผํ ๋๋ ํ ๋ฆฌ์ ์์ผ๋ฉด ์ฌ์ฉ์ ์ฝ๋์์ from split import StratifiedGroupKFold, RepeatedStratifiedGroupKFold
๋ฅผ ๊ฐ์ ธ์ค๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
from collections import Counter, defaultdict
import numpy as np
from sklearn.model_selection._split import _BaseKFold, _RepeatedSplits
from sklearn.utils.validation import check_random_state
class StratifiedGroupKFold(_BaseKFold):
"""Stratified K-Folds iterator variant with non-overlapping groups.
This cross-validation object is a variation of StratifiedKFold that returns
stratified folds with non-overlapping groups. The folds are made by
preserving the percentage of samples for each class.
The same group will not appear in two different folds (the number of
distinct groups has to be at least equal to the number of folds).
The difference between GroupKFold and StratifiedGroupKFold is that
the former attempts to create balanced folds such that the number of
distinct groups is approximately the same in each fold, whereas
StratifiedGroupKFold attempts to create folds which preserve the
percentage of samples for each class.
Read more in the :ref:`User Guide <cross_validation>`.
Parameters
----------
n_splits : int, default=5
Number of folds. Must be at least 2.
shuffle : bool, default=False
Whether to shuffle each class's samples before splitting into batches.
Note that the samples within each split will not be shuffled.
random_state : int or RandomState instance, default=None
When `shuffle` is True, `random_state` affects the ordering of the
indices, which controls the randomness of each fold for each class.
Otherwise, leave `random_state` as `None`.
Pass an int for reproducible output across multiple function calls.
See :term:`Glossary <random_state>`.
Examples
--------
>>> import numpy as np
>>> from sklearn.model_selection import StratifiedGroupKFold
>>> X = np.ones((17, 2))
>>> y = np.array([0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> groups = np.array([1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8])
>>> cv = StratifiedGroupKFold(n_splits=3)
>>> for train_idxs, test_idxs in cv.split(X, y, groups):
... print("TRAIN:", groups[train_idxs])
... print(" ", y[train_idxs])
... print(" TEST:", groups[test_idxs])
... print(" ", y[test_idxs])
TRAIN: [2 2 4 5 5 5 5 6 6 7]
[1 1 1 0 0 0 0 0 0 0]
TEST: [1 1 3 3 3 8 8]
[0 0 1 1 1 0 0]
TRAIN: [1 1 3 3 3 4 5 5 5 5 8 8]
[0 0 1 1 1 1 0 0 0 0 0 0]
TEST: [2 2 6 6 7]
[1 1 0 0 0]
TRAIN: [1 1 2 2 3 3 3 6 6 7 8 8]
[0 0 1 1 1 1 1 0 0 0 0 0]
TEST: [4 5 5 5 5]
[1 0 0 0 0]
See also
--------
StratifiedKFold: Takes class information into account to build folds which
retain class distributions (for binary or multiclass classification
tasks).
GroupKFold: K-fold iterator variant with non-overlapping groups.
"""
def __init__(self, n_splits=5, shuffle=False, random_state=None):
super().__init__(n_splits=n_splits, shuffle=shuffle,
random_state=random_state)
# Implementation based on this kaggle kernel:
# https://www.kaggle.com/jakubwasikowski/stratified-group-k-fold-cross-validation
def _iter_test_indices(self, X, y, groups):
labels_num = np.max(y) + 1
y_counts_per_group = defaultdict(lambda: np.zeros(labels_num))
y_distr = Counter()
for label, group in zip(y, groups):
y_counts_per_group[group][label] += 1
y_distr[label] += 1
y_counts_per_fold = defaultdict(lambda: np.zeros(labels_num))
groups_per_fold = defaultdict(set)
groups_and_y_counts = list(y_counts_per_group.items())
rng = check_random_state(self.random_state)
if self.shuffle:
rng.shuffle(groups_and_y_counts)
for group, y_counts in sorted(groups_and_y_counts,
key=lambda x: -np.std(x[1])):
best_fold = None
min_eval = None
for i in range(self.n_splits):
y_counts_per_fold[i] += y_counts
std_per_label = []
for label in range(labels_num):
std_per_label.append(np.std(
[y_counts_per_fold[j][label] / y_distr[label]
for j in range(self.n_splits)]))
y_counts_per_fold[i] -= y_counts
fold_eval = np.mean(std_per_label)
if min_eval is None or fold_eval < min_eval:
min_eval = fold_eval
best_fold = i
y_counts_per_fold[best_fold] += y_counts
groups_per_fold[best_fold].add(group)
for i in range(self.n_splits):
test_indices = [idx for idx, group in enumerate(groups)
if group in groups_per_fold[i]]
yield test_indices
class RepeatedStratifiedGroupKFold(_RepeatedSplits):
"""Repeated Stratified K-Fold cross validator.
Repeats Stratified K-Fold with non-overlapping groups n times with
different randomization in each repetition.
Read more in the :ref:`User Guide <cross_validation>`.
Parameters
----------
n_splits : int, default=5
Number of folds. Must be at least 2.
n_repeats : int, default=10
Number of times cross-validator needs to be repeated.
random_state : int or RandomState instance, default=None
Controls the generation of the random states for each repetition.
Pass an int for reproducible output across multiple function calls.
See :term:`Glossary <random_state>`.
Examples
--------
>>> import numpy as np
>>> from sklearn.model_selection import RepeatedStratifiedGroupKFold
>>> X = np.ones((17, 2))
>>> y = np.array([0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> groups = np.array([1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8])
>>> cv = RepeatedStratifiedGroupKFold(n_splits=2, n_repeats=2,
... random_state=36851234)
>>> for train_index, test_index in cv.split(X, y, groups):
... print("TRAIN:", groups[train_idxs])
... print(" ", y[train_idxs])
... print(" TEST:", groups[test_idxs])
... print(" ", y[test_idxs])
TRAIN: [2 2 4 5 5 5 5 8 8]
[1 1 1 0 0 0 0 0 0]
TEST: [1 1 3 3 3 6 6 7]
[0 0 1 1 1 0 0 0]
TRAIN: [1 1 3 3 3 6 6 7]
[0 0 1 1 1 0 0 0]
TEST: [2 2 4 5 5 5 5 8 8]
[1 1 1 0 0 0 0 0 0]
TRAIN: [3 3 3 4 7 8 8]
[1 1 1 1 0 0 0]
TEST: [1 1 2 2 5 5 5 5 6 6]
[0 0 1 1 0 0 0 0 0 0]
TRAIN: [1 1 2 2 5 5 5 5 6 6]
[0 0 1 1 0 0 0 0 0 0]
TEST: [3 3 3 4 7 8 8]
[1 1 1 1 0 0 0]
Notes
-----
Randomized CV splitters may return different results for each call of
split. You can make the results identical by setting `random_state`
to an integer.
See also
--------
RepeatedStratifiedKFold: Repeats Stratified K-Fold n times.
"""
def __init__(self, n_splits=5, n_repeats=10, random_state=None):
super().__init__(StratifiedGroupKFold, n_splits=n_splits,
n_repeats=n_repeats, random_state=random_state)
@hermidalc ๊ธ์ ์ ์ธ ๋ต๋ณ ๊ฐ์ฌํฉ๋๋ค!
์ค๋ช
ํด์ฃผ์ ๋๋ก ๋น ๋ฅด๊ฒ ์ฑํํฉ๋๋ค. ๊ทธ๋ฌ๋ ํ๋ จ ๋๋ ํ
์คํธ ์ธํธ์ ๋ฐ์ดํฐ๋ง ์๋ ๋ถํ ๋ง ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ์ฝ๋ ์ค๋ช
์ ์ดํดํ๋ ํ ํ๋ จ ์ธํธ์ ํ
์คํธ ์ธํธ ์ฌ์ด์ ๋น์จ์ ์ง์ ํ๋ ๋งค๊ฐ ๋ณ์๊ฐ ์์ต๋๊น?
๊ณ์ธตํ, ๊ทธ๋ฃน ์ ์ด ๋ฐ ๋ฐ์ดํฐ ์ธํธ ๋น์จ ๊ฐ์ ์ถฉ๋์ด๋ผ๋ ๊ฒ์ ์๊ณ ์์ต๋๋ค. ๊ทธ๋์ ๊ณ์ํ๊ธฐ๋ฅผ ํฌ๊ธฐํ์ต๋๋ค... ํ์ง๋ง ์ฌ์ ํ ํด๊ฒฐํด์ผ ํ ํํ์ ์ ์ฐพ์ ์ ์์ต๋๋ค.
์ง์ ์ผ๋ก
@hermidalc ๊ธ์ ์ ์ธ ๋ต๋ณ ๊ฐ์ฌํฉ๋๋ค!
์ค๋ช ํด์ฃผ์ ๋๋ก ๋น ๋ฅด๊ฒ ์ฑํํฉ๋๋ค. ๊ทธ๋ฌ๋ ํ๋ จ ๋๋ ํ ์คํธ ์ธํธ์ ๋ฐ์ดํฐ๋ง ์๋ ๋ถํ ๋ง ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ์ฝ๋ ์ค๋ช ์ ์ดํดํ๋ ํ ํ๋ จ ์ธํธ์ ํ ์คํธ ์ธํธ ์ฌ์ด์ ๋น์จ์ ์ง์ ํ๋ ๋งค๊ฐ ๋ณ์๊ฐ ์์ต๋๊น?
๊ณ์ธตํ, ๊ทธ๋ฃน ์ ์ด ๋ฐ ๋ฐ์ดํฐ ์ธํธ ๋น์จ ๊ฐ์ ์ถฉ๋์ด๋ผ๋ ๊ฒ์ ์๊ณ ์์ต๋๋ค. ๊ทธ๋์ ๊ณ์ํ๊ธฐ๋ฅผ ํฌ๊ธฐํ์ต๋๋ค... ํ์ง๋ง ์ฌ์ ํ ํด๊ฒฐํด์ผ ํ ํํ์ ์ ์ฐพ์ ์ ์์ต๋๋ค.
ํ
์คํธํ๊ธฐ ์ํด split.py
๋ฅผ ๋ง๋ค๊ณ ์ด ์์ ๋ฅผ ipython์์ ์คํํ๋๋ฐ ์๋ํฉ๋๋ค. ๋๋ ์ค๋ซ๋์ ๋ด ์์
์์ ์ด๋ฌํ ์ฌ์ฉ์ ์ ์ CV ๋ฐ๋ณต์๋ฅผ ์ฌ์ฉํด ์์ผ๋ฉฐ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. BTW 0.23.x๊ฐ ์๋ scikit-learn 0.22.2 ๋ฅผ ์ฌ์ฉํ๊ณ ์์ผ๋ฏ๋ก ์ด๊ฒ์ด ๋ฌธ์ ์ ์์ธ์ธ์ง ํ์คํ์ง ์์ต๋๋ค. ์๋์์ ์ด ์๋ฅผ ์คํํ๊ณ ์ฌํํ ์ ์๋์ง ํ์ธํด ์ฃผ์๊ฒ ์ต๋๊น? ๊ฐ๋ฅํ๋ค๋ฉด ์์
์ y
๋ฐ groups
๊ฐ ํฌํจ๋ ๊ฒ์ผ ์ ์์ต๋๋ค.
In [6]: import numpy as np
...: from split import StratifiedGroupKFold
...:
...: X = np.ones((17, 2))
...: y = np.array([0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
...: groups = np.array([1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8])
...: cv = StratifiedGroupKFold(n_splits=3, shuffle=True, random_state=777)
...: for train_idxs, test_idxs in cv.split(X, y, groups):
...: print("TRAIN:", groups[train_idxs])
...: print(" ", y[train_idxs])
...: print(" TEST:", groups[test_idxs])
...: print(" ", y[test_idxs])
...:
TRAIN: [2 2 4 5 5 5 5 6 6 7]
[1 1 1 0 0 0 0 0 0 0]
TEST: [1 1 3 3 3 8 8]
[0 0 1 1 1 0 0]
TRAIN: [1 1 3 3 3 4 5 5 5 5 8 8]
[0 0 1 1 1 1 0 0 0 0 0 0]
TEST: [2 2 6 6 7]
[1 1 0 0 0]
TRAIN: [1 1 2 2 3 3 3 6 6 7 8 8]
[0 0 1 1 1 1 1 0 0 0 0 0]
TEST: [4 5 5 5 5]
[1 0 0 0 0]
@hermidalc ์ด ๊ธฐ๋ฅ์ ์ ๊ธฐ์ ์ธ ๊ด์ฌ์ด ์๋ ๊ฒ ๊ฐ์ผ๋ฉฐ ์ฐ๋ฆฌ๋
๋น์ ์ด ์ ๊ฒฝ ์ฐ์ง ์๋๋ค๋ฉด ๋๋ด์ค ์ฌ๋์ ์ฐพ์ ์ ์์ ๊ฒ์
๋๋ค.
@hermidalc '๋์ผํ ๊ทธ๋ฃน์ ๋ชจ๋ ์ํ์ ๋์ผํ ํด๋์ค ๋ ์ด๋ธ์ด ์๋์ง ํ์ธํด์ผ ํฉ๋๋ค.' ๋ถ๋ช
ํ ๊ทธ๊ฒ์ด ๋ฌธ์ ์
๋๋ค. ๊ฐ์ ๊ทธ๋ฃน์ ์ํ์ด ๊ฐ์ ํด๋์ค๋ฅผ ๊ณต์ ํ์ง ์์ต๋๋ค. ์... ๊ฐ๋ฐ์ ๋ ๋ค๋ฅธ ์ง์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค.
์ด์จ๋ ๋๋จํ ๊ฐ์ฌํฉ๋๋ค.
@hermidalc '๋์ผํ ๊ทธ๋ฃน์ ๋ชจ๋ ์ํ์ ๋์ผํ ํด๋์ค ๋ ์ด๋ธ์ด ์๋์ง ํ์ธํด์ผ ํฉ๋๋ค.' ๋ถ๋ช ํ ๊ทธ๊ฒ์ด ๋ฌธ์ ์ ๋๋ค. ๊ฐ์ ๊ทธ๋ฃน์ ์ํ์ด ๊ฐ์ ํด๋์ค๋ฅผ ๊ณต์ ํ์ง ์์ต๋๋ค. ์... ๊ฐ๋ฐ์ ๋ ๋ค๋ฅธ ์ง์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค.
์ด์จ๋ ๋๋จํ ๊ฐ์ฌํฉ๋๋ค.์, ์ด๊ฒ์ ์ฌ๊ธฐ์ ๋ค์ํ ์ค๋ ๋์์ ๋ ผ์๋์์ต๋๋ค. ์ ์ฉํ ๋ ๋ค๋ฅธ ๋ ๋ณต์กํ ์ฌ์ฉ ์ฌ๋ก์ด์ง๋ง ๋์ ๊ฐ์ ๋ง์ ์ฌ๋๋ค์ ํ์ฌ ํด๋น ์ฌ์ฉ ์ฌ๋ก๊ฐ ํ์ํ์ง ์์ง๋ง ๊ทธ๋ฃน์ ํจ๊ป ์ ์งํ๋ฉด์ ์ํ์ ๊ณ์ธตํํ๋ ๋ฌด์ธ๊ฐ๊ฐ ํ์ํฉ๋๋ค. ์ ์ฝ๋์ ์๊ตฌ ์ฌํญ์ ๊ฐ ๊ทธ๋ฃน์ ๋ชจ๋ ์ํ์ด ๋์ผํ ํด๋์ค์ ์ํด์ผ ํ๋ค๋ ๊ฒ์ ๋๋ค.
์ฌ์ค @dispink ์ ๊ฐ ํ๋ ธ์ต๋๋ค. ์ด ์๊ณ ๋ฆฌ์ฆ์ ๊ทธ๋ฃน์ ๋ชจ๋ ๊ตฌ์ฑ์์ด ๊ฐ์ ํด๋์ค์ ์ํ ๊ฒ์ ์๊ตฌํ์ง ์์ต๋๋ค. ์๋ฅผ ๋ค์ด:
In [2]: X = np.ones((17, 2))
...: y = np.array([0, 2, 1, 1, 2, 0, 0, 1, 2, 1, 1, 1, 0, 2, 0, 1, 0])
...: groups = np.array([1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8])
...: cv = StratifiedGroupKFold(n_splits=3)
...: for train_idxs, test_idxs in cv.split(X, y, groups):
...: print("TRAIN:", groups[train_idxs])
...: print(" ", y[train_idxs])
...: print(" TEST:", groups[test_idxs])
...: print(" ", y[test_idxs])
...:
TRAIN: [1 1 2 2 3 3 3 4 8 8]
[0 2 1 1 2 0 0 1 1 0]
TEST: [5 5 5 5 6 6 7]
[2 1 1 1 0 2 0]
TRAIN: [1 1 4 5 5 5 5 6 6 7 8 8]
[0 2 1 2 1 1 1 0 2 0 1 0]
TEST: [2 2 3 3 3]
[1 1 2 0 0]
TRAIN: [2 2 3 3 3 5 5 5 5 6 6 7]
[1 1 2 0 0 2 1 1 1 0 2 0]
TEST: [1 1 4 8 8]
[0 2 1 1 0]
๋ฐ๋ผ์ ์คํฌ๋ฆฐ์ท์ผ๋ก๋ ๋ฐ์ดํฐ ๋ ์ด์์์ด ๋ฌด์์ธ์ง, ์ด๋ค ์ผ์ด ์ผ์ด๋ ์ง ์ ๋๋ก ๋ณผ ์ ์๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ์ ๋ฌด์จ ์ผ์ด ์ผ์ด๋๊ณ ์๋์ง ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. scikit-learn ๋ฒ์ ๋ฌธ์ (์ ๋ 0.22.2๋ฅผ ์ฌ์ฉํ๊ณ ์๊ธฐ ๋๋ฌธ์)๊ฐ ์๋์ง ํ์ธํ๊ธฐ ์ํด ์ฌ๊ธฐ์์ ๋ณด์ฌ์ค ์์ ๋ฅผ ๋จผ์ ์ฌํํ๊ณ ์ฌํํ ์ ์๋ค๋ฉด ์์ ๋ถ๋ถ๋ถํฐ ์์ํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ ํ ์คํธํฉ๋๋ค. ~104k ์ํ์ ์ฌ์ฉํ๋ ๊ฒ์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ด๋ ต์ต๋๋ค.
@hermidalc ๋ต๋ณ ๊ฐ์ฌํฉ๋๋ค!
์ค์ ๋ก ์์ ๊ฒฐ๊ณผ๋ฅผ ์ฌํํ ์ ์์ผ๋ฏ๋ก ์ง๊ธ์ ๋ ์์ ๋ฐ์ดํฐ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ณ ์์ต๋๋ค.
+1
๋ด๊ฐ ์ด ๋ฌธ์ ๋ฅผ ๊ณจ๋ผ๋ ์๊ด์๋์?
https://github.com/scikit-learn/scikit-learn/issues/13621#issuecomment -600894432์ ํจ๊ป #15239์ ์ด๋ฏธ ๊ตฌํ์ด ์๊ณ ๋จ์ ํ
์คํธ๋ง ๋จ์ ๊ฒ ๊ฐ์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ธก์ ์ ๋ฐ๋ณตํด์ผ ํ๋ ๊ฒฝ์ฐ ์ํ ๋ฐ ์๋ฌผํ์์ ๋งค์ฐ ์ผ๋ฐ์ ์ธ ์ฌ์ฉ ์ฌ๋ก์ ๋๋ค.
์: MR ์ด๋ฏธ์ง์์ ์์ธ ํ์ด๋จธ๋ณ(AD) ๋ ๊ฑด๊ฐํ ๋์กฐ๊ตฐ๊ณผ ๊ฐ์ ์ง๋ณ์ ๋ถ๋ฅํ๋ ค๊ณ ํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค. ๋์ผํ ์ฃผ์ ์ ๋ํด ์ฌ๋ฌ ์ค์บ์ด ์์ ์ ์์ต๋๋ค(์ถ์ ์ธ์ ๋๋ ์ข ๋จ ๋ฐ์ดํฐ์์). ์ด 1000๋ช ์ ํผํ์๊ฐ ์๊ณ ๊ทธ ์ค 200๋ช ์ด AD(๋ถ๊ท ํ ํด๋์ค)๋ก ์ง๋จ๋๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค. ๋๋ถ๋ถ์ ํผ์ฌ์ฒด๋ ํ ๋ฒ ์ค์บํ์ง๋ง ์ผ๋ถ๋ 2~3๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ถ๋ฅ๊ธฐ๋ฅผ ํ๋ จ/ํ ์คํธํ ๋ ๋ฐ์ดํฐ ๋์ถ์ ๋ฐฉ์งํ๊ธฐ ์ํด ๋์ผํ ์ฃผ์ ์ ์ด๋ฏธ์ง๊ฐ ํญ์ ๋์ผํ ์ ํ์ ์๋์ง ํ์ธํ๋ ค๊ณ ํฉ๋๋ค.
์ด๋ฅผ ์ํด StratifiedGroupKFold๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ฐ์ฅ ์ข์ต๋๋ค. ํด๋์ค ๋ถ๊ท ํ์ ์ค๋ช ํ๊ธฐ ์ํด ๊ณ์ธตํํ์ง๋ง ์ฃผ์ ๊ฐ ๋ค๋ฅธ ํด๋์ ๋ํ๋์ง ์์์ผ ํ๋ค๋ ๊ทธ๋ฃน ์ ์ฝ ์กฐ๊ฑด์ด ์์ต๋๋ค.
์ฃผ์: ๋ฐ๋ณต ๊ฐ๋ฅํ๊ฒ ๋ง๋๋ ๊ฒ์ด ์ข์ต๋๋ค.
์๋๋ kaggle-kernel ์์ ์๊ฐ์ ๋ฐ์ ๊ตฌํ ์์ ๋๋ค.
RepeatedStratifiedKFold
(๊ฐ์ ๊ทธ๋ฃน์ ์ํ์ด ๋ ํด๋์ ๋ํ๋ ์ ์์)์RepeatedStratifiedGroupKFold
๋น๊ต: